我對某個項目存儲我的項目,人,當我借一些項目一些人空指針,同時還與其他類
這個項目動作的屬性的類基於古典圖書館的YouTube tutorial。
對不起,我會發布一些更多的代碼,因爲我想給出一些想法我是如何做到這一點。我個人認爲我如何得到ItemClass或PersonClass的實例存在問題。或者在BorrowClass附近繼承。
現在前兩種方法在BorrowClass工作好,創造項目和人並添加到列表中,但是當我嘗試創建新BorrowClass含和項目或人實例它拋出nullPointer,但通過調試我能看到借來在variables和一切似乎不爲空或紅色方塊意味着問題?
創建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;
}
現在,以上所有我在圖書館類處理
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;
的意義,因爲在視頻中,我並沒有得到它,我想知道。
感謝您的任何答案。
究竟是拋出一個'NullPointerException'的調用? – 2015-02-24 17:30:38
您發佈的任何代碼都可能會拋出'NullPointerException'。請按照堆棧跟蹤中的指示發佈拋出'NullPointerException'的代碼行,您可能會在這裏得到一個非常快速的答案。 – Asaph 2015-02-24 17:31:26
閱讀http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it。首先要做的是讀取堆棧跟蹤。 – 2015-02-24 17:31:49