简体   繁体   English

为什么我会收到 nullpointerexception (Android)?

[英]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:计数器作为 int 初始化为 0,它跟踪 next 被点击的次数以存储 v 这里是代码的一部分,第 67 行和 216 行被标记:

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在第 64 行,您在“值”object 上使用toString ,它可能会返回 TextView toString()的默认实现并且可能不是 0 长度

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:但是在第 216 行,您使用了正确的方法来获取文本值,并且由于您没有对其进行测试,因此可能是 null:

value.getText().toString()

Change on line 64 to something like this:将第 64 行更改为如下内容:

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

(and similar for the angle value ) (和angle值类似)

If JScoobyCed answer isnt the null pointer, then it might be the array.如果 JScoobyCed 答案不是 null 指针,那么它可能是数组。

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

maybe you want:也许你想要:

info[counter - 1].value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么会出现NullPointerException? - Why am I getting NullPointerException? 为什么我从Android 7 Nougat收到NullPointerException? - Why am I getting a NullPointerException from Android 7 Nougat? 为什么我在Android 4.0.3中得到TimePicker NullPointerException? - Why am I getting TimePicker NullPointerException in android 4.0.3? 为什么我会得到一个nullpointerexception,可能是类错误(Android)? - Why am I getting a nullpointerexception, probably a class error (Android)? 为什么在添加linearlayout(Android)时会出现nullpointerexception? - Why am I getting a nullpointerexception when adding a linearlayout (Android)? Android - 为什么我在SQLiteOpenHelper onCreate中得到NullPointerException? - Android - Why am I getting NullPointerException in SQLiteOpenHelper onCreate? 为什么我在ImageView上收到NullPointerException? - Why I am getting NullPointerException on ImageView? 为什么我在这里得到NullPointerException? - Why am I getting NullPointerException here? 为什么在asyncTask中通过AlertDialog收到NullPointerException? - Why am I getting a NullPointerException with AlertDialog in an asyncTask? 为什么在Firebase中的onChildAdded中得到NullPointerException? - Why am I getting NullPointerException in onChildAdded in Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM