views:

412

answers:

6

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!!!!!!!!!!!

+3  A: 

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.

Michael Myers
+5  A: 

The catch clauses need to return something.

public static Message[] getStoredMails(){

     try
     {

            ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser"));
            Message[] array = (Message[]) in.readObject() ;
            System.out.println("Size: "+array.length);      //return array;
            in.close();
            return array;    
     }
     catch(IOException ex)
     {
            ex.printStackTrace();
     }
     catch(ClassNotFoundException ex)
     {
            ex.printStackTrace();
     }    
     return null; //fix  
}
sblundy
A: 

I modified the source. I added "return null" in exception and the for loop the output in the function. And the function gives me the right output but then throws it the exception.

wellenreiter
It looks like your modifications just got clobbered. But anyway. Are you still getting the 'Unresolved compilation problem' or a new one?
sblundy
I get no exception but i cant transfer the working array from the function to the main function :-( i dunno why
wellenreiter
What do you mean by "can't transfer the working array"? Are you or are you not getting an exception? Is the method returning null?
Michael Myers
I get no exception and the method returns null. But the system.out in function prints the right content
wellenreiter
You might want to update you post again, someone overwrote your first edits with spelling and grammatical fixes to your initial post. Looking at the revisions pages, it looks fine to me.
sblundy
done, it should work without the "simple lib".
wellenreiter
i thought this is a lib, but its a tut. i'll read it.
wellenreiter
"I get no exception and the method returns null. But the system.out in function prints the right content." Are you running this exact code? Because I don't see how that's possible.
Michael Myers
Is it working now? I just compiled and ran your code ok on my machine.
sblundy
sblundy, mmyers, thank you a lot. It works now, but i dont know why. I just added "return null" to the exception, but if it returns null, it cant return the array? or iam wrong? sorry about this rookie questions, but i am just learning it. THANK a lot!
wellenreiter
Yes, if an exception gets thrown, you will return null. But see also what I added to my answer.
Michael Myers
+2  A: 

On a different note, have you considered a third-party serializer library?

I'm using Simple right now for a project, and it seems to do stuff just fine with very little effort.

Henrik Paul
+1  A: 

in the exception handling blocks of the getStoredMails method you do not return anything.

Suggested modification:

public static Message[] getStoredMails(){

         try
         {

                ObjectInputStream in = new ObjectInputStream(new FileInputStream("mail.ser"));
                Message[] array = (Message[]) in.readObject() ;
                System.out.println("Size: "+array.length);      //return array;
                in.close();
                return array;    
         }
         catch(IOException ex)
         {
                ex.printStackTrace();
         }
         catch(ClassNotFoundException ex)
         {
                ex.printStackTrace();
         }  

         return null;    
    }
Declan Shanaghy
A: 

This really helps, thanks!

I found this Google custom search engine: searchjava.org. Hope this helps too!