Manipulating 2d Arrays in Java

So, for homework, I have to write a program that, by default, displays a table of airline seats indicated -(that is, to show that they are “open” and have the right to book). Subsequently, it is assumed that the user will offer the place that they would like to buy and change the value of that particular place in the array to X.

This is what my code looks like right now - it displays the table perfectly, 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 result that I get 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 would be greatly appreciated! Thank!

+4
3

SeetingChart - , .. String. null.

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

(.. equals) null. :

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

NullPointerException.

+1

StackOverflow!

, , , equals() , NPE, , , "null" Java. , , :

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

, , "-", SeetingChart .

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

.equals , .

I would recommend initializing all elements SeetingChartto "-".

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

Example:

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

All Articles