Here I am using three simple java classes named Person
, MainActivity
and SecondActivity
.
Person.class
String name, gender;
int age;
//setters and getters below, code not shown
MainActivity.class
Person person = new Person(); //object created
person.set_name("John Legend");
SecondActivity.class
person.set_age(20);
person.set_gender("male");
Apparently, SecondActivity.class
will cause a NullPointerException
because it is pointing to a null
object reference. How do I tell SecondActivity.class
that I am referring to the Person(John Legend)
and set the objects fields of Person
John Legend?
I spent two days looking at this problem.