|
-
Jul 7th, 2001, 08:27 AM
#1
Thread Starter
Addicted Member
Urgent Help Required!!!
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.
Code:
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
Code:
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.
-
Jul 7th, 2001, 02:08 PM
#2
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))
-
Jul 7th, 2001, 02:40 PM
#3
Thread Starter
Addicted Member
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!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|