空计数器计数器变量上的异常

时间:2018-10-21 03:16:23

标签: java eclipse pointers nullpointerexception

我不明白为什么我会不断收到这个空指针异常,我对引用和指针等等一无所知,因此才开始学习类和对象。非常感谢帮助!这是有问题的代码:

public class Library {

    public static MediaItem[] item = new MediaItem[100];

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int choice = 0;
        int numberOfItems = 0;

        System.out.println("Personal Lending Library!");

        while (choice !=6) {

            choice = menu(input);

            switch(choice) {
            case 1: addNewItem(numberOfItems, input);
                break;
            case 2:
                break;
            case 3: listItems(numberOfItems);
                break;
            case 4:
                break;
            }

        }

    }

public static void addNewItem(int numberOfItems, Scanner input) {

         numberOfItems++;
         System.out.print("Enter the title: ");
         item[numberOfItems].title = input.nextLine();
         System.out.print("Enter the format: ");
         item[numberOfItems].format = input.nextLine();

0 个答案:

没有答案