Note that there are some explanatory texts on larger screens.

plurals
  1. POUnkown NullPointerException
    text
    copied!<p>I'm writing a simple class to simulate an airplane reservation system. Unfortunately, I keep having my plan-to-hog-all-first-class-seats glee interrupted by multiple null pointer exceptions, and I can't find them anywhere. Here is my completed work:</p> <pre><code>import java.util.*; public class airplane { boolean[] seats; Scanner scan = new Scanner(System.in); public airplane(int num_of_seats) { boolean[] seats = new boolean[num_of_seats]; for (int counter = 0; counter &lt; num_of_seats; counter++) { seats[counter] = true; } } public boolean seatFirstClass(int seat) { if ( seat &lt;= seats.length / 4 ) return true; else return false; } public void seatsReserveFirstClass() { for (int seat = 0; seat &lt; seats.length; seat++) { if (seats[seat] == true &amp;&amp; seat &lt;= seats.length / 4) { seats[seat] = false; System.out.print("I have reserved one seat in first class. Please tell the passenger to enjoy their flight with these airlines! "); break; } } System.out.print("I could not find a seat in first class. Can the passenger switch to economy? (0 for no, 1 for yes) "); int choice = scan.nextInt(); if (choice == 0) System.out.print("I could not add passenger due to lack of a seat."); else if (choice == 1) { this.seatsReserveEconomy(); } } public void seatsReserveEconomy() { for (int seat = 0; seat &lt; seats.length; seat++) { if (seats[seat] == true &amp;&amp; seat &gt; seats.length / 4) { seats[seat] = false; System.out.print("I have reserved a seat in economy. Please tell the passenger to enjoy their flight with these airlines! "); break; } } System.out.print("I could not find a seat in economy. Can the passenger switch to first class? (0 for no, 1 for yes) "); int choice = scan.nextInt(); if (choice == 0) System.out.print("I could not add the passenger due to a lack of seats."); else if (choice == 1) this.seatsReserveFirstClass(); } public void planeClear() { for (int seat = 0; seat &lt; seats.length; seat++) { seats[seat] = true; } } public void seatUnReserve(int seat) { if (seat &lt; seats.length) { if (seats[seat] == false) { seats[seat] = true; System.out.printf("Seat %d has been unreserved.\n", seat); } else { System.out.print("The seat you have entered is already unreserved.\n"); } } else { System.out.printf("There is no seat number %d on this plane.\n", seat); } } } </code></pre> <p>and here is a main class to test it:</p> <pre><code>public class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); boolean check = false; System.out.print("How many seats does the plane have? "); int num_of_seats = scan.nextInt(); airplane Airplane = new airplane(num_of_seats); while (check == false) { System.out.print("Please enter 0 to reserve a first class seat, 1 to reserve an economy seat,\n2 to cancel a reservation, 3 to clear the plane,\nand 4 to quit the program. "); int choice = scan.nextInt(); switch(choice) { case 0: Airplane.seatsReserveFirstClass(); break; case 1: Airplane.seatsReserveEconomy(); break; case 2: System.out.print("Which seat should I unreserve? "); int seat = scan.nextInt(); Airplane.seatUnReserve(seat); break; case 3: Airplane.planeClear(); break; case 4: check = true; break; } } } } </code></pre> <p>If this is run, the dreaded NullPointException error shows up, and I'm not sure what calls this beast.</p> <pre><code>Exception in thread "main" java.lang.NullPointerException at airplane.seatsReserveFirstClass(airplane.java:22) at Test.main(Test.java:15) </code></pre>
 

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