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.
Here is the Airplane classCode: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 } //-------------------------------------------------------------------------- }
If your require further explanation please ask.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; } } //------------------------------------------------------------------------------




Reply With Quote