user2019287
user2019287

Reputation: 187

How to resolve this Null pointer exception?

I got an error at principal==0.0 and I don't understand why this happened.

if(et1.getText()!=null) {
    try {
        principal = Double.valueOf(et1.getText().toString());
}
catch(Exception e) {
    message = "Incorrect value";
}
}
// The problem is in the if statement below!
if(principal==0.00) {
    message = "value cannot be zero";
    title = "error";
}

The error I get is the following:

   01-31 22:21:37.700: E/AndroidRuntime(985): FATAL EXCEPTION: main
   01-31 22:21:37.700: E/AndroidRuntime(985):
   java.lang.NullPointerException 01-31 22:21:37.700:
   E/AndroidRuntime(985):   at
   com.compoundinterest.MainActivity$1.onClick(MainActivity.java:65)
   01-31 22:21:37.700: E/AndroidRuntime(985):   at
   android.view.View.performClick(View.java:2485) 01-31 22:21:37.700:
   E/AndroidRuntime(985):   at
   android.view.View$PerformClick.run(View.java:9080) 01-31
   22:21:37.700: E/AndroidRuntime(985):     at
   android.os.Handler.handleCallback(Handler.java:587) 01-31
   22:21:37.700: E/AndroidRuntime(985):     at
   android.os.Handler.dispatchMessage(Handler.java:92) 01-31
   22:21:37.700: E/AndroidRuntime(985):     at
   android.os.Looper.loop(Looper.java:123) 01-31 22:21:37.700:
   E/AndroidRuntime(985):   at
   android.app.ActivityThread.main(ActivityThread.java:3683) 01-31
   22:21:37.700: E/AndroidRuntime(985):     at
   java.lang.reflect.Method.invokeNative(Native Method)

What might be the cause of this error and how do I solve it?

Upvotes: 0

Views: 140

Answers (4)

Christian Strempfer
Christian Strempfer

Reputation: 7383

When an exception occurs during valueOf your variable principal won't be set. And the comparsion will throw a NullPointerException, because it is auto-unboxing principal to a double, which cannot be null.

Therefore it should be

if (principal == null) {
  // error handling
} else if (principal==0.00) {

And check you logs for previous exceptions during valueOf.


I know it's your fourth question to same problem and you just copied your code from Kovge's answer. You would be learning more, if you would try to understand the given answers.

Here are some variants for your copy-and-paste approach:

  1. Ignoring null values explicitly:

    if (principal == null && principal==0.00) {
    
  2. Ignoring null values by using Doubles:

    if (Double.valueOf(0.0).equals(principal)) {
    

Upvotes: 0

Govil
Govil

Reputation: 2054

Seems principal is null.

When you do this check.

if(principal==0.00)

It actually is

if(principal.doubleValue() ==0.00)

And this cause the nullPointer because the principal is null.

So change it to:

if(principal ==null)
//return or do something
else if(principal ==0.00)

Upvotes: 0

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try this :

// Declaration on top
Double principal=0.00;

Use this :

EditText et1 = (EditText) findViewById(R.id.myEdit);

if(!et1.getText().toString().equals("")) {
    try {
        principal = Double.parseDouble(et1.getText().toString());
    }
    catch(Exception e) {
        message = "Incorrect value";
    }
}

if(principal==0.00) {
    message = "value cannot be zero";
    title = "error";
}

Upvotes: 1

MrFox
MrFox

Reputation: 1254

Without knowing more it's hard to tell. My guess is that if et1.getText() == null then principal is never initialized.

Upvotes: 0

Related Questions