0

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

2
  • Can someone remove the User and Message listing? It's kind of awkward to have to scroll all the way past them. Commented Oct 6, 2008 at 20:36
  • You should avoid using com.sun classes if at all possible. They may break in a future release or not even be present in a different VM. Commented Oct 7, 2008 at 2:53

5 Answers 5

5

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  
}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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.

Comments

1

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;    
    }

Comments

0

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.

9 Comments

It looks like your modifications just got clobbered. But anyway. Are you still getting the 'Unresolved compilation problem' or a new one?
I get no exception but i cant transfer the working array from the function to the main function :-( i dunno why
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?
I get no exception and the method returns null. But the system.out in function prints the right content
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.
|

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.