variable name might not have been initialized
Code:
import javax.swing.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.io.File;
import java.util.*;
class RPGContents extends JPanel
{
RPGContents()
{
boolean flagA=true;
boolean flagB=true;
String name;
int name_length, age1;
while(flagA)
{
String inputString = JOptionPane.showInputDialog(null,"What is the name of your character?");
Scanner keyboard = new Scanner (inputString);
name = keyboard.next();
name_length=name.length();
if (name_length>=3 && name_length<=12)
{
flagA=false;
}else
{
flagA=true;
}
while(flagB)
{
String age = JOptionPane.showInputDialog(null,"What is the age of your character?");
Scanner keyboard1 = new Scanner (age);
age1 = keyboard1.nextInt();
if (age1>=18 && age1<=72)
{
flagB=false;
}else
{
flagB=true;
}
}
}
JPanel profile = new JPanel();
JLabel labelProf = new JLabel();
labelProf = new JLabel();
Icon pic1 = new ImageIcon ("IMAGES/portrait.JPG");
labelProf.setIcon (pic1);
labelProf.setHorizontalTextPosition(SwingConstants.CENTER);
labelProf.setVerticalTextPosition(SwingConstants.BOTTOM);
labelProf.setText(" " + name );
this.add(profile);
this.add(labelProf);
Problem:
RPGContents.java:55: variable name might not have been initialized
labelProf.setText(" " + name );
^
solution is to move last 2 brackets after the label.Prof.setText(" "+ name);
but meybe there is another way?