0

Dont Mark it As Duplicate Question, cz i've been tried to search any possible answer from Stackoverflow sublink

i have 2 spinner in my MainActivity, then i use this code to save it inside button setOnClickListener code below :

Intent i= new Intent(MainActivity.this, Pertanyaan.class);
                    i.putExtra("dosen",String.valueOf(sp.getSelectedItem()));
                    i.putExtra("matkul",String.valueOf(sp2.getSelectedItem()));
                    startActivity(i);

then in my 2ndActivity, i've tried to call the key i've declared like this:

TextView textdosen=(TextView)findViewById(R.id.txtdos);
TextView textmatkul=(TextView)findViewById(R.id.txtmat);
Bundle b=getIntent().getExtras();
String dosen=b.get("dosen").toString();
String matkul=b.get("matkul").toString();
textdosen.setText(dosen);
textmatkul.setText(matkul);

I've read and find out another Stackoverflow question, and almost of the answer using PutExtras()/PutExtra() from intent to save selected item from spinner, so i've tried it and getting error in my logcat

FATAL EXCEPTION: main
Process: flix.yudi.penilaian, PID: 28524
java.lang.RuntimeException: Unable to start activity ComponentInfo{flix.yudi.penilaian/flix.yudi.penilaian.Pertanyaan}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at flix.yudi.penilaian.Pertanyaan.onCreate(Pertanyaan.java:45)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 
at android.app.ActivityThread.access$900(ActivityThread.java:153) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5441) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

what should i do to fix the error?? PLEASE HELP!

yudi
  • 13
  • 1
  • 6

3 Answers3

0

Please make sure that below are not null

TextView textdosen=(TextView)findViewById(R.id.txtdos);

TextView textmatkul=(TextView)findViewById(R.id.txtmat);
Jayakrishnan
  • 4,457
  • 29
  • 29
0
TextView textdosen=(TextView)findViewById(R.id.txtdos);
TextView textmatkul=(TextView)findViewById(R.id.txtmat);

String dosen = getIntent().getStringExtra("dosen");
String matkul = getIntent().getStringExtra("matkul");

textdosen.setText(dosen);
textmatkul.setText(matkul);

That's all you need .

Hobbit
  • 601
  • 1
  • 9
  • 22
0

In the logcat there is a following line:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

which basically means you are calling a setText method on a null view. This could be caused by a number of different reasons. I think you should check out findViewByID returns null.

Community
  • 1
  • 1
Andrej Jurkin
  • 2,216
  • 10
  • 19