user963070
user963070

Reputation: 639

can't find error android

I have the following code:

Log.e("MainScreen", "1");  
ListAction listAction = new ListAction(this);
Log.e("MainScreen", "2");

The code only runs until it meets Log.e("MainScreen", "1"). Then it crashes. However, I do not know how this is possible since the program never gets to the first line of the ListAction constructor (Log.e("ListAction", "1");) Also, when I remove the ListAction listAction line, it goes on fine.

And the mentioned constructor:

public ListAction(Context context) {
    Log.e("ListAction", "1");
    this.db = new DataBaseCreation(context);
    Log.e("ListAction", "2");
    db.open();
    Log.e("ListAction", "3");
    this.bdd = db.getBDD();
    Log.e("ListAction", "4");

No Logs are posted when running the code.

What gives? What is the problem?

Output:

03-26 03:54:54.680: E/MainScreen(534): 1

03-26 03:54:54.680: D/AndroidRuntime(534): Shutting down VM
03-26 03:54:54.690: W/dalvikvm(534): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-26 03:54:54.721: E/AndroidRuntime(534): FATAL EXCEPTION: main
03-26 03:54:54.721: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.GroceryListManager/android.GroceryListManager.MainScreen}: java.lang.NullPointerException
03-26 03:54:54.721: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)

Upvotes: 1

Views: 90

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234797

If you have any instance variables with initializers in class ListAction, the initializers will execute before the first line of the constructor. For instance, your class might look like this:

public class ListAction {
    public ListAction(Context context) {
        . . .
    }

    private Thing mThing = new Thing();
}

If one of the initializers (like new Thing()) raises an exception, that will produce the behavior you describe. It would help if you posted the logcat output of the crash.

Upvotes: 3

Related Questions