Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when trying to save file to hardrive Level7. #67

Closed
Marcon2509 opened this issue Aug 24, 2020 · 9 comments
Closed

Comments

@Marcon2509
Copy link

Marcon2509 commented Aug 24, 2020

image

I am attempting Level 7. Save and I am trying to use serialization to save an array which stores my list that is printed out. However my method saveData(ArrayList list) gets the nullPointer error as shown in the image above. I am using intelliJ

Below is my SaveData method (Note not sure why the insert code button isnt working)

public void saveData(ArrayList<Task> listToSave) {
      
  FileOutputStream fos = null;
       
 try {
            
fos = new FileOutputStream(dataFile);

        } catch (FileNotFoundException e) {
          
  
e.printStackTrace();

        } finally {

            if (fos != null) {

                ObjectOutputStream oos = null;
                try {

                    oos = new ObjectOutputStream(fos);

                    if (oos != null) {

                        oos.writeObject(listToSave);

                    }
                    assert oos != null;
                    oos.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
@damithc
Copy link
Contributor

damithc commented Aug 24, 2020

Note not sure why the insert code button isnt working)

I updated your post to use the triple-back-tick notation for fenced code blocks.

See https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks

In think others need to know exactly which statement is causing the NPE i.e., which statement is at Storage.java:36

@Marcon2509
Copy link
Author

image

The image above shows the nullPointerException is coming from fos = new FileOutputStream(dataFile);

@wakululuu
Copy link

Have you handled the case where dataFile doesn't exist?

@Marcon2509
Copy link
Author

Marcon2509 commented Aug 24, 2020

image
i think i do in the initFile method @wakululuu

@wakululuu
Copy link

Could you try printing the dataFile object and see what the output is?

@Marcon2509
Copy link
Author

Marcon2509 commented Aug 24, 2020

image
image
image

@wakululuu added the system.out.println(datafile) and in the picture shows i intialise it at the begining

@wakululuu
Copy link

wakululuu commented Aug 24, 2020

Sorry for the unclear suggestion previously but maybe print it right before fos = new FileOutputStream(dataFile); instead. Could you also show the part of your code where you call saveData?

@zhiayang
Copy link

doesn't the error writing aborted; java.io.NotSerialisableException: ToDo suggest that your class ToDo does not implement the Serialisable interface

@Marcon2509
Copy link
Author

@zhiayang ah yes my bad that was the problem thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants