NullPointerException in 3D Array

Viewed 13

I am having a problem with a 3D Array in Java. The goal is to create an array in which i can add, remove and show birthdays. It mostly works, however when I try to delete a birthday I get a Nullpointerexception, and I don't know how to fix it.

I did notice, if I delete the if statement in the method option2() i don't get the error, but I don't know how else to check

The error:

   Exception in thread "main" java.lang.NullPointerException
    at main.option2(main.java:94)
    at main.main(main.java:19)
    Command execution failed.

Below is my code:

import java.util.Scanner;

public class main {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String[][][] calendar = new String[12][31][5];

    while (true) {
        startup();

        int option = Integer.valueOf(scanner.nextLine());
        if (option == 1) {
            option1(calendar);

        }
        if (option == 2) {
            option2(calendar);
            continue;
        }
        if (option == 3) {
            option3(calendar);

        }
    }

}
//                                                       ~ ~ ~ ~ ~ STARTUP TEXT
public static void startup() {
    System.out.println("Birthday Calendar 0.1");
    System.out.println("");

    System.out.println("Choose option:");
    System.out.println("1. Add birthday");
    System.out.println("2. Delete birthday");
    System.out.println("3. Show all birthdays");
}
//                                                       ~ ~ ~ ~ ~ ADD BIRTHDAY
public static String[][][] option1(String[][][] list) {
    Scanner scanner = new Scanner(System.in);
    String birthday = "";

    while (true) {

        System.out.println("\nWho's birthday is it?");
        String name = scanner.nextLine();
        System.out.println("\nIn what month?");
        int month = Integer.valueOf(scanner.nextLine());
        if (month > 12) {
            System.out.println("Month can't be higher than 12");
            continue;
        }
        System.out.println("\nOn what day?");
        int day = Integer.valueOf(scanner.nextLine());
        if (day > 31) {
            System.out.println("The day can't be higher than 31");
            continue;
        }

        birthday = month + "-" + day + " " + name;

        int x = 0;
        while (true) {
            if (list[month - 1][day - 1][x] == null) {
                list[month - 1][day - 1][x] = birthday;
                break;
            }
            x++;

            if (x == 5) {
                System.out.println("Sorry, there's no room left :(");
                break;
            } else {
                System.out.println("Birthday saved!");
            }
        }
        System.out.println("");
        break;

    }

    return list;
}
    //                                                       ~ ~ ~ ~ ~ DELETE BIRTHDAY
public static String[][][] option2(String[][][] list) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("What birthday do you want to delete?");
    String delete = scanner.nextLine();

    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 31; j++) {
            for (int x = 0; x < 5; x++) {
                if (list[i][j][x].contains(delete)) {
                    list[i][j][x] = null; 
                }
            }
        }

    }
    return list;
}
  //                                                       ~ ~ ~ ~ ~ SHOW ALL BIRTHDAYS
public static void option3(String[][][] list) {
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 31; j++) {
            for (int x = 0; x < 5; x++) {
                if (list[i][j][x] != null) {
                    System.out.println(list[i][j][x]);
                }
            }
        }
    }

}

Any help is greatly appreciated.

0 Answers
Related