0

I am trying to send data from one activity to another activity using POJO class object. but if I call the get() methods the app doesn't run without the get methods the app runs package com.example.user.bankemployee;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Employee implements Serializable{
    private String employeeName;
    private String employeeId;
    private String age;
    private String gender;
    private String phone;
    private String email;
    private String city;
    private List<String>skill=new ArrayList<>();

    public Employee() {
    }

    public Employee(String employeeName, String employeeId, String age) {
        this.employeeName = employeeName;
        this.employeeId = employeeId;
        this.age = age;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public String getEmployeeId() {
        return employeeId;
    }

    public String getAge() {
        return age;
    }

    public List<String> getSkill() {
        return skill;
    }

    public void setSkill(List<String> skill) {
        this.skill = skill;
    }

    public String getGender() {
        return gender;
    }
    public void Next(View view) {

this is the intent code

    Employee employee=new Employee(empName,empId,age);

    Intent intent=new Intent(this,Step2.class);
    intent.putExtra("step1",employee);

    startActivity(intent);
}

and this is the main activity

public class EmployeeList extends AppCompatActivity {
String empName,empId,age,Gender,phone,email,city;
List<String> skill=new ArrayList<>();
TextView tb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_employee_list);
    tb=findViewById(R.id.TB);
    Employee employee=(Employee) getIntent().getSerializableExtra("step1");
    empName=employee.getEmployeeName();
    empId=employee.getEmployeeId();
    age=employee.getAge();

if i do this it doesnt show any error other wise it the app keeps stopping

public class EmployeeList extends AppCompatActivity {
    String empName,empId,age,Gender,phone,email,city;
    List<String> skill=new ArrayList<>();
    TextView tb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee_list);
        tb=findViewById(R.id.TB);
        Employee employee=(Employee) 
         getIntent().getSerializableExtra("step1");
        //empName=employee.getEmployeeName();
        //empId=employee.getEmployeeId();
        //age=employee.getAge();
rakib5175
  • 3
  • 1
  • The `Intent` you're attaching that `Employee` extra to is used to start `Step2`, but the `Activity` you've shown that is trying to retrieve that extra is `EmployeeList`. Are you sure the `Intent` starting `EmployeeList` has that extra on it? – Mike M. Aug 12 '18 at 11:12
  • yes here i forgot to edit this.this will be EmployeeList.class but still there is an error – rakib5175 Aug 12 '18 at 11:18

1 Answers1

0

Instead of putting the "employee" object directly on the intent, try creating a Bundle and adding the employee to that bundle. Afterwards, add the bundle as extra to the Intent. When you start your activity, recover the bundle and get the employee. Try that way.

Also, as Mike M. said, make sure that you recover the Intent in the proper Activity.

  • I assume your method "Next" is used as an onClickListener method or something like that. I guess is not a good idea to locate this method inside the "Employee" (Serializable) class, so I would try moving it to the EmployeeList Activity, and so creating the new Employee and launching the new intent from that Activity. I don't know if I'm properly understanding your program but that's what I would do. – Miguel Ortiz Aug 12 '18 at 12:41
  • this next method is an onClickListener method and it is inside the activity that I am Intenting EmployeeList from. it is not in the Employee (Serializable) class – rakib5175 Aug 12 '18 at 13:03
  • Okey sorry mobile version of stack overflow showed it weird. Try with intent.putExtras(bundle) and bundle = intent.getExtras() instead of that "putExtraBundle". Here I found a link where they use an example: https://stackoverflow.com/questions/14333449/passing-data-through-intent-using-serializable – Miguel Ortiz Aug 12 '18 at 14:20
  • I solved it, actually it was a null pointer Exception.thank you – rakib5175 Aug 13 '18 at 15:09