0

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!

Tomik
  • 23,857
  • 8
  • 121
  • 100
NiPfi
  • 1,710
  • 3
  • 18
  • 28
  • 2
    what is line 150 in `DBAdapter.java` ? – Mohsin Naeem Oct 15 '12 at 17:47
  • Null Pointer Exceptions are nice because they tell you the _exact_ line it occurred on `Caused by: java.lang.NullPointerException ... at maturaarbeit.nicola_pfister.marks.database.DBAdapter.getAllSubjects(DBAdapter.getAllSubjects(DBAdapter.java:150)` The problem is on line 150 in `DBAdapter.java`, start here. – Sam Oct 15 '12 at 17:48
  • That's strange because I never had trouble with this class before... – NiPfi Oct 15 '12 at 17:53
  • The line 150 is the first of the class which returns the subjects for use in the spinner, in this case. There has to be some conflict about using the subjects before. I'll probably just remove the spinner because it's already defined with the call of the "Marks" class which subject should be modified. public Cursor getAllSubjects() { return db.query(DATABASE_TABLE_SUBJECTS, new String[] { KEY_ROWID, KEY_SUBJECT }, null, null, null, null, null); } } – NiPfi Oct 15 '12 at 18:11
  • you need to narrow down your problem, after debugging..:) – Mohsin Naeem Oct 15 '12 at 18:18

1 Answers1

0

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

Vincent Mungai
  • 202
  • 3
  • 4