我收到空指针异常错误,但我不明白出了什么问题。
public static void main(String[] args) {
CDlist CD[] = new CDlist[5];
CD[0].add();
}
那是我的主要,很简单,它只是创建5个CD对象并调用第一个对象的add方法。
public boolean add(){
String author;
String title;
String songTitle;
int amount;
boolean result = false;
if(numUsed < length){
System.out.println("Please input the name of the CD you wish to add.");
title = input.next();
CD[numUsed].title = title;
System.out.println("Please input the author of the CD you wish to add.");
author = input.next();
CD[numUsed].title = title;
System.out.println("Please input the amount of songs you want to have.");
amount = input.nextInt();
for(int i = 0; i<amount; i++){
System.out.println("Add song name:");
songTitle = input.next();
CD[numUsed].song[amount] = songTitle;
}
numUsed++;
result = true;
}
return result;
}
这是我在 CDlist 类中的 add 方法