1

I do not understand why the code in this try block does not execute. I get a compilation error that says when I use these variables after the try block, they may not be initialized.

double star, planet, posLife, actLife, intelLife, comm, length;

try{
    star = Double.parseDouble(factor.elementAt(0).getText());    
    planet = Double.parseDouble(factor.elementAt(1).getText());
    posLife = Double.parseDouble(factor.elementAt(2).getText());
    actLife = Double.parseDouble(factor.elementAt(3).getText());
    intelLife = Double.parseDouble(factor.elementAt(4).getText());
    comm = Double.parseDouble(factor.elementAt(5).getText());
    length = Double.parseDouble(factor.elementAt(6).getText());
} catch(NumberFormatException E){ 
    System.err.println("NumberFormatException");
}   
Luca
  • 515
  • 5
  • 17
  • 2
    Scope of local variables is limited by `{` and `}` braces. This means you will not have access to variables declared inside `try` block in rest of code (like in `catch` or after `try`. But judging by error you mentioned it may seem that you shadowed variables declared before `try` by redeclaring new variables with same names. In that case simply remove type like change `double star = ...` to ` star = ...` to avoid redeclaring. – Pshemo Apr 14 '15 at 21:54
  • 1
    Possible duplicate: http://stackoverflow.com/questions/17399669/initialising-a-variable-inside-try-block-a-workaround – Beko Apr 14 '15 at 21:55
  • Actually, I had the variables declared outside the block before. I still had the same problem, because no variable assignments were being made within the try block. I appreciate your help, but the situation remains largely unchanged. – Luca Apr 14 '15 at 22:19
  • Okay, I figured it out. I had tried declaring the variables outside of the try block, but strangely I have to also initialize the variables outside of the try block to allow the variables to take assignments within the try block. Weird... – Luca Apr 14 '15 at 22:30

4 Answers4

5

First, the variables you define in the try block should not be visible outside the try block ; so I believe you must have another double star; etc. before the try.

Now, I'll assume your code is more like the following, because the error you spoke of could not occur with the code you gave us:

double a;
try {
    a = Double.parseDouble(/* blah */);
} catch (NumberFormatException e) {
    System.err.println("NumberFormatException");
}

Here, after the execution of this block of code, a may perfectly well be uninitialized. Indeed, if any error occured in Double.parseDouble, the assignment to a would be skipped to jump to the catch block, which does not return.

Thus, after the above block of code, a may be uninitialized, hence the error message.

To fix it, you should remove the types before the variables inside the try block, and either provide a default value in case it's impossible to parse the double, or, if the exception cannot be recovered from in this scope, do not catch it in the first place.

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
Ekleog
  • 1,054
  • 7
  • 19
3

You should assign a default value to the doubles before trying to access them, I've given mine a value of -1

You must declare the vars you're using in the try-catch before the try block if you need to used them after the try-catch block.

Here is an example of this.

//Some default value
double star = -1;

try {
    star = 0;
} catch(NumberFormatException E){ 
    System.err.println("NumberFormatException");
}

//You can use this now.
System.out.println(star);
Gready
  • 1,134
  • 8
  • 15
  • Oops, sorry, I downvoted you based on the fact that the variables are *not* declared in the `try` block, and the edit history showed that they weren't declared in the `try` block in the original version of the question, either--but I just noticed that you probably wrote this answer in response to the *second* version of the question, in which the variables *are* declared in the `try` block. My bad. – Kyle Strand Apr 15 '15 at 00:00
  • Technically my code sample is correct, you need to assign the double's to an initial value, see my other answer. Remove that down vote if at-all possible. :-) – Gready Apr 15 '15 at 00:36
  • I realize that, which is why I apologized. (I did the same thing for Steve Kuo's answer, but since his still ignores the underlying problem, I didn't bother apologizing.) My vote is locked in unless your answer is edited--which, by the way, it *should* be, because rather than having two answers, you should simply edit this one to reflect updates to the original question. – Kyle Strand Apr 15 '15 at 00:36
  • I've updated this to reflect the changes. I'll edit my answer next time instead of creating a new one, makes sense. Thanks Kyle. – Gready Apr 15 '15 at 00:41
  • No problem. Ideally you should also delete your other answer; there are very few instances when posting multiple answers on the same question are encouraged. (And in fact I think it's somewhat debated whether this is *ever* desirable.) – Kyle Strand Apr 15 '15 at 00:42
  • I can see instances where more then one answer is helpful, this is _not_ one of those times though, so I've deleted it. – Gready Apr 15 '15 at 00:44
1

The scope of star, planet, etc are limited to the try block. You'll need to declare them outside and before the try block.

double star = null;
try {
  star = Double.parseDouble(...
} catch(NumberFormatException e) {
  ...
}

System.out.println(star);
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
1

You mention that you've already figured it out: "strangely I have to also initialize the variables outside of the try block..."

That's not "strange". Think about all possible paths this code can take at runtime, including the exceptional one. If you catch an exception, what happens? The program continues, but none of the code in the try block is guaranteed to have been executed. So you might have uninitialized variables. And by "might" I mean "definitely will," because the fact that an exception occurs at all means that something in the try block didn't finish executing, so at a minimum, you know that the last statement that happens in the block did not go to completion--which means that length was never assigned to.

And therefore, if an exception occurs, you will leave the try block with length (and probably others, possibly all of your doubles) uninitialized.

Note also that although your question asks why the code in the try block "doesn't execute", the error you're seeing is a compile error, stating that the variables may not be initialized. If the compiler cannot ensure that a variable will be initialized, it throws this error--and if your initialization code is in any sort of conditionally-executed block (such as an if statement or, as in this case, a try block), then of course the compiler can't ensure that the block will always be executed, so it can't guarantee that the variables will be initialized!

Kyle Strand
  • 15,941
  • 8
  • 72
  • 167