简体   繁体   English

为什么此List <>抛出NullPointerException?

[英]Why is this List<> throwing NullPointerException?

so i've been sitting above this code for a while , ready the NullPointerException threads, and still can't figure out what is going wrong in my code, so i turn to you. 因此,我已经在这段代码上方坐了一段时间,准备好NullPointerException线程,但仍然无法弄清楚代码中出了什么问题,因此我转向您。

public class Main {
public static void main(String[] args){     
    /* Making catalog, loading last state */
    Collection catalog = new Collection();      
    try {
        catalog.readFromFile();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
catalog.addShip(new Ship("ABC123", "John", "Suzuki", 50));
 }
}

And my Collection class looks like this: 我的Collection类如下所示:

public class Collection {
    private List<Ship> shipList;
    private String fileName = "catalog.txt";
    private int income;
    private int space;

public Collection() {
    shipList = new ArrayList<Ship>();
    income = 0;
    space = 500;
    File f = new File("catalog.txt");
    if(!f.exists()) {
        try {
            f.createNewFile();
            writeToFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void addShip(Ship SHIP){
    space -= SHIP.LENGTH;
    income += SHIP.COST;
    shipList.add(SHIP);
}

public Ship getShip(int INDEX){
    return shipList.get(INDEX);
}

public void writeToFile() throws IOException {
    FileOutputStream f = new FileOutputStream(fileName);
    ObjectOutputStream out = new ObjectOutputStream(f);
    out.writeObject(shipList);
    out.close();        
    }

@SuppressWarnings("unchecked")
public void readFromFile() throws IOException, ClassNotFoundException {
    FileInputStream f = new FileInputStream(fileName);
    ObjectInputStream in = new ObjectInputStream(f);
    shipList = (ArrayList<Ship>)in.readObject();
    in.close();
}

public int getIncome(){
    return income;
}

public int getSpace(){
    return space;
}
}

My problem is, when i call in main catalog.addship() i get nullptr error. 我的问题是,当我在main catalog.addship()调用时,出现nullptr错误。 After following the console errors, it says i get the nullptrexc when i call the addShip() on the catalog, following from there i get the error when i add() a Ship to the Collection's shipList . 遵循控制台错误后,它说我在目录上调用addShip()时得到nullptrexc,然后从那里add()运送到集合的shipList时得到错误。 So what i concluded, it is because the shipList in the Collection is uninitialized. 所以我得出的结论是,因为Collection中的shipList未初始化。 But in the constructor i write shipList = new ArrayList<Ship>(); 但是在构造函数中,我编写了shipList = new ArrayList<Ship>(); so it is clearly initialized. 因此它已明确初始化。

The exception stacktrace is the following: 异常堆栈跟踪如下:

Exception in thread "main" java.lang.NullPointerException
    at collection.Collection.addShip(Collection.java:31)
    at main.Main.main(Main.java:100)

In your main method, you initialize the ArrayList properly. 在您的main方法中,您可以正确初始化ArrayList。 But then, you make a 但是,然后,

catalog.readFromFile()

call. 呼叫。 In the readFromFile() method, you re-initialize the ArrayList 在readFromFile()方法中,您重新初始化ArrayList

shipList = (ArrayList<Ship>)in.readObject();

the in.readObject() is returning null. in.readObject()返回null。 That is why your shipList variable is null. 这就是为什么shipList变量为null的原因。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM