带有对象数组的空指针异常

时间:2012-06-19 00:23:09

标签: java arrays object nullpointerexception switch-statement

我正在创建的以下程序将允许用户输入值以创建视频数组。每个视频包含多个数据字段(数字,标题,发布者,持续时间和日期)。该程序主要通过切换菜单进行控制。但是,在创建阵列后,选择其他选项(例如显示视频)将抛出空指针异常。这些异常通常在您没有为数组赋值时发生,但应该已在第一个选项createLibrary()中完成。搜索视频和更改视频中的其他功能具有相同的问题,每次使用get方法时都会出现这种情况。

任何有合理实际答案的人都会有很大的帮助。

以下是我的代码的一部分:

import java.util.*;
public class EnterLibrary
{
public final int MAX_ITEMS = 5;
public Library[] videos;
public int size = 0;

public EnterLibrary()
    {
        videos = new Library[MAX_ITEMS];
        java.util.Scanner scannerObject =new java.util.Scanner(System.in);
        LibraryMenu Menu = new LibraryMenu();
        Menu.displayMenu();
        switch (scannerObject.nextInt())
        {
            case 1:
            System.out.println ("1 - Add Videos");
                if (size < MAX_ITEMS) {
                Library video = createLibrary();
                videos[size++] = video;
                }
            new EnterLibrary();
            break;
            case 2:
            System.out.println ("2 - Show Videos");
            printVidLibrary(videos);
            new EnterLibrary();
            break;
            case 3:
            System.out.println ("3 - Search Videos");
            searchLibrary(videos);
            new EnterLibrary();
            break;
            case 4:
            System.out.println ("4 - Change Videos");
            changeLibrary(videos);
            break;      
            case 5:
            System.out.println ("5 - Delete Videos");
            deleteVideo(videos);
            new EnterLibrary();
            break;
            default:
            System.out.println ("Unrecognized option - please select options 1-5 ");
            break;
        }
    }

public Library createLibrary()
{
    Library video = new Library();
    java.util.Scanner scannerObject =new java.util.Scanner(System.in); 
    for (int i = 0; i < videos.length; i++)
    {
    //User enters values into set methods within the Library class
    System.out.print("Enter video number: " + (i+1) + "\n");
    String number = scannerObject.nextLine();
    System.out.print("Enter video title: " + (i+1) + "\n");
    String title = scannerObject.nextLine();
    System.out.print("Enter video publisher: " + (i+1) + "\n");
    String publisher = scannerObject.nextLine();
    System.out.print("Enter video duration: " + (i+1) + "\n");
    String duration = scannerObject.nextLine();
    System.out.print("Enter video date: " + (i+1) + "\n");
    String date= scannerObject.nextLine();
    System.out.print("VIDEO " + (i+1) + " ENTRY ADDED " + "\n \n");
    //Initialize arrays
    videos[i] = new Library ();
    videos[i].setVideo( number, title, publisher, duration, date  );
    }
    return video;
}

public void printVidLibrary( Library[] videos)
{
    //Get methods to print results
    System.out.print("\n***VIDEO CATALOGUE*** \n");
    for (int i = 0; i < videos.length; i++)
    {
    System.out.print("Video number " + (i+1) + ": \n" + videos[i].getNumber() + "\n ");
    System.out.print("Video title " + (i+1) + ": \n" + videos[i].getTitle() + "\n ");
    System.out.print("Video publisher " + (i+1) + ": \n" + videos[i].getPublisher() + "\n ");
    System.out.print("Video duration " + (i+1) + ": \n" + videos[i].getDuration() + "\n ");
    System.out.print("Video date " + (i+1) + ": \n" + videos[i].getDate() + "\n ");
    }
}
//Code for other functions not displayed here

public static void main(String[] args)
    {

    new EnterLibrary();
    }
}

异常错误:

Exception in thread "main" java.lang.NullPointerException
        at EnterLibrary.printVidLibrary(EnterLibrary.java:80)
        at EnterLibrary.<init>(EnterLibrary.java:26)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.<init>(EnterLibrary.java:22)
        at EnterLibrary.main(EnterLibrary.java:202)

3 个答案:

答案 0 :(得分:3)

您的代码中至少有两个严重错误:

  1. 堆栈溢出等待发生:您在EnterLibrary()内调用EnterLibrary(),这将产生无限递归
  2. 虽然你已经实例化了videos数组,但你还没有对它进行初始化,但是它已经充满了空对象;确保在createLibrary()的构造函数之前调用EnterLibrary 调用printVidLibrary(),这会迭代{{1}的所有内容并假设它已完全初始化

答案 1 :(得分:0)

videos = new Library[MAX_ITEMS];未初始化Library项,因此每个项都为null。试试这个

videos = new Library[MAX_ITEMS];
for(int x = 0; x < MAX_ITEMS; x++)
  videos[x] = new Library();

如果videos参数为null,您还可以检入printVidLibrary以避免异常。

答案 2 :(得分:0)

你弄错了,你有一个扭曲的心灵!

如果我了解你想再次检查你的开关(例如在添加视频之后):你应该在你的开关周围使用一个循环,而不是创建一个 new ,因此视频处女“EnterLibrary “对象。

一些代码:

while (i = scannerObject.nextInt()) {
  switch(i) {
    case 1:
        System.out.println ("1 - Add Videos");
        if (size < MAX_ITEMS) {
          Library video = createLibrary();
          videos[size++] = video;
        }
        break;
    case 2:
        System.out.println ("2 - Show Videos");
        printVidLibrary(videos);
        break;
    case 3:
        System.out.println ("3 - Search Videos");
        searchLibrary(videos);
        break;
    case 4:
        System.out.println ("4 - Change Videos");
        changeLibrary(videos);
        break;      
    case 5:
        System.out.println ("5 - Delete Videos");
        deleteVideo(videos);
        break;
    default:
        System.out.println ("Unrecognized option - please select options 1-5 ");
        break;
    }
}

另一方面,我没有多想,现在我有点累了,但这并不是对OOP的整体好用,我可能错了,但我不能得到对于初学者来说,抓住“EnterLibrary”对象是什么。

是的,按照bmargulies的建议熟悉调试器,他是你最好的朋友我猜

编辑:我编辑了代码,忘了删除你的递归调用