Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: variable might not have been initialized!!

  1. #1
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Unhappy variable might not have been initialized!!

    I keep getting this error message with this code and I don't understand what it's telling me! Do I need to sort of merge the two chatter methods?

     
    .\Chatter.java:96: error: variable name might not have been initialized
        }
        ^
    1 error

    And that is the whole error. it points towards the } which is closing the final method (public Chatter).

    I would really appreciate if someone could help me with this, I also get the feeling that I will need to change a method in another class which uses this one once this problem has been resolved..

    The Code
     
    public class Chatter implements Runnable
    {
     
       /**
        * The name of the Chatter.
        * This should be the ID chosen by the remote user on joining
        * the chatroom.
        * This value is set in the constructor.
        *
        */
       public final String name;
     
     
    	public void run()
    	{
    	}
       /**
        * Creates a new <code>Chatter</code> instance with a given name.
        * This constructor implements operation newChatter in the
        * <a href="chatroom.maude">Chatter specification</a>.
        *
        * @param n the name for this Chatter
        */
     
       public Chatter(String n)
       {
    		name = n;
       }
     
       /**
        * Return the {@link #name name} of the Chatter.
        * This method implements operation getName in the
        * <a href="chatroom.maude">Chatter specification</a>.
        *
        * @return the {@link #name name} of the Chatter
        */
       public String getName()
       {
          return name;
       }
     
       /**
        * Send a message across the network to the remote user.
        *
        * @param msg the msg to be sent
        */
       public void sendToUser(String msg)
       {
       }
     
        Chatter theChatter;
       /**
        * Pointer to the tail of the list.
        * Default scope modifier, so that this field can be efficiently
        * accessed in class ChatterList.
        *
        */
       Chatter next;
       /**
        * Creates a new <code>ChatterNode</code> instance.
        *
        * @param c the Chatter at this node
        * @param next the tail of this list
        */
    	   public Chatter(Chatter c, Chatter next)
       {
    		this.next = next;
    		theChatter = c;
       }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: variable might not have been initialized!!

    Assign the variable a default value when you define it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: variable might not have been initialized!!

    Thanks for the reply again Norm!

    I assume that by this you mean that when I define variable name I should set it as something, i.e.

     
    public final String name = x;

    However I can't do this because it is a final string. If I get rid of the final, I get the old "could not find symbol" on x!

    Am I completely off track?

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: variable might not have been initialized!!

    What is x? It doesn't appear to be a String. Why not get rid of final and assign name an initial value that makes more sense like ""?

  5. The Following User Says Thank You to curmudgeon For This Useful Post:

    bassie (December 4th, 2012)

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: variable might not have been initialized!!

    Try adding another constructor that sets a value for the variable so that the compiler will know that the variable has been given a value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    bassie (December 4th, 2012)

Similar Threads

  1. variable might not have been initialized...help appreciated
    By neontiger in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 3rd, 2012, 05:58 PM
  2. Variable might not have been initialized
    By JavaGirl9001 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 7th, 2011, 11:24 AM
  3. variable might not be initialized problem. Can someone tell me whats wrong?
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 10th, 2011, 09:25 AM
  4. variable might not have been initialized
    By SV25 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 25th, 2011, 10:58 AM
  5. Variable might not have been initialized?
    By n56 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 03:03 PM