Question

public class Person{
private Disease currentDisease;
private String name;
private Point position;
private List<Susceptible> othersInfected;


public Person(Point position,String name){
this.position =position;
this.name = name;
List<Susceptible> othersInfected = new ArrayList<>();
}

public void addToTracing(Susceptible person){
othersInfected.add(person);
}
public List<Susceptible> getOthersInfected(){
return this.othersInfected;
}
}

I got nullpointerException error when using addToTracing method, how do I fix it

9 List<Susceptible> folks = new ArrayList<>(); 10 11 folks.add(new Person (new Point(0,0), Ann)); 12 folks.add(new Person (

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi There,

When I try to compile the person class i get the below error. This is because Person class in not extending Susceptible class.

By adding the below statement this error was resolved.

public class Person extends Susceptible

Then the above mentioned nullpointerException was observed.

The code below was used to check the issue. it was observed that othersInfected was null.

The Reson for othersInfected to be null is the following statement in constructor.

This is allocating memory to a local variable in the constuctor. changing this to the below code will allocate memory to the member variable and executes without any exceptions.

It can also be changed to the code below to get rid of the null pointer exception.

Summarizing:

1. Exception was due to the fact that memory was allocated to the local variable created in the constructor and not to the member variable of the class.

2. Solution is to allocate memory to the member variable. either by any one of the below methods.

OR

private List<Susceptible> othersInfected = new ArrayList<>();   
public Person(Point position,String name)
{
this.position =position;
this.name = name;
//othersInfected
}

OR

private List<Susceptible> othersInfected;
public Person(Point position,String name)
{
this.position =position;
this.name = name;
othersInfected = new ArrayList<>();
}

Please Comment below if you need more information.

Add a comment
Know the answer?
Add Answer to:
public class Person{ private Disease currentDisease; private String name; private Point position; private List<Susceptible> othersInfected; public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...

    import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt;...

  • What is the final value of the count field? public class Person { private String name;...

    What is the final value of the count field? public class Person { private String name; private int age; private static int count=0; public Person(String name, int age) { this.name = name; this.age age; count++; } public static void main(String[] args) { Person a - new Person("John Doe". 35): Person b new Person("Jane Doe", 38): for(int i=0;i<5;i++) Person tempa

  • Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public...

    Susceptible.java interface Susceptible { public boolean infect(Disease disease); public void forceInfection(Disease disease); public Disease getCurrentDisease(); public void setImmune(); public boolean isImmune(); public Point getPosition(); } Movable.java interface Movable { public void move(int step); } public class Point { private int xCoordinate; private int yCoordinate; /** * Creates a point at the origin (0,0) and colour set to black */ public Point(){ xCoordinate = 0; yCoordinate = 0; } /** * Creates a new point at a given coordinate and colour...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • public class Scheduler {    private List<Course> classes;    private List<Student> students;    public Scheduler() {...

    public class Scheduler {    private List<Course> classes;    private List<Student> students;    public Scheduler() {        classes = new ArrayList<Course>();        students = new ArrayList<Student>();    }       public void addCourse(Course course) {        this.classes.add(course);    }    public List<Course> getCourses() {        List<Course> newCList=new ArrayList<>();        for(int i=0;i<classes.size();i++){        newCList.add(classes.get(i));        }        return newCList;    }    public void addStudent(Student student) {        this.students.add(student);    }   ...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • public class Player { private String name; private int health; public Player(String name) { this.name =...

    public class Player { private String name; private int health; public Player(String name) { this.name = name; } } Write a complete method using java to find a Player by name in an array of Player objects. Use a linear search algorithm. The method should either return the first Player object with the requested name, or null if no player with that name is found.

  • A teacher wants to create a list of students in her class. Using the existing Student...

    A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...

  • Public class Person t publie alass EmployeeRecord ( private String firstName private String last ...

    public class Person t publie alass EmployeeRecord ( private String firstName private String last Nanei private Person employee private int employeeID publie EnmployeeRecord (Person e, int ID) publie Person (String EName, String 1Name) thia.employee e employeeID ID setName (EName, 1Name) : publie void setName (String Name, String 1Name) publie void setInfo (Person e, int ID) this.firstName- fName this.lastName 1Name this.employee e employeeID ID: publio String getFiritName) return firstName public Person getEmployee) t return employeei public String getLastName public int getIDO...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT