-4

How would I rewrite this code snippet into a form that doesn't produce java.lang.NullPointerException?

Intent bank_Amount_Intent = getIntent();
Bundle bundle = bank_Amount_Intent.getExtras();
double buyin_Money = bundle.getDouble("buyin", 0);
total = buyin_Money;
bank_Amount.setText(String.valueOf(currency_Format.format(buyin_Money)));
greg-449
  • 109,219
  • 232
  • 102
  • 145
ChanChan
  • 23
  • 1
  • 6

3 Answers3

0

You first need to determine which variable is null, so use the debugger or proceed like so :

public int yourFunction(){
    Intent bank_Amount_Intent = getIntent();
    if (bank_Amount_Intent == null) {
        System.out.println("bank_Amount_Intent is null");
        return -1; //error #1
    }

    Bundle bundle = bank_Amount_Intent.getExtras();
    if (bundle == null) {
        System.out.println("bundle is null");
        return -2; //error #2
    }

    double buyin_Money = bundle.getDouble("buyin", 0);
    total = buyin_Money; // assuming "total" also is a double
    bank_Amount.setText(String.valueOf(currency_Format.format(buyin_Money)));
    return 0; //no error
}

You can then see in your console, which value was null. You can also use the returned value to determine the error code of that function.

When you know which value was null, you can do of two things : evaluate whether you want to keep it null or not

If you agree to keep a null value in that method, you can use it to pop an error or whatever you may want to do with a null value

If you don't want a null value, you can create an instance of an object of the type you need. eg, if bundle was null, you can to this : bundle = new Bundle();. Or you can preferably check why this value is null in the first place and correct the error. It is possible that an other function returns null and you only get the NullPointerException further in the execution.

Sirmyself
  • 1,464
  • 1
  • 14
  • 29
0

Well Im gonna speculate based on the little amount of code shown. Since currency_Format its only shown being used Im assuming you have successfully instantiated it so that is not the source of the exception.

That leaves only bank_Amount_Intent and bundle.

While getIntent might return null on some extreme edge cases, my bet is on bundle, you are assuming the intent has a Bundle and might be where your null pointer resides.

try double buyin_Money = bundle == null ? 0: bundle.getDouble("buyin", 0);

that way to check first if the bundle if null, and if it is in fact null you set 0 value for your variable.

Marco Pierucci
  • 1,125
  • 8
  • 23
-2
try{
      Intent bank_Amount_Intent = getIntent();
        Bundle bundle = bank_Amount_Intent.getExtras();
        double buyin_Money = bundle.getDouble("buyin", 0);
        total = buyin_Money;
        bank_Amount.setText(String.valueOf(currency_Format.format(buyin_Money)));
}
catch(Exception e)
{
e.printStackTrace();
}

this won't produce any exception but if you're trying to work on null object then you won't be able to get any result.

happy coding!!

Nirmal Prajapat
  • 1,735
  • 1
  • 12
  • 23