Java 行内NPE调用equals()
对不起,如果这一切似乎很难理解,但我是一个新的编程,我已经在几本书和网站上看到,从我的理解,我试图做什么应该工作。我正在做的作业是调用类的类。没有把我所有的代码都放在这里,我会在不清楚的地方尽量具体。空指针异常适用于此特定代码行:Java 行内NPE调用equals(),java,class,methods,equals,Java,Class,Methods,Equals,对不起,如果这一切似乎很难理解,但我是一个新的编程,我已经在几本书和网站上看到,从我的理解,我试图做什么应该工作。我正在做的作业是调用类的类。没有把我所有的代码都放在这里,我会在不清楚的地方尽量具体。空指针异常适用于此特定代码行: if(CDList[i].getArtist().equals(artist) == true) //CDList是一个CD对象数组(在另一个类中创建) //getArtist()是CD类的一个方法,它返回一个字符串 //equals()中的艺术家是用户输入的扫
if(CDList[i].getArtist().equals(artist) == true)
//CDList是一个CD对象数组(在另一个类中创建)
//getArtist()是CD类的一个方法,它返回一个字符串
//equals()中的艺术家是用户输入的扫描仪对象,也是一个字符串
此特定方法的要点是在数组CDList中搜索,并将存储的艺术家字符串与扫描的艺术家字符串进行比较,然后将其与标题进行比较。如果找到,则该数组部分的内容将被删除。以下是该方法的其余部分(如果有帮助):
void delete()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter artist and title to be deleted: ");
String artist = input.nextLine();
String title = input.nextLine();
for(int i = 0; i <= numOfCDs; i++)
{
if(CDList[i].getArtist().equals(artist) == true)
{
for(int j = 0; j <= numOfCDs; j++)
{
if(CDList[j].getTitle().equals(title) == true)
{
System.out.println("Found CD: " + CDList[j].getArtist() + " " +
CDList[j].getTitle());
System.out.println("Would you like to delete it? Y/1 N/0 ");
if(input.nextInt() == 1)
{
CDList[j] = null;
numOfCDs--;
}
}
else
System.out.println("CD not found.");
}
}
else
System.out.println("CD not found.");
}
}
轨迹列表类:
package assignment3;
public class tracklist
{
public String[] tracks;
public int numElements;
tracklist()
{
tracks = new String[99];
numElements = 0;
}
public boolean add(String track)
{
boolean result = true;
int index = 0;
while(tracks[index] != null)
{
index++;
}
tracks[index] = track;
numElements++;
if(numElements > 99)
result = false;
return result;
}
public int count()
{
return numElements;
}
public void display(int indent)
{
for(int i = 1; i < numElements; i++)
{
System.out.print(i);
if(i >= 10)
{
for(int j = 0; j < (indent - 1); j++)
{
System.out.print(" ");
}
}
else
{
for(int j = 0; j < indent; j++)
{
System.out.print(" ");
}
}
System.out.println(tracks[i]);
}
}
}
package assignment3;
import java.util.Scanner;
public class CDList
{
public int numOfCDs;
private CD[] CDList;
private int front,rear;
CDList(int size)
{
CDList = new CD[size];
numOfCDs = 0;
front = 0;
rear = size - 1;
}
boolean add()
{
boolean result;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Artist Name and CD Title: ");
CD userCD = new CD(input.nextLine(), input.nextLine());
System.out.println("Enter the number of tracks: ");
int trackNumber = input.nextInt();
System.out.println("Enter your track titles: ");
for(int i = 0; i <= trackNumber; i++)
{
userCD.addTrack(input.nextLine());
}
if(rear == front)
result = false;
else
{
if(CDList[rear] != null)
rear--;
else
CDList[rear] = userCD;
result = true;
}
return result;
}
void delete()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter artist and title to be deleted: ");
String artist = input.nextLine();
String title = input.nextLine();
for(int i = 0; i <= CDList.length - 1; i++)
{
if((CDList[i].getArtist().equals(artist)) &&
(CDList[i].getTitle().equals(title)))
{
System.out.println("Found CD of: " + CDList[i].getArtist() + " " +
CDList[i].getTitle());
System.out.println("Would you like to delete it? Y/1 N/0 ");
if(input.nextInt() == 1)
{
CDList[i] = null;
numOfCDs--;
}
}
else
System.out.println("CD not found.");
}
}
void SortArtist()
{
CD temp = new CD(" ", " ");
for(int i = 0; i < numOfCDs; i++)
if(CDList[i].getArtist().compareTo(CDList[i + 1].getArtist()) < 0)
{
temp = CDList[i];
CDList[i] = CDList[i + 1];
CDList[i + 1] = temp;
}
}
void SortTitle()
{
CD temp = new CD(" ", " ");
for(int i = numOfCDs; i > 0; i--)
{
int x = 0;
for(int j = 1; j <= i; j++)
{
if(CDList[i].getTitle().compareTo(CDList[i + 1].getTitle()) < 0)
x = j;
}
temp = CDList[x];
CDList[x] = CDList[i];
CDList[i] = temp;
}
}
void Display()
{
for(int i = 0; i <= numOfCDs; i++)
{
while(CDList[i] == null)
i++;
CDList[i].display();
}
}
int size()
{
return numOfCDs;
}
}
包分配3;
公共类跟踪列表
{
公共字符串[]轨道;
公共设施;
跟踪列表()
{
轨迹=新字符串[99];
数值=0;
}
公共布尔添加(字符串轨迹)
{
布尔结果=真;
int指数=0;
while(跟踪[索引]!=null)
{
索引++;
}
轨道[索引]=轨道;
numElements++;
如果(数值>99)
结果=假;
返回结果;
}
公共整数计数()
{
回礼;
}
公共空白显示(整数缩进)
{
对于(int i=1;i=10)
{
对于(int j=0;j<(缩进-1);j++)
{
系统输出打印(“”);
}
}
其他的
{
对于(int j=0;j
CDList类:
package assignment3;
public class tracklist
{
public String[] tracks;
public int numElements;
tracklist()
{
tracks = new String[99];
numElements = 0;
}
public boolean add(String track)
{
boolean result = true;
int index = 0;
while(tracks[index] != null)
{
index++;
}
tracks[index] = track;
numElements++;
if(numElements > 99)
result = false;
return result;
}
public int count()
{
return numElements;
}
public void display(int indent)
{
for(int i = 1; i < numElements; i++)
{
System.out.print(i);
if(i >= 10)
{
for(int j = 0; j < (indent - 1); j++)
{
System.out.print(" ");
}
}
else
{
for(int j = 0; j < indent; j++)
{
System.out.print(" ");
}
}
System.out.println(tracks[i]);
}
}
}
package assignment3;
import java.util.Scanner;
public class CDList
{
public int numOfCDs;
private CD[] CDList;
private int front,rear;
CDList(int size)
{
CDList = new CD[size];
numOfCDs = 0;
front = 0;
rear = size - 1;
}
boolean add()
{
boolean result;
Scanner input = new Scanner(System.in);
System.out.println("Enter the Artist Name and CD Title: ");
CD userCD = new CD(input.nextLine(), input.nextLine());
System.out.println("Enter the number of tracks: ");
int trackNumber = input.nextInt();
System.out.println("Enter your track titles: ");
for(int i = 0; i <= trackNumber; i++)
{
userCD.addTrack(input.nextLine());
}
if(rear == front)
result = false;
else
{
if(CDList[rear] != null)
rear--;
else
CDList[rear] = userCD;
result = true;
}
return result;
}
void delete()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter artist and title to be deleted: ");
String artist = input.nextLine();
String title = input.nextLine();
for(int i = 0; i <= CDList.length - 1; i++)
{
if((CDList[i].getArtist().equals(artist)) &&
(CDList[i].getTitle().equals(title)))
{
System.out.println("Found CD of: " + CDList[i].getArtist() + " " +
CDList[i].getTitle());
System.out.println("Would you like to delete it? Y/1 N/0 ");
if(input.nextInt() == 1)
{
CDList[i] = null;
numOfCDs--;
}
}
else
System.out.println("CD not found.");
}
}
void SortArtist()
{
CD temp = new CD(" ", " ");
for(int i = 0; i < numOfCDs; i++)
if(CDList[i].getArtist().compareTo(CDList[i + 1].getArtist()) < 0)
{
temp = CDList[i];
CDList[i] = CDList[i + 1];
CDList[i + 1] = temp;
}
}
void SortTitle()
{
CD temp = new CD(" ", " ");
for(int i = numOfCDs; i > 0; i--)
{
int x = 0;
for(int j = 1; j <= i; j++)
{
if(CDList[i].getTitle().compareTo(CDList[i + 1].getTitle()) < 0)
x = j;
}
temp = CDList[x];
CDList[x] = CDList[i];
CDList[i] = temp;
}
}
void Display()
{
for(int i = 0; i <= numOfCDs; i++)
{
while(CDList[i] == null)
i++;
CDList[i].display();
}
}
int size()
{
return numOfCDs;
}
}
包分配3;
导入java.util.Scanner;
公共类CDList
{
公共国际货币基金组织;
私人CD[]CDList;
前,后;
CDList(整数大小)
{
CDList=新CD[尺寸];
numOfCDs=0;
正面=0;
后部=尺寸-1;
}
布尔加法()
{
布尔结果;
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入艺术家姓名和CD标题:”);
CD userCD=newcd(input.nextLine(),input.nextLine());
System.out.println(“输入曲目数量:”);
int trackNumber=input.nextInt();
System.out.println(“输入曲目标题:”);
对于(int i=0;i
如果您获得NPE,以下是可能的情况:
CDList
为空
CDList[i]
为空
CDLIst[i].getArtist()
返回null
类Artist
重写equals()
并具有导致NPE的错误,但在这种情况下,NPE将指向equals()
中的语句
您没有显示类Artist
,因此我们可以查看它是否覆盖equals()
,也没有发布堆栈跟踪,以便我们可以准确地看到引发异常的位置
正如其他人所评论的那样,==true
是多余的
如果您获得NPE,以下是可能的情况:
CDList
为空
CDList[i]
为空
CDLIst[i].getArtist()
返回null
类Artist
重写equals()
并具有导致NPE的错误,但在这种情况下,NPE将指向equals()
中的语句
您没有显示类Artist
,因此我们可以查看它是否覆盖equals()
,也没有发布堆栈跟踪,以便我们可以准确地看到引发异常的位置
正如其他人所评论的那样,==true
是多余的。我建议使用LinkedList
或ArrayList
而不是CD[]
这将允许您轻松删除项目,如下所示:
LinkedList<CD> cdList = new LinkedList<CD>();
// add items with cdList.add(...);
Iterator<CD> cdListIterator = cdList.iterator();
// Loop while the list still contains elements.
while (cdListIterator.hasNext()) {
CD thisCd = iterator.next();
// do some operation on the cd to tell whether you want to delete it
// for example:
if (thisCd.getArtist().equals(artist) && thisCd.getTitle().equals(title)) {
iterator.remove(); // it's that simple
// Don't have to mess with `cdCount--` or anything.
}
}
LinkedList cdList=新建LinkedList();
//使用cdList添加项目。添加(…);
迭代器cdListIterator=cdList.Iterator();
//在列表仍包含元素时循环。
while(cdListIterator.hasNext()){
CD thisCd=iterator.next();
//对cd执行一些操作,以确定是否要删除它
//例如:
if(thisCd.getArtist().equals(艺术家)&&thisCd.getTitle().equals(标题)){
iterator.remove();//就是这么简单
//不必去搞“cdCount”之类的东西。
}
}
而且,正如一些人所评论的,您不需要a.equals(b)=true
;您可以使用a.equals(b)
,我建议使用LinkedList
或ArrayList
,而不是CD[]
这将允许您轻松删除项目,如下所示:
LinkedList<CD> cdList = new LinkedList<CD>();
// add items with cdList.add(...);
Iterator<CD> cdListIterator = cdList.iterator();
// Loop while the list still contains elements.
while (cdListIterator.hasNext()) {
CD thisCd = iterator.next();
// do some operation on the cd to tell whether you want to delete it
// for example:
if (thisCd.getArtist().equals(artist) && thisCd.getTitle().equals(title)) {
iterator.remove(); // it's that simple
// Don't have to mess with `cdCount--` or anything.
}
}
LinkedList cdList=新建LinkedList();
//使用cdList添加项目。添加(…);
迭代器cdListIterator=cdList.Iterator();
//在列表仍包含元素时循环。
while(cdListIterator.hasNext()){
CD thisCd=iterator.next();
//对cd执行一些操作,以确定是否要删除它
//例如:
if(thisCd.getArtist().equals(艺术家)&&thisCd.getTitle().equals(标题)){
iterator.remove();//就是这么简单
//不必去搞“cdCount”之类的东西。
}
}
而且,正如一些人所评论的,您不需要a.equals(b)=true
;您可以使用a.equals(b)
if条件没有问题。问题出在您的循环上。
使用单个for循环:
for(int i = 0; i <= numOfCDs; i++)
{
if(CDList[i].getArtist().equals(artist) && CDList[i].getTitle().equals(title))
{
System.out.println("Found CD: " + CDList[j].getArtist() + " " + CDList[j].getTitle());
System.out.println("Would you like to delete it? Y/1 N/0 ");
if(input.nextInt() == 1)
{
CDList[i] = null;
// do not do numOfCDs-- here
}
}
}
for(inti=0;iif条件没有问题。这是您的lo