So I have to write a program that is a flight reservation system. If I run the program normally and enter in values when prompted, nothing goes wrong, but when I use a .txt file input, I get a NullPointerException. The part of the code im getting the NullPointerException is
public void instantiateAirplane() throws IOException{
boolean creatingAirplane = true;
String className= "";
String seatFormat;
int rowNums;
while (creatingAirplane){
input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter service class name or [ENTER] to finish: ");
className = input.readLine();
if (className.equals("")) {
creatingAirplane = false;
}
else{
System.out.println("Enter seating pattern: ");
seatFormat = input.readLine();
System.out.println("Enter number of rows: ");
rowNums = Integer.parseInt(input.readLine());
plane.creatSeatingChart(className, rowNums, seatFormat);
}
}
}
When doing normal input, it runs like this:
C:\Users\Owner\Desktop\Math&CIS\CS151\HW1>java UserInterface planes
Enter service class name or [ENTER] to finish:
First Class
Enter seating pattern:
WAAW
Enter number of rows:
2
Enter service class name or [ENTER] to finish:
Business Class
Enter seating pattern:
WAAW
Enter number of rows:
2
Enter service class name or [ENTER] to finish:
Add [P]assenger, Add [G]roup, [C]ancel Reservations, Print [S]eating chart, Prin
t [M]anifest, [Q]uit
But when when using a .txt input, the following happens
C:\Users\Owner\Desktop\Math&CIS\CS151\HW1>java UserInterface planes < input.txt
Enter service class name or [ENTER] to finish:
Enter seating pattern:
Enter number of rows:
Enter service class name or [ENTER] to finish:
Exception in thread "main" java.lang.NullPointerException
at UserInterface.instantiateAirplane(UserInterface.java:21)
at UserInterface.main(UserInterface.java:123)
C:\Users\Owner\Desktop\Math&CIS\CS151\HW1>
the exception happens at the line “if (className.equals("")) {“
the input text is the follwing:
First
WAAW
2
Economy
WCAACW
3
...
I’m not sure why it doesn’t work when using an input text.
That readLine() method returns a null once the iterator reaches what is known as EOF or the end of file. Check to see if
ClassName == null || className.Equal("")...Also you may want to declare
input = new BufferedReader(new InputStreamReader(System.in));out side of your loop just so you are not re-initializing the reader at each loop iteration.-!