4

So for homework I have to write a program that prints out a table of airline seats denoted with - by default (i.e. to show that they are "open" and eligible for booking). Subsequently, it is supposed to prompt the user for a seat they would like to purchase & change the value of that particular spot in the array to a X.

This is what my code looks like right now - it prints out the table just fine, but when I try to change the location it gives me an error:

import java.util.*;

public class AirlineSeeting {

    public static void main(String[]args) {
        int row = 0;
        int colum = 0;
        System.out.println("Enter n:");
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        String[][] SeetingChart = new String[n][4];

        for (int i = 1; i <= 4; i++) {
            System.out.printf("\t%d\t", i);
        }

        System.out.println("");

        for (int j = 1; j <= n; j++) {
            System.out.printf("%d", j);
            for (int k = 1; k <= 4; k++) {
                System.out.print("\t-\t");
            }
            System.out.println("");
        } 
        for( int i = 0 ; i < SeetingChart.length; i++){
            System.out.println("What row would you like to sit in");
            row = scanner.nextInt();
            System.out.println("What colum would you like to sit in");
            colum = scanner.nextInt();
            if (SeetingChart[row][colum].equals("X")){
                System.out.println("Please pick a seat that is avalable");
            }
            else if (SeetingChart[row][colum].equals("-")){
                SeetingChart[row][colum] = "X";
            }
            for (int j = 1; j <= 4; j++) {
                System.out.printf("\t%d", j);
            }

            System.out.println("");

            for (int j = 1; j <= n; j++) {
                System.out.printf("%d", j);
                for (int k = 1; k <= 4; k++) {
                    System.out.print("\t-");
                }
                System.out.println("");
            } 
        }
    } 
}

This is the output I am getting when executing the above code:

Enter n:
9
    1       2       3       4   
1   -       -       -       -   
2   -       -       -       -   
3   -       -       -       -   
4   -       -       -       -   
5   -       -       -       -   
6   -       -       -       -   
7   -       -       -       -   
8   -       -       -       -   
9   -       -       -       -   
What row would you like to sit in
2
What colum would you like to sit in
2
Exception in thread "main" java.lang.NullPointerException
    at AirlineSeeting.main(AirlineSeeting.java:30)

Any help with this will be greatly appreciated! Thank you!

3
  • what error does it give you? Commented Apr 15, 2015 at 18:51
  • 1
    "[...] it gives me an error." -> Wich error? Commented Apr 15, 2015 at 18:51
  • @TravisWittmann when your questions were answered please mark them as answered. Commented Apr 15, 2015 at 19:17

3 Answers 3

1

SeetingChart is an array of Objects, i.e. String. Therefore, all entries are initialized with null. Your program crashes at

            if (SeetingChart[row][colum].equals("X")){

because you try to call a method (i.e. equals) on a null. This is the reason, why you should switch the statement around:

            if ("X".equals(SeetingChart[row][colum])){

This prevents the NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

0

Welcome to StackOverflow!

Your problem is that you never initialise the contents of the array, so when you try to call equals() on the element an NPE is thrown because that is what happens if 'null' is manipulated in Java. When you check if the seat is occupied, add a null check like so:

if (SeetingChart[row][colum] != null && SeetingChart[row][colum].equals("X")){

Comments

0

The problem is you never initialize your array to have "-" in them so every element of array SeetingChart is initialized to null.

if (SeetingChart[row][colum].equals("X")){

You are trying to invoke .equals on a null reference which isn't possible.

I would recommend initializing all elements of SeetingChart to "-".

You can use Arrays.fill(SeetingChart[i], "-"); to fill your array taken from here

Example:

for(int i = 0; i < test.length; i++)
{
    Arrays.fill(SeetingChart[i], "-");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.