我有一个问题,我发送字符串变量,从主题,消息和附件到服务器,当我把它们放在列表中时,消息变量总是为空!我已经输出了变量消息,它提出了它应该的但是,但是只要我把它放在列表中。它显示为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);
}
示例输出
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
我甚至不确定是否有人可以帮助我,但如果您需要查看更多代码,请告诉我们!
电子邮件类
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);
}
}
答案 0 :(得分:4)
Email
班constructor
存在问题。您使用message
和message
两次分配attachment
字段。
答案 1 :(得分:0)
有两件事正在发生。正如@Dilip指出的那样,构造函数初始化中存在错误。
此外,self.firstField.placeholder = @"First";
self.firstField.textAlignment = NSTextAlignmentCenter;
self.firstField.font = [UIFont fontWithName:@"Helvetica Neue Light" size:50.0];
self.secondField.placeholder = @"Second";
self.secondField.textAlignment = NSTextAlignmentCenter;
self.secondField.font = [UIFont fontWithName:@"Helvetica Neue Light" size:50.0];
self.thirdField.placeholder = @"Third";
self.thirdField.textAlignment = NSTextAlignmentCenter;
self.thirdField.font = [UIFont fontWithName:@"Helvetica Neue Light" size:50.0];
[self.scrollView addSubview:self.firstField];
[self.firstField autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeRight];
[self.firstField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.firstField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];
[self.scrollView addSubview:self.secondField];
[self.secondField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.firstField];
[self.secondField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.secondField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];
[self.scrollView addSubview:self.thirdField];
[self.thirdField autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.secondField];
[self.thirdField autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeLeft];
[self.thirdField autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.scrollView];
[self.thirdField autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:self.scrollView];
self.automaticallyAdjustsScrollViewInsets = NO;
显然是一个空字符串。
因此,在ctor中,字符串字段attachment
被分配一个空字符串,而字符串字段message
未被初始化,因此attachment
。
在Java中,null
打印空值作为字符串文字“null”。
“”打印为局部变量toString()
的值,“null”打印为局部变量message
的值。
这说明了为什么对局部变量,函数参数和字段使用相同的名称可能是危险的。
此外,根据您的实现,使用String作为附件之类的东西,可能是也可能不是String,对于这种抽象可能是一个糟糕的选择。