0

So I'm making a notepad application that logs date entered, a subject, and a body text field. When I hit my post button, everything appears properly in my ListView, but when I close the application and re-open it, only the date remains intact and the other two values are NULL. Below is the code I'm using.

public class LogList implements Serializable {
private String logDate;
private String logBody;
private String logSubject;

public LogList(String date, String LogBody, String LogSubject){
    super();
    this.logDate = date;
    this.logBody = logBody;
    this.logSubject = logSubject;
}

Back in my main class, I have my method that is supposed to save the three values into an ArrayList lts.

private void saveInFile(String subject_text, String date, String body_text ){
    LogList lt = new LogList(date, subject_text, body_text);

    lts.add(lt);

    saveAllLogs();
}

Now if I change the order of the values in my new LogList, only the first one will be properly displayed after I close my app and reopen it. The following are my saveAllLogs method and my loadFromFile method.

private ArrayList<String> loadFromFile(){
    ArrayList<String> logs = new ArrayList<String>();
    try {
        FileInputStream fis = openFileInput(FILENAME);
        ObjectInputStream ois = new ObjectInputStream(fis);
        while (true) {
            LogList lt = (LogList) ois.readObject();
            logs.add(lt.toString());
            lts.add(lt);
        }
    } 
    catch (FileNotFoundException e) { e.printStackTrace(); }
    catch (IOException e) { e.printStackTrace(); }
    catch (ClassNotFoundException e) { e.printStackTrace(); }
    return logs;
}

private void saveAllLogs() {
    try {
        FileOutputStream fos = openFileOutput(FILENAME, 0);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        for (LogList lti : lts) {
            oos.writeObject(lti);
        }
        fos.close();
    }
    catch (FileNotFoundException e) { e.printStackTrace(); }
    catch (IOException e) { e.printStackTrace(); }
}

Any help would be greatly appreciated!

2
  • "date" is correct. Parameters LogBody and LogSubject are never used, because the case of your variables ("logBody") is different from the case of the parameter ("LogBody"). Commented Sep 21, 2013 at 20:58
  • I hate that such a small mistake has cost me hours trying to fix. Thanks for the help! Switched the variables and it now works properly. Commented Sep 22, 2013 at 17:29

1 Answer 1

1

For one thing,

public LogList(String date, String LogBody, String LogSubject){
    super();
    this.logDate = date;
    this.logBody = logBody;
    this.logSubject = logSubject;
}

Seems wrong. As you have capitalized argument names, but you're setting the members with lowercase names.

Do you mean:

public LogList(String date, String logBody, String logSubject){
    super();
    this.logDate = date;
    this.logBody = logBody;
    this.logSubject = logSubject;
}

EDIT: Trivial thing, not impacting your code: You don't need your call to super() in your constructor as you're not extending any class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I switched the variables and it now works properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.