我正在创建的以下程序将允许用户输入值来创建视频数组。每个视频都包含多个数据字段(编号、标题、发布者、持续时间和日期)。该程序主要通过开关菜单进行控制。但是在创建数组后,选择另一个选项,例如 Show videos 将抛出 Null Pointer Exception。这些异常通常发生在您没有为数组赋值时,但应该已经在第一个选项 createLibrary() 中完成了。 Search videos 和 Change videos 中的其他函数也有同样的问题,每次使用 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)
最佳答案
您的代码中至少有两个严重错误:
- 等待发生堆栈溢出:您在
EnterLibrary()
中调用EnterLibrary()
,这将产生无限递归 - 虽然您已经实例化了
videos
数组,但您还没有初始化它,因为它充满了空对象;确保在EnterLibrary
的构造函数开始时调用createLibrary()
,在调用printVidLibrary()
之前,它遍历videos
的所有内容并假定它已完全初始化
关于java - 对象数组的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092855/