【问题标题】:Why I get NullPointerException? [duplicate]为什么我得到 NullPointerException? [复制]
【发布时间】:2019-03-06 03:11:55
【问题描述】:

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

感谢所有关注这个问题的人!

【问题讨论】:

  • 你有一个未初始化的Heros数组。

标签: java arrays nullpointerexception


【解决方案1】:

您正在通过 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 将为空。这就是发生 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;
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 2016-11-17
      相关资源
      最近更新 更多