Please help with search function

Heya, im currently working on code that will allow me to add, view, search and remove prisoners and wardens from a prison system. So far i have managed to allow the user to add and view. Im a bit stuck on the search part of the code, its a bit temperamental :S, sometimes it will find and display the data, but other times it will not and comes up with the error

Exception in thread "main" java.lang.NullPointerException

at Prison.getNum(Prison.java:128)

at TestPrison.main(TestPrison.java:101)

Below i have put my Prison and TestPrison Class files and highlighted the lines the compiler said had the error, any help would be really appreciated because i really cant work out wats going on.

Prison

public class Prison

{

private Prisoner [] thePrisoner;

private Warden [] theWarden;

private HighPrisoner [] theHighPrisoner;

private int maxP;

private int maxPrisoners;

private int maxW;

private int maxWardens;

private int maxH;

private int maxHighPrisoners;

public String prisonName;

public Prison (String prison, int maxP, int maxW, int maxH)

{

prisonName = prison;

maxPrisoners = maxP;

maxP = 0;

thePrisoner = new Prisoner [maxPrisoners];

maxWardens = maxW;

maxW = 0;

theWarden = new Warden [maxWardens];

maxHighPrisoners = maxH;

maxH = 0;

theHighPrisoner = new HighPrisoner [maxHighPrisoners];

}

public boolean addPrisoner( Prisoner newPrisoner )

{

if ( maxP == maxPrisoners )

{

System.out.println("Prisons full");

return false;

}

else

{

System.out.println("Prisoner now in Prison");

thePrisoner[maxP] = newPrisoner;

maxP++;

return true;

}

}

public boolean addHighPrisoner( HighPrisoner newHighPrisoner )

{

if ( maxH == maxHighPrisoners )

{

System.out.println("Prisons full");

return false;

}

else

{

System.out.println("High Level Prisoner now in Prison");

theHighPrisoner[maxH] = newHighPrisoner;

maxH++;

return true;

}

}

public boolean addWarden( Warden newWarden )

{

if ( maxW == maxWardens )

{

System.out.println("We Have Anough Wardens");

return false;

}

else

{

System.out.println("Warden now working at Prison");

theWarden[maxW] = newWarden;

maxW++;

return true;

}

}

public void outputAll()

{

System.out.println("School Name: " + prisonName );

System.out.println("");

outputWardens();

System.out.println("");

outputPrisoners();

System.out.println("");

outputHighPrisoners();

System.out.println("");

}

public void outputWardens()

{

System.out.println("The Wardens:");

for( int i=0; i < maxW; ++i)

{

System.out.println(theWarden);

}

}

public void outputPrisoners()

{

System.out.println("The Low Level Prisoners:");

for( int i=0; i < maxP; ++i)

{

System.out.println(thePrisoner);

}

}

public void outputHighPrisoners()

{

System.out.println("The High Level Prisoners:");

for( int i=0; i < maxH; ++i)

{

System.out.println(theHighPrisoner);

}

}

public void getNum(String num)

{

System.out.println("");

System.out.println("Search for Prisoner ID:");

System.out.println("");

for(Prisoner nextPrisoner : thePrisoner)

{

--> 128if (nextPrisoner.getNum() == num)

{

System.out.println(nextPrisoner);

}

else

{

System.out.println("Sorry no Prisoner ID found");

}

}

for(HighPrisoner nextHighPrisoner : theHighPrisoner)

{

if (nextHighPrisoner.getNum() == num)

{

System.out.println(nextHighPrisoner);

}

else

{

System.out.println("Sorry No Prisoner ID found");

}

}

System.out.println("");

}

}

TestPrison

import java.util.*;

public class TestPrison

{

public static void main(String []args)

{

String sN;

int sS;

String sA;

String tN;

int tS;

int input;

String aN;

int aS;

String aA;

int aL;

String aC;

String pN;

String rN;

Scanner kybd = new Scanner(System.in);

Prison prison0 = new Prison("Alcatraz",200,200,100);

do {

menu();

System.out.println("Enter Option: ");

input = kybd.nextInt();

switch(input) {

case 1:

System.out.println("Enter Name: ");

tN = kybd.next();

System.out.println("Enter Rank: ");

tS = kybd.nextInt();

Warden wardenObject = new Warden(tN,tS);

prison0.addWarden(wardenObject);

System.out.println("\nWarden "+tN+" Added!");

break;

case 2:

System.out.println("Enter Prisoner Security Level: ");

aL = kybd.nextInt();

if (aL > 3)

{

System.out.println("Enter Name: ");

aN = kybd.next();

System.out.println("Enter Remaining Jail Time: ");

aS = kybd.nextInt();

System.out.println("Enter Prisoner ID: ");

aA = kybd.next();

System.out.println("Can Prisoner Share Cell? ");

aC = kybd.next();

HighPrisoner highprisonerObject = new HighPrisoner(aN,aS,aA,aL,aC);

prison0.addHighPrisoner(highprisonerObject);

System.out.println("\nPrisoner " + aA + " Added!");

break;

}

else

{

System.out.println("Enter Name: ");

sN = kybd.next();

System.out.println("Enter Remaining Jail Time: ");

sS = kybd.nextInt();

System.out.println("Enter Prisoner ID: ");

sA = kybd.next();

Prisoner prisonerObject = new Prisoner(sN,sS,sA);

prison0.addPrisoner(prisonerObject);

System.out.println("\nPrisoner " + sA + " Added!");

break;

}

case 3:

prison0.outputAll();

break;

case 4:

prison0.outputWardens();

break;

case 5:

prison0.outputPrisoners();

break;

case 6:

prison0.outputHighPrisoners();

break;

case 7:

prison0.outputPrisoners();

prison0.outputHighPrisoners();

break;

case 8:

System.out.println("Enter Prisoner ID: ");

pN = kybd.next();

> 101prison0.getNum(pN);

break;

default:

System.out.println("Error Selection Does Not Exist");

break;

}

} while(input >= 0);

}

public static void menu() {

System.out.println("");

System.out.println("MAIN MENU");

System.out.println("*********\n");

System.out.println("1. Add Warden\n");

System.out.println("2. Add Prisoner\n");

System.out.println("3. View All\n");

System.out.println("4. View Wardens\n");

System.out.println("5. View Low Level Prisoners\n");

System.out.println("6. View High Level Prisoners\n");

System.out.println("7. View All Prisoners\n");

System.out.println("8. Search Prisoner ID\n\n");

}

}

Once again any help would really be appreciated : )

[7417 byte] By [TNUR-Wamprata] at [2007-11-27 1:11:17]
# 1

The nextPrisoner is null, and you can't call getNum() on a null reference.

Try replacing that line with:if (nextPrisoner != null && nextPrisoner.getNum() == num)

Oh, next time you're posting code, please use code tags:

http://forum.java.sun.com/help.jspa?sec=formatting

prometheuzza at 2007-7-11 23:46:38 > top of Java-index,Java Essentials,New To Java...