1

I defined a Double instance variable like this:

public class CurrencyActivity extends Activity {

    private Button convertBtn;
    private Double SEKrate;

....
public void convertCurrency() {
....
Double inputNum = Double.parseDouble(editTextStr);
Double result = inputNum*SEKrate;
....
}
....
}

When running, it says NullPointer exception at the line

Double result = inputNum*SEKrate;

It seems like SEKrate is initialized to null, but autoboxing is not performed here. any thought on this?

Michael SM
  • 715
  • 3
  • 11
  • 25
  • And where do you make `SEKrate` something other than `null`, exactly? Also ... don't start variable names with uppercase letters. – Brian Roach Jan 26 '13 at 19:42
  • A related thread on this: http://stackoverflow.com/questions/3547804/double-null-is-causing-a-nullpointerexception – Michael SM Jan 26 '13 at 19:49
  • this code is written by someone else, I am debugging it - sorry about non-java variable names. Obviously as an instance variable, SEKrate is initialzed to null and there is no other assignment to it before the line in issue. so the compiler sees null as a Double and tries to unboxing it, then NPE throws. – Michael SM Jan 26 '13 at 19:56

1 Answers1

3

If you do not initialize a Double it is null and null can not be unboxed to a double.

So initalize your variable.

  private Double SEKrate=0;
MrSmith42
  • 9,961
  • 6
  • 38
  • 49