2

Here is the complete code for my MainActivity. As you can see I added just 2 lines:

ArrayList<String> y = null; 
y.add(new String("buggy")); // When I remove this line it works

When I comment out y.add() the Application does not crash.

My question is: Why does this use of y.add() cause the program to fail?

The mobile device is a Samsung Galaxy Mini S5570, running Android 2.3.4

Here is the complete code, followed by the LogCat:

package com.testbug;
import javax.*;
import java.util.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; 
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //
    // Only these 2 lines added by me. The rest is automatically generated in Eclipse.
    ArrayList<String> y = null; 
    y.add(new String("buggy")); // When I remove this line it works
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

Stacktrace:

07-05 21:01:37.919: D/AndroidRuntime(32067): Shutting down VM
07-05 21:01:37.919: W/dalvikvm(32067): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-05 21:01:37.929: E/AndroidRuntime(32067): FATAL EXCEPTION: main
07-05 21:01:37.929: E/AndroidRuntime(32067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testbug/com.testbug.MainActivity}: java.lang.NullPointerException
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.os.Looper.loop(Looper.java:130)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.main(ActivityThread.java:3687)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at java.lang.reflect.Method.invokeNative(Native Method)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at java.lang.reflect.Method.invoke(Method.java:507)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at dalvik.system.NativeStart.main(Native Method)
07-05 21:01:37.929: E/AndroidRuntime(32067): Caused by: java.lang.NullPointerException
07-05 21:01:37.929: E/AndroidRuntime(32067):    at com.testbug.MainActivity.onCreate(MainActivity.java:20)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 21:01:37.929: E/AndroidRuntime(32067):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-05 21:01:37.929: E/AndroidRuntime(32067):    ... 11 more
2
  • I should add that I explicitly added the rt.jar file in the Java Build Path, and I also tried adding it as a System library. Neither of these changes made any impact on the App. Commented Jul 5, 2012 at 21:35
  • 2
    I would suggest you take a short break from your android project and brush up on your Java syntax, there are many free learning materials available online. You will have a much easier time with Android programming if you have your java fundamentals down a bit better. Commented Jul 5, 2012 at 21:36

2 Answers 2

6

you are setting it to null and then calling a method on it... This will result in null pointer

Initialize it like this before calling add():

ArrayList<String> y = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. You got it. I think Android is less forgiving than "normal" Java. I'm sure I've done this and gotten away with it.
0

You have to initialize a declared array. If you try to add items to a null array it will cause a java.lang.NullPointerException (exactly what you have).

You have to do something like:

//Declaring and Initializing
ArrayList<String> y = new ArrayList<String>();
    y.add(new String("buggy"));

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.