【问题标题】:Why is my Java code giving me a NullPointerException error?为什么我的 Java 代码会出现 NullPointerException 错误?
【发布时间】:2015-02-07 18:09:39
【问题描述】:

我在运行 runSnackBar 方法时收到空指针异常错误,但我无法找出原因。当我收到此错误时,BlueJ 会突出显示 randomFlavour 方法中的第一行,但我不知道为什么。请帮忙。

 import java.util.Random;
    import java.util.ArrayList;

    public class SnackBar
    {
    private Random randomGenerator;

    private String[] packets;

    private SnackMachine newSnackMachine;

    private ArrayList<Student> students;

    public SnackBar(int numOfStudents, int numPackOfCrisps, int cost)
    {
        randomGenerator = new Random();

        String[] packets = {"ready salted", "cheese and onion", "salt and vinegar" , "smokey bacon"};

        newSnackMachine = new SnackMachine(numPackOfCrisps , cost);

        for(int n=0 ; n < numPackOfCrisps ; n++){
            newSnackMachine.addPack(new PackOfCrisps(randomFlavour()));
        }
        students =  new ArrayList<Student>();

        for(int i=0 ; i < numOfStudents ; i++){
            students.add(new Student(randomFlavour(), newSnackMachine));
        }


    }

    private String randomFlavour()
    {
        int index = randomGenerator.nextInt(packets.length);
        return packets[index];

    }

    public void describe(){
        System.out.println("The SnackBar has" + students.size() + "hungry students");
        System.out.println("The SnackMachine has:" + newSnackMachine.countPacks("ready salted") + "packets of ready salted crisps");
        System.out.println("," + newSnackMachine.countPacks("cheese and onion") + "of cheese and onion crisps");
        System.out.println("," + newSnackMachine.countPacks("salt and vinegar") + "of salt and vinegar crisps");
        System.out.println("," + newSnackMachine.countPacks("smokey bacon") + "of smokey bacon crisps");

    }
    public void runSnackBar(int nSteps){
        for( int x=1 ; x < nSteps ; x++){
            System.out.println("Time step" + x);
            describe();
            int y = randomGenerator.nextInt(students.size());
            students.get(y).snackTime();
        }


    }
}

【问题讨论】:

  • NPE在哪一行?
  • 显示错误的堆栈跟踪将是最有用的。
  • int index = randomGenerator.nextInt(packets.length);

标签: java nullpointerexception bluej


【解决方案1】:
private String[] packets;

packets 是默认分配的null,这里

String[] packets = {"ready salted", "cheese and onion", 
                    "salt and vinegar" , "smokey bacon"};

你在声明一个局部变量。所以你在这里得到了 NPE:

int index = randomGenerator.nextInt(packets.length);
                                    ↑ 
                                   null

JLS 4.12.5. Initial Values of Variables:

对于所有引用类型 (§4.3),默认值为 null。

ArrayType 是一个ReferejceType

【讨论】:

    【解决方案2】:

    您试图在构造函数中初始化packets,但您不小心创建了一个局部变量。

    改变

    String[] packets = {"ready salted", "cheese and onion", "salt and vinegar" , "smokey bacon"};
    

    packets = new String[] {"ready salted", "cheese and onion", "salt and vinegar" , "smokey bacon"};
    

    所以packets 引用您的实例变量,因此它被正确初始化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      相关资源
      最近更新 更多