I want to save and store simple mail objects via serializing, but I get always an error and I can’t find where it is.
package sotring; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import com.sun.org.apache.bcel.internal.generic.INEG; public class storeing { public static void storeMail(Message[] mail){ try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream('mail.ser')); out.writeObject(mail); out.flush(); out.close(); } catch (IOException e) { } } public static Message[] getStoredMails(){ try { ObjectInputStream in = new ObjectInputStream(new FileInputStream('mail.ser')); Message[] array = (Message[]) in.readObject() ; for (int i=0; i< array.length;i++) System.out.println('EMail von:'+ array[i].getSender() + ' an ' + array[i].getReceiver()+ ' Emailbetreff: '+ array[i].getBetreff() + ' Inhalt: ' + array[i].getContent()); System.out.println('Size: '+array.length); //return array; in.close(); return array; } catch(IOException ex) { ex.printStackTrace(); return null; } catch(ClassNotFoundException ex) { ex.printStackTrace(); return null; } } public static void main(String[] args) { User user1 = new User('User1', 'geheim'); User user2 = new User('User2', 'geheim'); Message email1 = new Message(user1.getName(), user2.getName(), 'Test', 'Fooobaaaar'); Message email2 = new Message(user1.getName(), user2.getName(), 'Test2', 'Woohoo'); Message email3 = new Message(user1.getName(), user2.getName(), 'Test3', 'Okay =) '); Message [] mails = {email1, email2, email3}; storeMail(mails); Message[] restored = getStoredMails();; } }
Here are the user and message class
public class Message implements Serializable{ static final long serialVersionUID = -1L; private String receiver; //Empfänger private String sender; //Absender private String Betreff; private String content; private String timestamp; private String getDateTime() { DateFormat dateFormat = new SimpleDateFormat('yyyy/MM/dd HH:mm:ss'); Date date = new Date(); return dateFormat.format(date); } Message (String receiver, String sender, String Betreff, String content) { this.Betreff= Betreff; this.receiver = receiver; this.sender = sender; this.content = content; this.timestamp = getDateTime(); } Message() { // Just for loaded msg } public String getReceiver() { return receiver; } public void setReceiver(String receiver) { this.receiver = receiver; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getBetreff() { return Betreff; } public void setBetreff(String betreff) { Betreff = betreff; } public String getContent() { return content; } public String getTime() { return timestamp; } public void setContent(String content) { this.content = content; } } public class User implements Serializable{ static final long serialVersionUID = -1L; private String username; //unique Username private String ipadress; //changes everytime private String password; //Password private int unreadMsg; //Unread Messages private static int usercount; private boolean online; public String getName(){ return username; } public boolean Status() { return online; } public void setOnline() { this.online = true; } public void setOffline() { this.online = false; } User(String username,String password){ if (true){ this.username = username; this.password = password; usercount++; } else System.out.print('Username not availiable'); } public void changePassword(String newpassword){ password = newpassword; } public void setIP(String newip){ ipadress = newip; } public String getIP(){ if (ipadress.length() >= 7){ return ipadress; } else return 'ip address not set.'; } public int getUnreadMsg() { return unreadMsg; } }
Here is the exception:
exception in thread 'main' java.lang.Error: Unresolved compilation problem: This method must return a result of type Message[] at sotring.storeing.getStoredMails(storeing.java:22) at sotring.storeing.main(storeing.java:57)
THANK YOU FOR YOUR HELP!!!!!!!!!!!
If an exception occurs, you never get to the return statement in getStoredMails. You need to either throw the exception you catch (possibly wrapping it in another more descriptive exception) or just return null at the end of the method. It really depends on what you want to do if there’s an error.
Oh, and your in.close() should be in a finally block. Otherwise, it is possible that you could read the data fine but then throw it away if you can’t close the stream.