-2

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.

Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
Principiante
  • 131
  • 1
  • 2
  • 8
  • 1
    Create method in SecondAcitivity and pass Person object as parameter. Also folow naming convention for setter getter of the vairables – Kick Sep 19 '17 at 08:38
  • all three are differnt class. either you should pass the object `person` as an arguments to the other class method. **OR** there is another way to inject objects using Spring framework. :) Thank You – Vikrant Kashyap Sep 19 '17 at 08:39
  • So how are you trying to reference `person` in your `SecondActivity`? You need to implement `Parcelable` interface and pass your pojo through an `Intent` - any advice to use `static` is a bad idea. – Mark Sep 19 '17 at 08:41
  • For your reference you can check [here.](https://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android?rq=1) – Procrastinator Sep 19 '17 at 08:41
  • I imagine that John Legend parameter is a String, because it doesn't have double quotes in the code – Albert Lazaro de Lara Sep 19 '17 at 08:47
  • 1
    @albert, my mistake. A newbie's typo error. Thanks for pointing out. – Principiante Sep 19 '17 at 08:52

2 Answers2

0

You have instantiated a new Person object with variable name person in MainActivity, but not in the SecondActivity. Therefore it is null in SecondActivity.

If you want to get the name of the person in the MainActivity but set the age and gender in your SecondActivity maybe you could pass the name string with an intent into the SecondActivity and add it when you create a Person object there. Read about intents here.

P Fuster
  • 2,224
  • 1
  • 20
  • 30
-2

You should have to create a constructor in SecondActivity class and pass the person object by parameter:

public class SecondActivity extends Activity {

  Person person;

  public SecondActivity(Person person){
    this.person = person;
  }

}

And after pass the person:

Person person = new Person(); //object created
person.set_name("John Legend");
SecondActivity sActivity = new SecondActivity(person);

Also, before you use person, you can check that is not null:

if (person != null) {
   // code ...
}
Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
  • how to retrieve Person object that is set in SecondAcitivity, it shoud return Person object – Kick Sep 19 '17 at 08:47
  • you can make a getPerson function that return person to retrieve the person in the secondActivity. Look for getter/setters information – Albert Lazaro de Lara Sep 19 '17 at 08:48
  • In android I think there are intent methods to pass variables, it should better to use them. This is a related question: https://stackoverflow.com/questions/2906925/how-do-i-pass-an-object-from-one-activity-to-another-on-android – Albert Lazaro de Lara Sep 19 '17 at 08:51
  • Firstly I think you should establish if `SecondActivity` extends `Activity` and if a content view is associated. If so you should never instantiate a `Activity` class -the Framework should do this. – Mark Sep 19 '17 at 08:54