为什么会出现NullPointerException?

时间:2019-03-06 03:11:55

标签: java arrays nullpointerexception

在以下代码中,我不断收到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;
    }
}

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

2 个答案:

答案 0 :(得分:3)

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

myHeroStack:

null 
null
null
null
null

然后在主体中通过执行Hero A = new Hero("Archer");添加了myHeros.push(A);,因此堆栈的外观将变得如此。

myHeroStack:

A (The Hero you added)
null
null
null
null

现在,在您的打印方法中,您使用了for(Hero hero: myHeroStack) {,这基本上意味着迭代堆栈中的每个元素。因此,当第二次迭代发生时,循环中的hero将为空。这就是为什么发生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;
}

答案 1 :(得分:0)

您可以将toString()方法中的for循环更改为以下代码,这样它就不会在您的数组中显示空值:

for (int i = 0; i < top; i++) {
    // ...
}