0

I am trying to make a program that can save data as "data.ser" and retrieve it and output it. However, although it does the task, the program shows that there is an issue with the NullPointerException.

public void loadData(){
        
        ArrayList<Object> deserialized = new ArrayList<Object>();

        try {
            FileInputStream fileIn = new FileInputStream("data.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            deserialized = (ArrayList<Object>)in.readObject();
            in.close();
            fileIn.close();
        } catch(IOException i  ){
            i.printStackTrace();
            
        } catch(ClassNotFoundException c){
            c.printStackTrace();
            
        } catch(NullPointerException e) {
            e.printStackTrace();    
        
    }

The message that always pops up when I run the whole program is

Exception in thread "main" java.lang.NullPointerException
at student_Copy.Main.loadData(Main.java:94)
at student_Copy.loadData.main(loadData.java:9)

There is also a class that retrieves the file

public class loadData {

public static void main(String[] args) {
    Main main = new Main();
    main.loadData();
    
}
Jackman6
  • 19
  • 1
  • You need to print out the data you are sending/receiving from main. Something is returning null. Are you sending null args? – Jayce Sep 12 '20 at 17:12
  • Um I am not sure, I am trying to check if there is anything null but I can't seem to find it. – Jackman6 Sep 13 '20 at 08:39
  • I fixed it! It was all because I had an array with a large index but a lot of them were empty cause U didn't fill it with objects yet. – Jackman6 Sep 13 '20 at 08:49

0 Answers0