1

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) 
0

1 Answer 1

3

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.

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

2 Comments

- posted the logcat output of the crash
@user963070 - You're almost certainly referencing an instance variable that has a default value of null in the initializer for some other instance variable. Check every field initializer expression. Was that the entire logcat output? I'm surprised there's no source line identified.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.