1

Im having an issue where im sending String variables to,from,subject,message and attachment to a server and when I go to place them in a list, the message variable is always null! I have outputted the variable message, and it comes up with what its supposed to but, but as soon as I put it in the list. its shows as null.

private void doSend(String name)
{
    String to = input.nextLine();
    String from = input.nextLine();
    String subject = input.nextLine();
    String message = input.nextLine();
    String attachment = input.nextLine();        

    System.out.println(to);
    System.out.println(from);
    System.out.println(subject);
    System.out.println(message);
    System.out.println(attachment);        

    // stores the message, but not into the mailbox        
    MultiEchoServer.MailBox.add(new Email(to, from,subject, message, attachment));

    System.out.println(MailBox);

    System.out.println("Message Sent to: " + to);
    System.out.println(message);
}

Sample output

pj     // this is the to variable

dsds   // this is the from variable

subject  // this is the subject variable

message  // this is the message variable

[pj dsds subject null]  //this is the Mailbox List

Message Sent to: pj //not part of the error

message // this is the message variable being outputted again to see it it changed

I'm not even sure if anyone can help me, but let me know if you need to see more code thanks!

The Email Class

class Email
{
    private String to, from, subject,  message, attachment;
    int id;        

    public Email(String to ,String from ,String subject, String message, String attachment)
    {
        this.to = to;
        this.from = from;
        this.subject = subject;
        this.message = message;
        this.message = attachment;
    }

    public int id()
    {   
        return(id);
    }

    public String to()
    {
        return(to);
    }

    public String from()
    {   
        return(from);
    }

    public String subject()
    {
        return(subject);
    }

    public String message()
    {
        return(message);
    }
    public String attachment()
    {
        return(attachment);
    }

    public String toString()
    {
        return(to + " " + from + " " + subject + " " + message + "" + attachment);
    }
}
10
  • Show the code for toString() method of MailBox class? Commented Apr 25, 2015 at 17:27
  • 2
    So, The problem is probably in the Email class, or maybe in the MailBox class. Show us their code. You should also learn to use a debugger and figure that out by yourself. Commented Apr 25, 2015 at 17:27
  • 4
    this.message = message; this.message = attachment; You're overriding message by attachment. Commented Apr 25, 2015 at 17:31
  • 2
    @AniketThakur: It's absolutely related to the question. Commented Apr 25, 2015 at 17:32
  • 1
    >Not related to your question but this.message = attachment; Actually, that is related to the question, as it is likely setting message to null. Commented Apr 25, 2015 at 17:33

2 Answers 2

4

There is problem in your Email class constructor. Your assigning message field twice with message and attachment.

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

Comments

0

There are two things going on. As @Dilip pointed out, there is an error in the constructor initializations.

Additionally, attachment is apparently an empty string.

Therefore, in the ctor, String field message is assigned an empty string and String field attachment is left uninitialized, and is therefore null.

In Java, toString() prints null values as the string literal "null".

"" is printed as the value of local variable message and "null" is printed as the value of local variable attachment.

This illustrates why using the same name for local variables, function parameters, and fields can be dangerous.

Additionally, using a String for something like an attachment, which may or may not be a String, depending on your implementation, is possibly a poor choice for this abstraction.

Comments

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.