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

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.
public class Person{ private Disease currentDisease; private String name; private Point position; private List<Susceptible> othersInfected; 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; 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 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...
I need help in converting this into pseudo-code Student.java public class Student implements Comparable<Student>{ private String name, major, status; private int rank; public Student() { this.name = this.major = this.status = ""; this.rank = 0; } public Student(String name, String major, String status) { this.name = name; this.major = major; this.status = status; } public String getName() { return name; } public String getMajor() { return major; } public String getStatus() { return status; } public int...
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;
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() {
...
This is my playlist class: class Playlist{ private String name; private int numberOfRecordings = 0; private int durationInSeconds = 0; private int MAX_PLAYLIST_SIZE; Recording recordingslist[]; Playlist(){ name = "Unknown"; MAX_PLAYLIST_SIZE = 5; recordingslist = new Recording[MAX_PLAYLIST_SIZE]; durationInSeconds = 0; } Playlist(String name, int size){ this.name = name; this.MAX_PLAYLIST_SIZE = size; recordingslist = new Recording[MAX_PLAYLIST_SIZE]; ...
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; 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 = 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.