Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用另一个类的属性创建类时使用空指针_Java_Inheritance_Nullpointerexception - Fatal编程技术网

Java 使用另一个类的属性创建类时使用空指针

Java 使用另一个类的属性创建类时使用空指针,java,inheritance,nullpointerexception,Java,Inheritance,Nullpointerexception,我从事的项目存储了我的项目、人员,以及当我借用一些项目给一些人员 该项目基于YouTube上的经典图书库 我很抱歉,我会发布更多的代码,因为我想告诉大家我是如何做的。我个人认为如何获得ItemClass或PersonClass的实例存在问题。或者是继承类的东西 现在,BorrowClass中的前两个方法可以正常工作,创建项目和人员并添加到列表中,但是当我尝试创建新的BorrowClass时,它会抛出项目或人员的实例,然而,通过调试,我能够看到借来的,所有的东西似乎都不是空的,或者红色的方块意味着

我从事的项目存储了我的项目人员,以及当我借用一些项目给一些人员

该项目基于YouTube上的经典图书库

我很抱歉,我会发布更多的代码,因为我想告诉大家我是如何做的。我个人认为如何获得ItemClassPersonClass的实例存在问题。或者是继承类的东西

现在,BorrowClass中的前两个方法可以正常工作,创建项目和人员并添加到列表中,但是当我尝试创建新的BorrowClass时,它会抛出项目或人员的实例,然而,通过调试,我能够看到借来的,所有的东西似乎都不是空的,或者红色的方块意味着问题吗

我创建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;

    }
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;

    }
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;

    }
然后PersonClass

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;

    }
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;

    }
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;

    }
然后借用类

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;

    }
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;

    }
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;

    }
现在我在类中处理以上所有内容

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);
    }
因为在视频中我没有看到,我想知道


谢谢您的回答。

关于
serialVersionUID

所以。。。基本上,在某个时刻,您可能希望将对象
序列化
并将其保存在某个位置(如文件或数据库中),以便以后可以
反序列化它们。现在,
serialVersionUID
字段用于了解您是否使用与创建序列化对象时使用的类定义相同的类定义


所以。。。每当您对某个可
序列化的类进行更改时,您都会将
serialVersionUID
增加1,以便旧类版本的
seiralized
对象不会使用当前类版本错误地
反序列化。

女士们、先生们,我为我的愚蠢向你们道歉。我从文件中加载了函数,我正在加载一个旧的库文件,其中列表借用仍然不存在,因此没有要添加的列表。这里是空指针。我被困在这个问题上有一段时间了,但只是因为我的愚蠢:)谢谢大家的帮助。您可以关闭主题

引发
NullPointerException
的调用具体是什么?您发布的任何代码都不可能引发
NullPointerException
。请按照堆栈跟踪中的指示发布抛出
NullPointerException
的代码行,您可能会在这里得到一个非常快速的答案。请阅读。首先要做的是读取堆栈跟踪。序列化对象时使用serialVersionUID。编译器将根据类的哈希值为您创建一个类,因此每次更改类时它都会更改。通过显式设置,您可以序列化,而不必担心类是否在序列化和反序列化之间发生更改。@RandyKamradtSr。不用担心?不,你只会得到一条隐秘的错误信息,而不是一条清晰的信息。