File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes info.java:52: variable x might not have been initialized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "info.java:52: variable x might not have been initialized" Watch "info.java:52: variable x might not have been initialized" New topic
Author

info.java:52: variable x might not have been initialized

Logan Knecht
Greenhorn

Joined: Jun 05, 2008
Posts: 16
I wrote some code and I keep getting

info.java:52: variable x might not have been initialized System.out.println(myPeople[x].first + " " + myPeople[x].last + " is " + myPeople[x].age);

I tried figuring out if it was a placement thing, of if it was something else, but I have no idea why it thinks x doesn't exist.

Also, does anyone have a suggestion on how to better do the if statement that assigns x a value?



Thank you for any help or suggestions.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi,

in java,local variable must initialized before it is used .

so , you can initialize like below
---------------------------------
int x=6(or give some other value)
----------------------------------
Thanks & Regards,
seetharaman.v
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi Logan,

It doesn't think x doesn't exist; it thinks (rightly) that the program could get to that println() line without a value ever being assigned to x. Imagine, for example, that you run "java info Ernest". We skip over all the if statements at the beginning, and end up in the "else" with the println() in it.

You could fix this either by assigning some default value to x in the "else" of the first chain of if/elses (which would be ugly, and wrong) or by reporting an error and exiting if the name isn't one of the ones you expect, again, in that final "else" of the first chain.

Note that you can't compare Strings with the "==" operator -- it won't work. "==" compares two objects to see if they're physically the same bit in memory. You don't want that -- you want to compare Strings to see if they're the same length, with the same characters, in the same order. That's what the "equals()" method does:

if (name.equals("tyler")) ...


[Jess in Action][AskingGoodQuestions]
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Originally posted by Ernest Friedman-Hill:
*snip*

Note that you can't compare Strings with the "==" operator -- it won't work. "==" compares two objects to see if they're physically the same bit in memory. You don't want that -- you want to compare Strings to see if they're the same length, with the same characters, in the same order. That's what the "equals()" method does:

if (name.equals("tyler")) ...


There are some situations where you can use the == operator to compare strings. More information:

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html


Java hobbyist.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Originally posted by Ronald Schild:
There are some situations where you can use the == operator to compare strings.

That's true, but in 99.999% of the cases you do not want to use == to compare strings. In any case, == never compares strings by content - even in the cases where you could use == to compare strings.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Logan Knecht
Greenhorn

Joined: Jun 05, 2008
Posts: 16
Ok so I think I get what you're saying. You're saying that x isn't being declared because it wasn't being seen first, so I have to declare it hire up, and outside of the if statements.

So What if I just put the system.out.println statements inside each if statement. Is there a better way to achieve this.

When my code is like this now, though, I get

"Exception in thread "main" java.lang.NoSuchMethodError: main"

Is there any better way to do this?

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Logan Knecht:
Ok so I think I get what you're saying. You're saying that x isn't being declared because it wasn't being seen first, so I have to declare it hire up, and outside of the if statements.



No, actually, that's not what I'm saying. I'm saying that it's possible for the code to execute up to the print statements without a value ever being assigned to x. Having a value assigned is different from being declared or being visible. All you have to do is make sure that the print statements will never be executed unless at some previous point a line is guaranteed to be executed that says "x = X" where X is any value.

For example, changing the declaration line to this:

int x = -1;

would make the program compile. But if you typed in a name besides the three expected ones, you'd get an exception complaining about the negative array index.
Logan Knecht
Greenhorn

Joined: Jun 05, 2008
Posts: 16
I tried assigning x=-1 to the first code I posted and I keep getting that error,

"Exception in thread "main" java.lang.NoSuchMethodError: main"

Do you know why that is?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
When you run the code, you type
java Personal
??
because that's where your main() method is.

If you type
java info
you will get the error you describe because the info class has no main method.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Also note that you want to compare Strings using the equals() method rather than ==
for example
name.equals("tyler")
rather than
name == "tyler"
 
 
subject: info.java:52: variable x might not have been initialized