-1

I want to make an app that gets user input from the first activity and sets these values to a class and shows them in the other activity.

The error I have is null pointer exception when i try to get the values in the second class. I don't understand what I do wrong.

This is the first activity:

public class MainActivity extends Activity {

EditText name, phone, type;
Button addBtn;
public final static String SER_KEY = "com.easyinfogeek.objectPass.ser";
public final static String Tag = "Yes";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    addBtn = findViewById(R.id.button);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           SerializeMethod();
        }
    });
}

public void SerializeMethod(){

    name = findViewById(R.id.name);
    phone = findViewById(R.id.phone);
    type = findViewById(R.id.type);

    PhoneBookEntry book = new PhoneBookEntry();
    book.setName(name.toString());
    book.setPhone(phone.toString());
    book.setType(type.toString());

    Intent intent = new Intent(this, SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable(SER_KEY, book);
     startActivity(intent);
 }
}

Second activity:

public class SecondActivity extends Activity {

final public static String Tag = "Yes";
TextView txt1, txt2, txt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    txt1 = findViewById(R.id.textView1);
    txt2 = findViewById(R.id.textView2);
    txt3 = findViewById(R.id.textView3);


    PhoneBookEntry book = (PhoneBookEntry) getIntent().getSerializableExtra(MainActivity.SER_KEY);


    txt1.setText(book.getName());
    txt2.setText(book.getPhone());
    txt3.setText(book.getType());


  }
}

and this is the PhoneBookEntry class:

public class PhoneBookEntry implements Serializable {
 private static final long serialVersionUID = -7060210544600464481L;
 private String name;
 private String phone;
 private String type;

--- getters and setters --- 
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
M.P.
  • 21
  • 1
  • 5
  • Another duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe May 19 '18 at 15:44

2 Answers2

0

You just forgot to add Bundle in your Intent check you code again

Just add your Bundle in Intent using intent.putExtras(mBundle); it will work fine

Try this

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(SER_KEY, book);
intent.putExtras(mBundle);
startActivity(intent);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

Use Parcelable instead of Serializable. Follow the example ...

public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 } 

here is the full example.