使用另一个 class 的属性创建 class 时的 NullPointer

NullPointer while creating a class with attribute of another class

我在一个项目中存储我的 ItemsPersonsBorrow 时的操作] 一些 物品 一些

此项目基于 YouTube tutorial 的 class 图书馆。

很抱歉,我会 post 编写更多代码,因为我想说明一下我是如何做的。我个人认为我如何获取 ItemClassPersonClass 的实例存在问题。或者围绕 BorrowClass.

继承的东西

现在 BorrowClass 中的前两个方法可以正常工作,创建 itemspersons 并添加到列表中,但是当我尝试创建包含 itemspersons 的新 BorrowClass 实例时抛出 nullPointer,但通过调试我能够在 variables 中看到 borrowed 并且一切似乎都不为空或红色方块意思是有问题?

我创建了ItemClass

public class ItemClass implements Serializable {

    private static final long serialVersionUID = 1L;

    private int isbn;
    private String title, author;
    private String otherInfo;

    public ItemClass(String title,String otherInfo, String author,int isbn) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
        this.otherInfo = otherInfo;

    }

然后PersonClass

public class PersonClass implements Serializable {

    private static final long serialVersionUID = 1L;

    private int telephone;
    private String name, surename, email;

    public PersonClass(String name,String surename, int telephone,String email) {
        this.name = name;
        this.surename = surename;
        this.telephone = telephone;
        this.email = email;

    }

然后BorrowCLass

public class BorrowClass implements Serializable  {

    private static final long serialVersionUID = 1L;

    private ItemClass item;
    private PersonClass person;
    private Date borrowDate;
    private Date expireDate;

    public BorrowClass(ItemClass item, PersonClass person, Date borrowDate,
            Date expireDate) {
        this.item = item;
        this.person = person;
        this.borrowDate = borrowDate;
        this.expireDate = expireDate;

    }

现在我在图书馆 class

中处理以上所有内容
public class Library extends Object implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<ItemClass> listOfItems;
    private List<PersonClass> listOfPersons;
    private List<BorrowClass> listOfBorrowed;

    public Library() {
        listOfItems = new ArrayList<ItemClass>();
        listOfPersons = new ArrayList<PersonClass>();
        listOfBorrowed = new ArrayList<BorrowClass>();
    }

    public void addItemtoItemsCollection(ItemClass item) {
        listOfItems.add(item);
    }

    public void addPersontoPersonCollection(PersonClass person) {
        listOfPersons.add(person);
    }

    public void addBorrow(BorrowClass borrowed) {
        listOfBorrowed.add(borrowed);
    }

如果有人能解释一下

的意思就好了
private static final long serialVersionUID = 1L;

因为视频里没看懂,想知道

感谢您的回答。

关于 serialVersionUID :

所以...基本上在某些时候您可能想要 serialize 您的对象并将它们保存在某个地方(例如在文件或数据库中),以便您以后可以 deserialize 它们。现在,serialVersionUID 字段用于了解您使用的 class 定义是否与创建此对象时使用的定义相同。

所以...每当您更改某些 class 即 serializable 时,您会将 serialVersionUID 增加 1,以便旧的任何 seiralized 对象class 版本没有错误 deserialized 使用当前 class 版本。

女士们先生们,我为我的愚蠢道歉。我有从文件加载的功能,我正在加载一个 OLD 库文件,其中列表借用仍然不存在,因此没有要添加的列表。在这里我有 nullPointer。我坚持了一段时间,但只是因为我的愚蠢 :) 谢谢大家的帮助。您可以关闭话题