0

I am trying to fill an array of an "Runners" with an information in an ArrayList but I get a null pointer exception. I've printed out the size of both the arraylist and the array of Runners and they are what they should be.

--I only included code that I thought was necessary to solve the problem, please comment if you need more

 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 (sorry I couldnt get it formatted)

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
  • 1
    what do you have on this line? MainActivity.java:99 Commented Apr 15, 2014 at 13:19
  • If that's all of the code we need, then you haven't initialised your runnerName EditText. Commented Apr 15, 2014 at 13:21
  • After the initialisation of array r all elements are null. If you try to call a method of a object that is null, that causes the exception. Commented Apr 15, 2014 at 13:24

3 Answers 3

4

You haven't actually initialized r[i]:

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

you have an array or Runners but each entry in the array is null by default. Add

r[i] = new Runner();

to initialize an entry there.

Sign up to request clarification or add additional context in comments.

Comments

0

You created a new array of runners in onClickFinish. However, you don't create new Runner objects in the array so r[i] is null.

Comments

-1

initialize runnerName before u use it.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.