Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy do I get a NullPointerException when using text input as opposed to user input?
    primarykey
    data
    text
    <p>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 <code>NullPointerException</code>. The part of the code im getting the <code>NullPointerException</code> is </p> <pre><code> 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); } } } </code></pre> <p>When doing normal input, it runs like this:</p> <pre><code>C:\Users\Owner\Desktop\Math&amp;CIS\CS151\HW1&gt;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 </code></pre> <p>But when when using a .txt input, the following happens </p> <pre><code>C:\Users\Owner\Desktop\Math&amp;CIS\CS151\HW1&gt;java UserInterface planes &lt; 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&amp;CIS\CS151\HW1&gt; </code></pre> <p>the exception happens at the line "<code>if (className.equals("")) {</code>"</p> <p>the input text is the follwing:</p> <pre><code>First WAAW 2 Economy WCAACW 3 ... </code></pre> <p>I'm not sure why it doesn't work when using an input text.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload