PDA

Click to See Complete Forum and Search --> : Urgent Help Required!!!


Ramandeep
Jul 7th, 2001, 08:27 AM
Hi bellow is most of a class I coded, I'm having problems retrieving the planeIdNo from the object array list. I get a NullPointerError. I can access the list in the addPlane method and retrieve the planeIdNo or planeSize but not in findPlane why is this am I doing somthing wrong, I'm new to OOP.


public class RegisteredList
{
Airplane list[];
int total = 0;

//--------------------------------------------------------------------------

public RegisteredList(int maxPlanes)
{
list = new Airplane[maxPlanes];
}

//--------------------------------------------------------------------------

public int addPlane(Airplane planeToAdd) throws IOException
{
if (isFull() == true)
{
return -1;
}

if (isInList(planeToAdd.getIdNo()) == true)
{
return 0;
}

list[total] = planeToAdd;
total++;

return 1;
}

//--------------------------------------------------------------------------

public Airplane findPlane(String planeId) throws IOException
{
for (int i = 0; i < list.length; i++)
{
try
{
if (list[i].getIdNo() == planeId)
{
return list[i]; // plane found
}
}

catch( NullPointerException npe)
{
// do nothing
}
}

return null; // plane not found
}

//--------------------------------------------------------------------------

public boolean isInList(String planeId) throws IOException
{
if (findPlane(planeId) != null)
{
return true; // plane is in list
}

return false; // plane is not in list
}

//--------------------------------------------------------------------------
}


Here is the Airplane class


public class Airplane
{
String idNo;
int size;

//--------------------------------------------------------------------------

public Airplane(String planeId, int planeSize)
{
idNo = planeId;
size = planeSize;
}

//--------------------------------------------------------------------------

public String getIdNo()
{
return idNo;
}

//--------------------------------------------------------------------------

public int getSize()
{
return size;
}
}

//------------------------------------------------------------------------------


If your require further explanation please ask.

VirtuallyVB
Jul 7th, 2001, 02:08 PM
Just scanning your code, I didn't see each/any element of list[] being initialized with
list[anIndex] = new Airplane(aPlaneId, aPlaneSize);
If you don't, list[anIndex] will be null although list.length is valid.

Also, instead of
if (list[i].getIdNo() == planeId)
you'll want to use
if (list[i].getIdNo().equals(planeId))

Ramandeep
Jul 7th, 2001, 02:40 PM
VirtuallyVB thanks for your help really appreciate it!!!

I did however have the list[anIndex] = new Airplane("", 0); in the constructor before and can't rember why I took it out, any way I added that in there again and added/changed code here and ther and managed to get it working, I think one of the main thing was that I == instead of the equals() function, thanks again!!!