gpt4 book ai didi

java - 使用数组打印时出现空指针异常

转载 作者:行者123 更新时间:2023-12-02 03:35:30 24 4
gpt4 key购买 nike

似乎无法消除该错误,查看了消除它的不同方法,但无法真正理解。

static Holding[]holding = new Holding[15];

public static void main(String[] args){
sampleData(); //Sample Data from Assignment

int options;
do{
scanner = new Scanner(System.in);

System.out.println("1. Add Holding");
System.out.println("7. Print all Holdings");
System.out.println("13. Exit");

options = scanner.nextInt();
switch(options){

case 1:
addHolding();
break;
case 7:
printHolding();
break;

default:
System.out.println("Please");
}

}while(options != 13);
System.out.println("I'm out");

}

public static void sampleData(){
holding[0] = new Book("b000001", "Intro to Java");
holding[1] = new Book("b000002", "Learning UML");
holding[2] = new Book("b000003", "Design Patterns");
holding[3] = new Book("b000004", "Advanced Java");
holding[4] = new Video("v000001", "Java 1", 4);
holding[5] = new Video("v000002", "Java 2", 6);
holding[6] = new Video("v000003", "UML 1", 6);
holding[7] = new Video("v000004", "UML 2", 4);

}

public static void printHolding(){
int option;
do{
for(Holding h : holding){
h.print();
System.out.println();
}

System.out.println("To exit press '0'");
option = input.nextInt();
}while(option != 0);


}

Exception in thread "main" java.lang.NullPointerException
at s3599741_A2.LibraryMenu.printHolding(LibraryMenu.java:307)
at s3599741_A2.LibraryMenu.main(LibraryMenu.java:71)

错误是

h.print();

printHolding();

在 switch 语句中。正如您所看到的,该数组是由另一个对象组成的,例如

holding[i] = new Book("Insert ID", "Insert Title");

我写了一个这样的问题,但被标记为重复,我查看了链接

What is a NullPointerException, and how do I fix it?

但是找不到我遇到的问题或者我还没有了解的知识。有人可以解释我应该做什么吗?

Holding 的限制必须是 15,我不能使用 ArrayList 或 List。

控股类(Class):

public abstract class Holding{

private String holdingId;
private String title;

public Holding(String holdingId, String title){

this.holdingId = holdingId;
this.title = title;
}

public String getId() {

return holdingId;
}

public String getTitle(){

return title;
}

public boolean getStatus(){

return active;
}

public void print(){
System.out.println("ID: " + getId());
System.out.println("Title: " + getTitle());
}

}

最佳答案

您的holding数组开始时填充有nullsampleData 仅将 holding 的八个条目(条目 0 - 7,包含在内)设置为非 null 值;这意味着条目 8 - 14(含)仍然为 null

这意味着这段代码将抛出:

for (Holding h : holding) {
h.print(); // Here
System.out.println();
}

要修复此问题,请添加 null 检查:

for (Holding h : holding) {
if (h != null) {
h.print();
System.out.println();
}
}

...和/或跟踪您通过实例字段添加了多少 Holding(因为您显然不允许为此使用正确的数据结构,列表);

static int holdingCount = 0;

...那么 sampleData 看起来像:

public static void sampleData() {
addSampleData(new Book("b000001", "Intro to Java"));
addSampleData(new Book("b000002", "Learning UML"));
addSampleData(new Book("b000003", "Design Patterns"));
addSampleData(new Book("b000004", "Advanced Java"));
addSampleData(new Video("v000001", "Java 1", 4));
addSampleData(new Video("v000002", "Java 2", 6));
addSampleData(new Video("v000003", "UML 1", 6));
addSampleData(new Video("v000004", "UML 2", 4));
}

然后

private static void addSampleData(Holding h) {
if (holdingCount < holding.length) {
holding[holdingCount++] = h;
} else {
throw new IllegalStateException("Tried to add too many holdings to array");
}
}

addHolding 会做类似的事情。

但是,这只是因为您说过不允许为此使用正确的数据结构。

关于java - 使用数组打印时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37495994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com