-1

I work on a project that stores my Items , Persons and action when I Borrow some Item to some Person

This project is based on a YouTube tutorial for classical library of books.

I'm sorry for I will post some more code because I want to give some idea how I'm doing it. I personally think that there is a problem with how I get the instances of ItemClass or PersonClass. Or something with inheritance around BorrowClass.

Now first two Methods in BorrowClass works okay, creating items and persons and adding to the lists, but when I try to create new BorrowClass containing and instance of items or persons it throws nullPointer, yet through debug I was able to see borrowed in variables and everything seemed to be not null or does the red squares means problem ?

I create 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;

    }

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

    }

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

    }

Now All above I handle in Library 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);
    }

Also would be nice if someone could explain the meaning of

private static final long serialVersionUID = 1L;

because in the video I didn't get it and I would like to know.

Thanks for any answer.

Asaph
  • 159,146
  • 25
  • 197
  • 199
Gowronga
  • 11
  • 4
  • 1
    What exactly is the call that throws a `NullPointerException`? – André Stannek Feb 24 '15 at 17:30
  • 2
    None of the code you've posted could possibly throw a `NullPointerException`. Please post the line of code that is throwing the `NullPointerException` as indicated in stack trace and you'll likely get a very quick answer here. – Asaph Feb 24 '15 at 17:31
  • Read http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it. The very first thing to do is to read the stack trace. – JB Nizet Feb 24 '15 at 17:31
  • Also: http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it – André Stannek Feb 24 '15 at 17:32
  • 1
    The serialVersionUID is used when serializing objects. The compiler will create one for you based on the hash of the class, so it will change every time you change the class. By setting it explicitly, you can serialize without worrying about if the class changes between serialization and de-serialization. – Randy Kamradt Sr. Feb 24 '15 at 17:35
  • 2
    @RandyKamradtSr. without worrying? Hell no. You'll only get a cryptic error message instead of a clear one. – JB Nizet Feb 24 '15 at 17:39
  • `java.lang.NullPointerException at library.model.Library.addBorrow(Library.java:38) at library.controller.LibSystem.AddNewBorrow(LibSystem.java:378)` This goes to console on line `library.addBorrow(borrowed);` – Gowronga Feb 24 '15 at 17:47
  • @Gowronga that doesn't seem possible from the code you are showing us. Did you try to clean your project? – André Stannek Feb 24 '15 at 17:51
  • and I think that earlier I found error like this I think this might be what you asked for `com.sun.jdi.classnotloadedexception: type has not been loaded occurred while retrieving component type of array` – Gowronga Feb 24 '15 at 17:53
  • @AndréStannek yes sir I did refresh and clean my project – Gowronga Feb 24 '15 at 17:55

2 Answers2

0

About serialVersionUID :

So... basically at some point you may want to serialize your objects and save them somewhere ( like in a file or in db), so that you can deserialize them later. Now, serialVersionUID field is used to know, if you are using the same class definition as that was being used when this object was created an serialized.

So... whenever you make a change to some class that is serializable, you increase the serialVersionUID by 1 so that any seiralized object of old class version is not wrongly deserialized using the current class version.

sarveshseri
  • 13,738
  • 28
  • 47
0

Ladies and gentleman I humbly apologise for my stupidity. I have function load from file and I was loading an OLD library file where list borrow was still not existing there fore no list to add to. And here I have the nullPointer. I was stuck on this for a while but only for my stupidity :) Thanks all for help. You can close the topic

Gowronga
  • 11
  • 4