-1

Error in logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.example.sagar.project1.profile.onCreate(profile.java:16)

Here is my profile.java:

  Bundle extras = getIntent().getExtras();

    Line 16: String name = extras.getString("name");
    String email = extras.getString("email");
    String pass = extras.getString("pass");
    String contact = extras.getString("contact");

    TextView textView1 = (TextView) findViewById(R.id.txv1);
    if(extras != null){
        textView1.setText("Name: " +name);
    }
    TextView textView2 = (TextView) findViewById(R.id.tv2);
    if (extras != null){
        textView2.setText("Email: " +email);
    }
    TextView textView3 = (TextView) findViewById(R.id.tv3);
    if (extras != null){
        textView3.setText("Password: " +pass);
    }
    TextView textView4 = (TextView) findViewById(R.id.tv4);
    if (extras != null){
        textView4.setText("Pnone : " +contact);
    }
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64

2 Answers2

0

To pass data between Activities you use putExtra in the activity that gonna launch the target activity and get the extras in the target activity. I show you an simple example working.

MainActivity

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this,TargetActivity.class);
        intent.putExtra(TargetActivity.PUT_EXTRA_NAME, "My user name");
        intent.putExtra(TargetActivity.PUT_EXTRA_EMAIL, "My email");
        intent.putExtra(TargetActivity.PUT_EXTRA_PASS, "122345");
        intent.putExtra(TargetActivity.PUT_EXTRA_CONTACT, "My contact ");

        startActivity(intent);

    }
}

TargetActivity

public class TargetActivity extends AppCompatActivity {

    public static final String PUT_EXTRA_NAME = "name";
    public static final String PUT_EXTRA_EMAIL = "email";
    public static final String PUT_EXTRA_PASS = "pass";
    public static final String PUT_EXTRA_CONTACT = "contact";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_target);

        Bundle extras = getIntent().getExtras();
        if(extras != null){
            String name = extras.getString(PUT_EXTRA_NAME);
            String email = extras.getString(PUT_EXTRA_EMAIL);
            String pass = extras.getString(PUT_EXTRA_PASS);
            String contact = extras.getString(PUT_EXTRA_CONTACT);

        }
    }
}

I hope this helps you.

Juanjo Berenguer
  • 789
  • 1
  • 5
  • 8
0

Make sure the bundle (extras) is not null.

String name = "";
String email = "";
String pass = "";
String contact = "";

Bundle extras = getIntent().getExtras();

if(extras != null){ 

  name = extras.getString("name");
  email = extras.getString("email");
  pass = extras.getString("pass");
  contact = extras.getString("contact");

 }
Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37