简体   繁体   English

为什么我会得到一个nullpointerexception,可能是类错误(Android)?

[英]Why am I getting a nullpointerexception, probably a class error (Android)?

I'm stuck. 我被卡住了。 I've spent hours looking at this and I know it must be something trivial I have done wrong. 我花了几个小时研究这个问题,我知道这一定是我做错了的琐碎事情。 My native "tongue" is C++, but I wanted to work with Android. 我的母语是C ++,但是我想使用Android。 I really like using structs in C++, but discovered I had to use a work around using a class in Java. 我真的很喜欢在C ++中使用结构,但是发现我不得不绕过使用Java中的类的工作。 I've fairly well narrowed down that I am getting a nullpointer because of something wrong with my class. 由于班级出了点问题,我得到了一个空指针,这已经相当不错了。 Any ideas? 有任何想法吗?

Here is the code with line 267 marked: 这是带有第267行标记的代码:

public class Vectors extends Activity{

    Button next;

    public class infoC{
        double value = 0, angle = 0;
        boolean radian = false; //radians/degrees
        int quad = 1; //Quadrant
    }
    public infoC[] info;

    int counter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vectors);
        vectorsView = new VectorsView(this);
        l = (LinearLayout) findViewById(R.id.llCanvasV);
        l.addView(vectorsView);

        Initialize();       

        next.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View v) {
                error.setText("");
                if(value.getText().toString().length()==0||value.getText()==null||angle.getText().toString().length()==0||angle.getText()==null)
                    error.setText("Must enter both value and angle");
                else{
                    Log.e("Counter", Integer.toString(counter));
                    Log.e("Value", value.getText().toString());
                    storeInfo(); //121
                    counter++;
                }
            }
        });
            }
public void storeInfo(){
        Doublify(value);
        Doublify(angle);

        String temp;
        temp=value.getText().toString();

        info[counter].value = Double.parseDouble(temp);//267, Inserted temp instead of 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("");
    }
}

Here is the logcat: 这是logcat:

04-13 18:50:45.188: ERROR/AndroidRuntime(9892): FATAL EXCEPTION: main
04-13 18:50:45.188: ERROR/AndroidRuntime(9892): java.lang.NullPointerException
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at com.prattia.webs.cheaterphysics.Vectors.storeInfo(Vectors.java:267)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at com.prattia.webs.cheaterphysics.Vectors$7.onClick(Vectors.java:121)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at android.view.View.performClick(View.java:2465)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at android.view.View$PerformClick.run(View.java:8907)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at android.os.Handler.handleCallback(Handler.java:587)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at android.os.Looper.loop(Looper.java:123)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at java.lang.reflect.Method.invoke(Method.java:521)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
04-13 18:50:45.188: ERROR/AndroidRuntime(9892):     at dalvik.system.NativeStart.main(Native Method)

It doesn't look like you ever initialized your info[] array. 看起来您从未初始化过info []数组。 Thus info[counter] throws an exception. 因此info [counter]引发异常。

Could it be because you never initialize your array public infoC[] info = new infoC[size]; 可能是因为您从未初始化过数组public infoC[] info = new infoC[size]; ?

You should just debug your code, place a breakpoint at line 267 and see what the values of your variables are. 您应该只调试代码,在第267行放置一个断点,然后查看变量的值。 Are you sure your info is not null? 您确定您的信息不为空吗? I can't see any initialization being done on it. 我看不到对其进行任何初始化。

info has to be initialized before you use it. 使用前必须先初始化信息。

public infoC[] info = new infoC[10];

I'm pretty sure that's how it works in C++ too. 我很确定这也是C ++的工作方式。 Of course I don't know how many elements you will need, I put 10. So you might want to use something dynamic like: 当然,我不知道您需要多少元素,我输入了10。所以您可能想使用一些动态的东西,例如:

...
public ArrayList<infoC> info = new ArrayList<infoC>();
...
info.add(new infoC());
info.get(counter).value = Double.parseDouble(temp);
info.get(counter).angle = Double.parseDouble(angle.getText().toString());
info.get(counter).radian = rad.isChecked();
....

define variable l as LinearLayout before using it 在使用变量l之前将其定义为LinearLayout

this on your variable declarations, also you should declare your vectorview as view 这在您的变量声明中,也应该将vectorview声明为view

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

相关问题 为什么我会收到 nullpointerexception (Android)? - Why am I getting a nullpointerexception (Android)? 为什么会出现NullPointerException? - Why am I getting NullPointerException? Android - 为什么我在SQLiteOpenHelper onCreate中得到NullPointerException? - Android - Why am I getting NullPointerException in SQLiteOpenHelper onCreate? 为什么我从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? 为什么在添加linearlayout(Android)时会出现nullpointerexception? - Why am I getting a nullpointerexception when adding a linearlayout (Android)? 为什么我在此行上收到NullPointerException? - Why am I getting a NullPointerException on this line? 为什么在Firebase中的onChildAdded中得到NullPointerException? - Why am I getting NullPointerException in onChildAdded in Firebase? 为什么在此应用程序中出现NullPointerException? - Why am I getting a NullPointerException in this app? 为什么在.setOnTouchListener()上得到nullpointerexception? - Why am I getting a nullpointerexception on .setOnTouchListener()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM