简体   繁体   中英

Why am I getting a nullpointerexception (Android)?

I have absolutely no idea why I am getting a nullpointerexception here because almost this same exact code worked in a different program. I hope it's something simple.

Here is the logcat:

04-09 08:18:57.903: ERROR/AndroidRuntime(14378): java.lang.NullPointerException
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at com.prattia.webs.cheaterphysics.Vectors.storeInfo(Vectors.java:216)
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at com.prattia.webs.cheaterphysics.Vectors$2.onClick(Vectors.java:67)
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at android.view.View.performClick(View.java:2465)
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at android.view.View$PerformClick.run(View.java:8907)
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at android.os.Handler.handleCallback(Handler.java:587)
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 08:18:57.903: ERROR/AndroidRuntime(14378):     at android.os.Looper.loop(Looper.java:123)

Counter is as int initialized at 0 that keeps track of how many times next has been clicked in order to store vAnd here is part of the code with lines 67 and 216 marked:

next.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View v) {
                error.setText("");
                if(value.toString().length()==0||angle.toString().length()==0)
                    error.setText("Must enter both value and angle");
                else{
                    storeInfo(); //67
                    counter++;
                }
            }
        });

public void storeInfo(){
        Doublify(value);
        Doublify(angle);

    //216   info[counter].value = Double.parseDouble(value.getText().toString());
        info[counter].angle = Double.parseDouble(angle.getText().toString());
        info[counter].radian = rad.isChecked();
        if(q1.isChecked())
            info[counter].quad=1;
        if(q2.isChecked())
            info[counter].quad=2;
        if(q3.isChecked())
            info[counter].quad=3;
        if(q4.isChecked())
            info[counter].quad=4;
        angle.setText("");
        value.setText("");
    }

On line 64 you use toString on the 'value' object which is likely to return the default implementation of the TextView toString() and probably not a 0 length

value.toString()

but on line 216 you use the correct way to get the text value, and since you didn't test for it it is possible to be null:

value.getText().toString()

Change on line 64 to something like this:

if(value.getText() == null || value.getText().toString().length() == 0)

(and similar for the angle value )

If JScoobyCed answer isnt the null pointer, then it might be the array.

info[counter].value // might be null because array's start at 0 not 1

maybe you want:

info[counter - 1].value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM