0

so i've been sitting above this code for a while , ready the NullPointerException threads, and still can't figure out what is going wrong in my code, so i turn to you.

public class Main {
public static void main(String[] args){     
    /* Making catalog, loading last state */
    Collection catalog = new Collection();      
    try {
        catalog.readFromFile();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }
catalog.addShip(new Ship("ABC123", "John", "Suzuki", 50));
 }
}

And my Collection class looks like this:

public class Collection {
    private List<Ship> shipList;
    private String fileName = "catalog.txt";
    private int income;
    private int space;

public Collection() {
    shipList = new ArrayList<Ship>();
    income = 0;
    space = 500;
    File f = new File("catalog.txt");
    if(!f.exists()) {
        try {
            f.createNewFile();
            writeToFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public void addShip(Ship SHIP){
    space -= SHIP.LENGTH;
    income += SHIP.COST;
    shipList.add(SHIP);
}

public Ship getShip(int INDEX){
    return shipList.get(INDEX);
}

public void writeToFile() throws IOException {
    FileOutputStream f = new FileOutputStream(fileName);
    ObjectOutputStream out = new ObjectOutputStream(f);
    out.writeObject(shipList);
    out.close();        
    }

@SuppressWarnings("unchecked")
public void readFromFile() throws IOException, ClassNotFoundException {
    FileInputStream f = new FileInputStream(fileName);
    ObjectInputStream in = new ObjectInputStream(f);
    shipList = (ArrayList<Ship>)in.readObject();
    in.close();
}

public int getIncome(){
    return income;
}

public int getSpace(){
    return space;
}
}

My problem is, when i call in main catalog.addship() i get nullptr error. After following the console errors, it says i get the nullptrexc when i call the addShip() on the catalog, following from there i get the error when i add() a Ship to the Collection's shipList. So what i concluded, it is because the shipList in the Collection is uninitialized. But in the constructor i write shipList = new ArrayList<Ship>(); so it is clearly initialized.

The exception stacktrace is the following:

Exception in thread "main" java.lang.NullPointerException
    at collection.Collection.addShip(Collection.java:31)
    at main.Main.main(Main.java:100)
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Broccholio
  • 63
  • 1
  • 7
  • The trace: Exception in thread "main" java.lang.NullPointerException at collection.Collection.addShip(Collection.java:31) at main.Main.main(Main.java:100) Ship: https://gist.github.com/broccholio/c85f443829e6500d52e7 The code itself is longer, but i cut out the irrelevant parts, like creating Jframes and buttons. – Broccholio Nov 28 '15 at 02:20
  • Apparently deserialization of your input file returns null. Probably you created your file when your `writeToFile` code worked bad. Try to delete `catalog.txt` and rerun your code. – Tagir Valeev Nov 28 '15 at 02:23
  • Obviously `catalog` is still null, because you caught and ignored the exceptions. – user207421 Nov 28 '15 at 02:25
  • 2
    @EJP, even if exception occurs, catalog is constructed out of the try-catch block, so it cannot be null. Also stacktrace posted by OP clearly says that `shipList` is null. – Tagir Valeev Nov 28 '15 at 02:26
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Nov 30 '15 at 21:32

1 Answers1

6

In your main method, you initialize the ArrayList properly. But then, you make a

catalog.readFromFile()

call. In the readFromFile() method, you re-initialize the ArrayList

shipList = (ArrayList<Ship>)in.readObject();

the in.readObject() is returning null. That is why your shipList variable is null.

Hope this helps!

Kabir
  • 860
  • 5
  • 11
  • Oh i see, thank you! The concept of reading the object from file, felt like if i read an empty file, i still get the object, just created with the default constructor. – Broccholio Nov 28 '15 at 02:26