Mar Oli
Mar Oli

Reputation: 67

Android: File not found exception, it worked in the first time?

I am performing a project, where so far in the discipline, we can not use database to persist the data. I am persisting the data in .tmp files. The first time I persisted the list of doctors, and it worked, but now that I'm trying to persist the patient user data, but this error happens, that file is not found.

These are my load, anda save methods in the class "SharedResources":

public void loadUserPatient(Context context) {
    FileInputStream fis1;
    try {

        fis1 = context.openFileInput("patient.tmp");

        ObjectInputStream ois = new
                ObjectInputStream(fis1);
        userPatient = (UserPatient) ois.readObject();
        ois.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    } catch(ClassNotFoundException e) {
        e.printStackTrace();
    }
}


public void saveUserPatient(Context context) {
    FileOutputStream fos1;
    try {
        fos1 = context.openFileOutput("patient.tmp",
                Context.MODE_PRIVATE);
        ObjectOutputStream oos =
                new ObjectOutputStream(fos1);
        oos.writeObject(userPatient);
        oos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

here is the whole class: https://ideone.com/f3c74u

the error is happening on line 16 of MainActivity:

SharedResources.getInstance().loadUserPatient(this);

here is the whole class "Main": https://ideone.com/OyiljP

And I think this error is ocurring because of the 52nd line of the UserPatientAdd class:

SharedResources.getInstance().getUserPatient(); 

because when I work with an ArrayList, I put an add at the end of the line, like:SharedResources.getInstance().getDoctors().add(doctor);

And I get confused on how to proceed when I deal only with a user. This is the whole UserPatientAdd class: https://ideone.com/clUSa3

How can I solve this problem?

Upvotes: 0

Views: 91

Answers (1)

diegoveloper
diegoveloper

Reputation: 103441

You need to set the UserPatient using something like this

In your SharedResources class, create a new method:

 public void setUserPatient(UserPatient user) {
        userPatient = user;
    }

Then in your UserPatientAdd class set the new object:

 UserPatient userPatient = new UserPatient (birth, name, bloodType, bloodPressure, cbpm, vacinesTaken, vacinesToBeTaken,
            allergies,weight, height, surgeries, desease);


SharedResources.getInstance().setUserPatient(userPatient);

Done

Upvotes: 1

Related Questions