0

Following is my code : I am sending the jSON object from one activity to another Activity.

// In LoginActivity
Intent intent = new Intent(LoginActivity.this,EmpLoginActivity.class);
                                    startActivity(intent);

intent.putExtra("data",jsonObject.toString());

In EmpLoginActivty.java

  try {
        JSONObject jsonObject = new JSONObject(getIntent().getStringExtra("data"));
            tv1.setText(jsonObject.getString("name"));
            tv2.setText(jsonObject.getString("type"));
            tv3.setText(jsonObject.getString("hours"));

    } catch (JSONException e) {
        e.printStackTrace();
    }

Error :

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.krish.emp/com.example.krish.emp.EmpLoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

1 Answers1

0

You need to fully populate the Intent before you pass it to startActivity.

So change this:

startActivity(intent);
intent.putExtra("data",jsonObject.toString());

to this:

intent.putExtra("data",jsonObject.toString());
startActivity(intent);
EJK
  • 11,784
  • 3
  • 34
  • 53