Results 1 to 4 of 4
  1. #1
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default variable name might not have been initialized

    Java 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?
    Last edited by p0rnstar; 01-26-2010 at 12:28 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Just a remark: your indentation style (or lack thereof) sucks; it is extremely difficult to see the flow of control or where blocks start or end; for your own sake improve your indents. IDEs can do that for you.

    kind regards,

    Jos

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Set 'name' to null when you declare it.

  4. #4
    p0rnstar is offline Member
    Join Date
    Jan 2010
    Posts
    14
    Rep Power
    0

    Default

    okey i fixed that, but now the same error with age1, and i cant do it like age1 = null;


    got that :)
    integer variable;
    variable = null;

    works perfectly :)
    Last edited by p0rnstar; 01-26-2010 at 03:46 PM.

Similar Threads

  1. Replies: 0
    Last Post: 04-20-2009, 11:08 AM
  2. Variable not initialized after while loop
    By ejs7597 in forum New To Java
    Replies: 6
    Last Post: 02-28-2009, 05:00 AM
  3. [SOLVED] &quot;start: applet not initialized&quot;
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-24-2009, 02:10 AM
  4. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 01:42 AM
  5. Not initialized variable
    By bbq in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •