Android – (Android) Unable to start activity ComponentInfo: java.lang.NullPointerException

androidandroid-activityruntimeexception

I have a problem, I get RuntimeException everytime I try to start a new activity with this code:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_add:
        startActivity(new Intent(this, Addmark.class));
        break;
    }
    return super.onOptionsItemSelected(item);
}

The activity which I try to start the new activity in was called using this code:

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
    Cursor cursor = db.getSubject(id);
    String subject = null;
    try {
        subject = cursor.getString(cursor.getColumnIndex("subject"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    Intent intent = new Intent(Main.this, Marks.class);
    intent.putExtra("selected", subject);
    startActivity(intent);
}

Because there are many causes for this exception type I was unable to find a solution but it's probably some simple thing I'm just not seeing it.

Here's the LogCat:

10-15 17:30:33.064: E/AndroidRuntime(5926): FATAL EXCEPTION: main
10-15 17:30:33.064: E/AndroidRuntime(5926): java.lang.RuntimeException: Unable to start activity ComponentInfo{maturaarbeit.nicola_pfister.marks/maturaarbeit.nicola_pfister.marks.Addmark}: java.lang.NullPointerException
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.os.Looper.loop(Looper.java:137)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at java.lang.reflect.Method.invokeNative(Native Method)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at java.lang.reflect.Method.invoke(Method.java:511)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at dalvik.system.NativeStart.main(Native Method)
10-15 17:30:33.064: E/AndroidRuntime(5926): Caused by: java.lang.NullPointerException
10-15 17:30:33.064: E/AndroidRuntime(5926):     at maturaarbeit.nicola_pfister.marks.database.DBAdapter.getAllSubjects(DBAdapter.java:150)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at maturaarbeit.nicola_pfister.marks.Addmark.getSubjectSpinner(Addmark.java:38)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at maturaarbeit.nicola_pfister.marks.Addmark.onCreate(Addmark.java:31)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.Activity.performCreate(Activity.java:4465)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-15 17:30:33.064: E/AndroidRuntime(5926):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-15 17:30:33.064: E/AndroidRuntime(5926):     ... 11 more

Thank you for helping!

Best Answer

The error could be caused by two reasons 1) In Addmark Activity, check whether the ids exist in this case R.id.view, most probably could be the cause to the NUllPointerException

   TextView tv = findViewById(R.id.view);
   tv.setText("Some text");

2) If you use startActivityForResult() you have to override this method in the calling Activity. For more reference read https://stackoverflow.com/a/23925196/6751183