0

I have made the code and there's no error, but logcat shows a NullPointerException. This is my code to pass and to catch the value from each activity. Please help me solve this.

First activity:

try {
    Intent in = new Intent();
    in.setClass(p_daftar_pelanggan.this, ubah_data_pelanggan.class);
    in.putExtra("id", eid.getText().toString());
    in.putExtra("nama", tnama.getText().toString());
    in.putExtra("alamat", ealamat.getText().toString());
    in.putExtra("no_hp", ehp.getText().toString());
    startActivity(in);
}
catch(NullPointerException ex)
{
}

Second activity:

Bundle b = getIntent().getExtras();
String idpgu = b.getString("id");
String namapgu = b.getString("nama");
String alamatpgu = b.getString("alamat");
String hppgu = b.getString("no_hp");

idp.setText(idpgu);
idp.setKeyListener(null);

nama.setText(namapgu);
ealamat.setText(alamatpgu);
hp.setText(hppgu);
tynn
  • 38,113
  • 8
  • 108
  • 143
YRO
  • 29
  • 4
  • 2
    You would need to post the stacktrace. You also shouldn't leave `catch` blocks empty – codeMagic Sep 01 '15 at 20:29
  • @YRO probably you need validate that the values that you are receiving aren´t null, see my answer. Also i recommed post your messages displayed in LogCat, very important to determinate the source of this exception. – Jorgesys Sep 01 '15 at 21:38
  • @Elenasys oke, i'll try. – YRO Sep 01 '15 at 21:45
  • @DhawalSodhaParmar yep, i know its duplicate but i still have no answer from that's page – YRO Sep 01 '15 at 21:46
  • @codeMagic thanks, i forgot to write down the catch statement – YRO Sep 01 '15 at 21:47

4 Answers4

0

Try the following in the second activity:

Intent intent = getIntent();
String idpgu = intent.getStringExtra("id");
user3728064
  • 158
  • 1
  • 11
0

You put extras using Intent in FirstActivity But Use Bundle to receive it, that is why you have NullPointerException because SecondActivity is looking for values in different place. Do the following in your secondActvity

Intent intent = getIntent(); 
String idpgu = intent.getStringExtra("id");
String namapgu = intent.getStringExtra("nama");
String alamatpgu = intent.getStringExtra("alamat");
String hppgu = intent.getStringExtra("no_hp");
// Check Null for each Values e.g. 
  if (idpgu.trim().equals(" ")) 
{ idpgu = "null"; 
} 
Want2bExpert
  • 527
  • 4
  • 11
  • is getString can be use to catch value ? i think is not – YRO Sep 01 '15 at 21:06
  • See my edit answer. Besides what is the work of this in.setClass(p_daftar_pelanggan.this, ubah_data_pelanggan.class);? Remove it, clean, build and run the app again – Want2bExpert Sep 01 '15 at 21:17
  • setClass makes move to second activity. your suggestion is still not working – YRO Sep 01 '15 at 21:30
  • You seem to be passing null value(s) into the putExtra. Try this, in the secondActivity, use if to check if any of the value is null. E.g. if (idpgu.equals("") || idpgu == null) idpgu = "null"; Let me know if it work or want me to update my answer. – Want2bExpert Sep 01 '15 at 21:36
  • Do Null check in Both Activities – Want2bExpert Sep 01 '15 at 21:55
0

You can use Bundle for this purpose:

Activity destActivity = new Activty();
Bundle b = new Bundle();
destActivity.putString("TAG_ForValue",YourValue.getText().toString());
destActivity.setArguments(b);

Intent intent = new Intent(startActivity.this, destActivity.class);
startActivity(intent);

And in your destActivity :

Bundle bundle = getArguments();

if(bundle !=null)
   String value =  bundle.getString("TAG_ForValue");
hdiz
  • 1,141
  • 2
  • 13
  • 27
0

You are sending and receiving the values correctly, but probably some values are null, you can validate setting an empty string instead of a null value:

   Bundle b = getIntent().getExtras();
   String idpgu = (b.getString("id") != null)? b.getString("id") : "";
   String namapgu = (b.getString("nama") != null)? b.getString("nama") : "";
   String alamatpgu = (b.getString("alamat") != null)? b.getString("alamat") : "";
   String hppgu = (b.getString("no_hp") != null)? b.getString("no_hp") : "";
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • ok post your message displayed in LogCat, probably some of your TextViews is null – Jorgesys Sep 01 '15 at 21:44
  • 1
    thanks for your help Elen. i have solved my problem. it's my own silly mistake, i called the wrong xml file. hiks, poor me. – YRO Sep 04 '15 at 04:54