gpt4 book ai didi

java - 为什么我会收到 NullPointerException?

转载 作者:行者123 更新时间:2023-12-01 17:46:14 26 4
gpt4 key购买 nike

我在下面的代码中不断收到 NullPointerException 。异常出现在这一行 System.out.println(myHeros.toString());

公开课测试{

public static void main(String[] args) {

Hero A = new Hero("Archer");
Hero B = new Hero("Bacca");
Hero C = new Hero("Chester");
Hero D = new Hero("Teemo");
Hero E = new Hero("Garen");

MyStack myHeros = new MyStack(10);

System.out.println(!(A instanceof Hero)); // Output: false;

boolean debug = true;
try {

myHeros.push(A);
if(debug) {
System.out.println("lalala");
}
}
catch(notAHeroException e) {
//e.printStackTrace();
}

System.out.println(myHeros.toString()); //This is the line that throws nullPointerException;

System.out.println(myHeros.getTop()); //(Debug) Output: 1, the Hero was actually pushed into myHeroStack;
}

}

这是包含 toString 方法的类。我真的看不出哪里可能会导致 NullPointerException。

public class MyStack implements Stack{

public Hero[] myHeroStack; //instance field; can be from this class to the whole world;
int top; //The combination of these instance fields represent all you know about your object and all you can use about your object;

public MyStack() {
myHeroStack = new Hero[5];
top = 0;

}

public MyStack(int numOfHero) {
myHeroStack = new Hero[numOfHero];
top = 0;
}

@Override
public void push(Hero h) throws notAHeroException{
//check the input;
boolean checkType = !(h instanceof Hero);
if (checkType) { //if (!(h instanceof Hero))
throw new notAHeroException("Check again what did you add?!");
}

else {
myHeroStack[top] = h;
top = top + 1;
}

}

@Override
public void pull(Hero g) throws emptyStackException { //check whether the element you want to throw actually exist;
//check whether the element exist
if(myHeroStack[top - 1] == null) {
throw new emptyStackException("You do have this hero in you stack!");

}
else {
Hero heroToPull = myHeroStack[top - 1];
System.out.println(heroToPull.getName());
myHeroStack[top - 1] = null;
top = top -1;

}

}

@Override
public void peek(Hero p) {
System.out.println(p.getName());
}

public int getTop() {
return top;
}

public String toString() {
//check whether the stack is null
if(top == 0) {
return ("You hero stack is empty");
}
else {
String AllMyHero = "";
for(Hero hero: myHeroStack) {
AllMyHero = AllMyHero + "All my hero is " + hero.getName()+ "%n"; //hero is null point
}
return AllMyHero;
}


}

}

这是 Hero 类,非常简单。

public class Hero {

private String name;

public Hero(String name) {
this.name = name;
}
}

感谢所有关注此问题的人!

最佳答案

您正在通过执行 myHeroStack = new Hero[5]; 来初始化 myHeroStack,这基本上意味着为 5 个 Hero 对象准备 5 个位置。这将是初始化时堆栈的外观。

我的英雄堆栈:

null 
null
null
null
null

然后在你的 main 中添加 Hero A = new Hero("Archer"); 通过这样做 myHeros.push(A); 因此你的堆栈的外观将会。

我的英雄堆栈:

A (The Hero you added)
null
null
null
null

现在,在您的打印方法中,您使用了 for(Hero Hero: myHeroStack) { 这基本上意味着迭代堆栈中的每个元素。因此,当第二次迭代发生时,循环中的 hero 将为 null。这就是发生 NullPointerException 的原因。

您应该更新您的打印方法以包含对空值的检查。请引用下面的代码。

public String toString() {
//check whether the stack is null
if(top == 0) {
return ("You hero stack is empty");
}
else {
String AllMyHero = "";
for(Hero hero: myHeroStack) {
if (hero != null) {
AllMyHero = AllMyHero + "All my hero is " + hero.getName()+ "%n"; //hero is null point
}
}
}
return AllMyHero;
}

关于java - 为什么我会收到 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55014939/

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