1

我正在创建的以下程序将允许用户输入值来创建视频数组。每个视频包含多个数据字段(编号、标题、发布者、持续时间和日期)。该程序主要通过开关菜单进行控制。但是,在创建数组后,选择另一个选项,例如 Show videos 将引发 Null Pointer Exception。当您没有为数组赋值时,通常会发生这些异常,但这应该已经在第一个选项 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)
4

3 回答 3

3

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

  1. 有一个堆栈溢出等待发生:你正在调用EnterLibrary()inside EnterLibrary(),这将产生一个无限递归
  2. 尽管您已经实例化了videos数组,但您还没有初始化它,因为它充满了空对象;确保在调用之前在构造函数createLibrary()的开头调用,它遍历所有内容并假定它已完全初始化EnterLibraryprintVidLibrary()videos
于 2012-06-19T00:29:28.773 回答
0

videos = new Library[MAX_ITEMS];不初始化库项目,因此每个项目都是空的。试试这个

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

如果视频参数为空,您还可以签入 printVidLibrary 以避免异常。

于 2012-06-19T00:30:22.050 回答
0

你错了,你脑子有问题!

如果我知道您想再次检查您的开关(例如在添加视频之后):您应该在开关周围使用循环,而不是创建的,因此是 video-virgin "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 的建议熟悉调试器,我猜他是你最好的朋友

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

于 2012-06-19T00:47:09.950 回答