2015-02-24 57 views
-1

我對某個項目存儲我的項目,當我一些項目一些空指針,同時還與其他類

這個項目動作的屬性的類基於古典圖書館的YouTube tutorial

對不起,我會發布一些更多的代碼,因爲我想給出一些想法我是如何做到這一點。我個人認爲我如何得到ItemClassPersonClass的實例存在問題。或者在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; 

的意義,因爲在視頻中,我並沒有得到它,我想知道。

感謝您的任何答案。

+1

究竟是拋出一個'NullPointerException'的調用? – 2015-02-24 17:30:38

+2

您發佈的任何代碼都可能會拋出'NullPointerException'。請按照堆棧跟蹤中的指示發佈拋出'NullPointerException'的代碼行,您可能會在這裏得到一個非常快速的答案。 – Asaph 2015-02-24 17:31:26

+0

閱讀http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it。首先要做的是讀取堆棧跟蹤。 – 2015-02-24 17:31:49

回答

0

女士們,先生們,我爲我的愚蠢謙遜地道歉。我有文件的函數加載,我正在加載一個庫文件,其中列表借用仍然不存在,因此沒有要添加的列表。在這裏我有nullPointer。我被困在這一段時間,但只爲我的愚蠢:)感謝所有的幫助。你可以關閉主題

0

關於serialVersionUID

所以...基本上你可能想serialize你的對象,並保存在某個地方他們(就像在一個文件或數據庫),這樣你可以在以後deserialize他們一些點。現在,serialVersionUID字段用於知道,如果您使用的是與創建此對象時使用的相同類定義的序列化。

所以...每當你對serializable的某個類進行更改時,都會將serialVersionUID增加1,以便舊類版本的任何seiralized對象不會錯誤地使用當前類版本deserialized