M
Mitch
Hi.
I am having trouble with a set of ArrayLists which when I use the
ArrayListReference.add(object); method i recieve a null pointer error.
This puzzled me because A) I thought that the point of Arraylist was
that you didn't have to worry with null pointers etc as you can simply
add items and get items and let the ArrayList object deal with the
details. B) Is the fact that I have used them ahppily already in my
program design.
I can supply code if you like but the details are
Class A
{
...
Station.addPassenger(P);
}
Class Station
{
...
public void addPassenger(Passenger P)
{
stationPassengerList.add(P)
}
}
This set up is recieving nullPointerExceptions at runtime.
Now I have done some reading around both on this group and elsewhere
and there is a major possibility I can see. I see that trying to add a
null value to the ArrayList will result in a nullPointerException In my
constructor for passenger i have a line that println's "Passenger made"
and this prints fine, however if i was to then call the method below
immeadiately after calling the constructor I get a compilation error
saying symbol cant be found
Passenger P = new Passenger(startStationReference,
stopStationReference);
System.out.Println("New passenger starts
at test " + P.getStartStation().getNameOfStation() );
startStationReference.addPassengerToStation(P);
commenting out the println lets the program compile and then gives me
the nullpointerexception at runtime.
So my querstion is along the lines of if Passenger P = new Passenger();
only creates a reference to a new passenger object (This is what i read
elsewhere and had never heard before) then how does it become an object
and what must i do to change it so that it can be referenced. I have
never had any troubles like this before.
Kind Regards, and hope i have been clear enough.
Mitch.
I am having trouble with a set of ArrayLists which when I use the
ArrayListReference.add(object); method i recieve a null pointer error.
This puzzled me because A) I thought that the point of Arraylist was
that you didn't have to worry with null pointers etc as you can simply
add items and get items and let the ArrayList object deal with the
details. B) Is the fact that I have used them ahppily already in my
program design.
I can supply code if you like but the details are
Class A
{
...
Station.addPassenger(P);
}
Class Station
{
...
public void addPassenger(Passenger P)
{
stationPassengerList.add(P)
}
}
This set up is recieving nullPointerExceptions at runtime.
Now I have done some reading around both on this group and elsewhere
and there is a major possibility I can see. I see that trying to add a
null value to the ArrayList will result in a nullPointerException In my
constructor for passenger i have a line that println's "Passenger made"
and this prints fine, however if i was to then call the method below
immeadiately after calling the constructor I get a compilation error
saying symbol cant be found
Passenger P = new Passenger(startStationReference,
stopStationReference);
System.out.Println("New passenger starts
at test " + P.getStartStation().getNameOfStation() );
startStationReference.addPassengerToStation(P);
commenting out the println lets the program compile and then gives me
the nullpointerexception at runtime.
So my querstion is along the lines of if Passenger P = new Passenger();
only creates a reference to a new passenger object (This is what i read
elsewhere and had never heard before) then how does it become an object
and what must i do to change it so that it can be referenced. I have
never had any troubles like this before.
Kind Regards, and hope i have been clear enough.
Mitch.