Hi there i am working on LinkedList project with Java and i have some unclear issues in my mind. for example this is my “Patient” class;
public class Patient {
private int id;
private String name;
private String lastName;
private String doctor;
private Patient next;
private Patient prev;
public Patient(int id, String name, String lastName, String doctor, Patient next, Patient prev){
this.id = id;
this.name = name;
this.lastName = lastName;
this.doctor = doctor;
this.next = next;
this.prev = prev;
}
and when i construct my LinkedList i create a header and tail node like this.
private Patient header = new Patient(0, null, null, null, null, null);
private Patient tail = new Patient(0, null, null ,null ,null, null);
but if i create these two nodes without new Patient(0, null, null, null, null, null); anything doesnt change. could you please explain why_?
You get null references when not using the new keyword. Have you tried to access and do something with Objects that aren’t instanced with the new key word?
Add a method called getName() and call it on a Patient variable before it is instanced. This will result in an “null pointer exception”