android应用程序中的java.lang.NullPointerException。

时间:2014-04-15 13:18:09

标签: java android nullpointerexception

我正在尝试填充一个"跑步者"使用ArrayList中的信息,但我得到一个空指针异常。我已经打印出了arraylist和Runner阵列的大小,他们应该是这样。

- 我只包含了我认为解决问题所必需的代码,如果您需要更多,请发表评论

 private Runner r[];
 private EditText runnerName,trackLength,raceDistance;
 private ArrayList<String> runners = new ArrayList<String>();

 public void onClickSetRunner(View v){
    runners.add(runnerName.getText().toString());
    runnerName.setText("");
}

 public void onClickFinish(View v){         
    r = new Runner[runners.size()];     
    int i =0;       
    for(String name : runners){             
        r[i].setFirst(name);
        i++;
    }


}

Logcat(抱歉,我无法将其格式化)

04-15 09:09:45.009: E/AndroidRuntime(28225): FATAL EXCEPTION: main
04-15 09:09:45.009: E/AndroidRuntime(28225): Process: com.example.runtracker, PID: 28225
04-15 09:09:45.009: E/AndroidRuntime(28225): java.lang.IllegalStateException: Could not execute method of the activity
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.view.View$1.onClick(View.java:3865)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.view.View.performClick(View.java:4480)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.view.View$PerformClick.run(View.java:18673)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.os.Handler.handleCallback(Handler.java:733)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.os.Looper.loop(Looper.java:157)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.app.ActivityThread.main(ActivityThread.java:5872)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at java.lang.reflect.Method.invoke(Method.java:515)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at dalvik.system.NativeStart.main(Native Method)
04-15 09:09:45.009: E/AndroidRuntime(28225): Caused by: java.lang.reflect.InvocationTargetException
04-15 09:09:45.009: E/AndroidRuntime(28225):    at java.lang.reflect.Method.invokeNative(Native Method)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at java.lang.reflect.Method.invoke(Method.java:515)
04-15 09:09:45.009: E/AndroidRuntime(28225):    at android.view.View$1.onClick(View.java:3860)
04-15 09:09:45.009: E/AndroidRuntime(28225):    ... 11 more
04-15 09:09:45.009: E/AndroidRuntime(28225): Caused by: java.lang.NullPointerException
04-15 09:09:45.009: E/AndroidRuntime(28225):    at com.example.runtracker.MainActivity.onClickFinish(MainActivity.java:99)
04-15 09:09:45.009: E/AndroidRuntime(28225):    ... 14 more

3 个答案:

答案 0 :(得分:4)

您尚未初步确定r[i]

r = new Runner[runners.size()];     
int i =0;       
for(String name : runners){             
    r[i].setFirst(name);

您有一个数组或Runner,但默认情况下,数组中的每个条目都是null。添加

r[i] = new Runner();

在那里初始化一个条目。

答案 1 :(得分:0)

您在onClickFinish中创建了一个新的跑步者数组。但是,您不能在数组中创建新的Runner对象,因此r[i]为空。

答案 2 :(得分:-1)

在使用之前初始化runnerName。

 runnerName.setText("");
    runners.add(runnerName.getText().toString());