Java 尝试显示从服务器对象检索的值时显示空值
为了更新这个问题,我附加了产品和客户类,它们位于服务器和客户机类下面 如果我不清楚,我会提前道歉。我试图在Java 尝试显示从服务器对象检索的值时显示空值,java,swing,Java,Swing,为了更新这个问题,我附加了产品和客户类,它们位于服务器和客户机类下面 如果我不清楚,我会提前道歉。我试图在JTextField中显示ArrayList中包含的String值。字符串值确实显示,但null值也显示,我想显示字符串值。这是显示在JTextField [nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0] [nullDamonTholsonnull0, nullSylvesterStallonenull
JTextField
中显示ArrayList
中包含的String
值。字符串
值确实显示,但null
值也显示,我想显示字符串
值。这是显示在JTextField
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
在我的程序中,我的客户端用户界面从服务器
对象
请求字符串
值,然后在JTextField
中显示它们。以下是客户端接口代码:
private void jButtonDisplaycNamesActionPerformed(java.awt.event.ActionEvent evt) {
{
try {
Socket socket = new Socket("localhost", 67);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
DataRequest request = new DataRequest(DataRequest.NAME);
out.writeObject(request);
out.flush();
List<Customer> customerNames = (List<Customer>) in.readObject();
jTextAreaOutput.setText("");
for (Customer customerName : customerNames) {
jTextAreaOutput.append(customerNames.toString());
jTextAreaOutput.append("\n");
}
socket.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "unable to contact Server");
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} // TODO add your handling code here:
}
// TODO add your handling code here:
}
private void jButtonDisplaycIDActionPerformed(java.awt.event.ActionEvent evt) {
try {
Socket socket = new Socket("localhost", 67);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
DataRequest request = new DataRequest(DataRequest.ID);
out.writeObject(request);
out.flush();
List<Customer> customerIDs = (List<Customer>) in.readObject();
jTextAreaOutput.setText("");
for (Customer customerID : customerIDs) {
jTextAreaOutput.append(customerIDs.toString());
jTextAreaOutput.append("\n");
}
socket.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "unable to contact Server");
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} // TODO add your handling code here:
}
private void jButtonDisplaypIDActionPerformed(java.awt.event.ActionEvent evt) {
{
try {
Socket socket = new Socket("localhost", 67);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
DataRequest request = new DataRequest(DataRequest.PRODUCTID);
out.writeObject(request);
out.flush();
List<Product> productIDs = (List<Product>) in.readObject();
jTextAreaOutput.setText("");
for (Product procuctID : productIDs) {
jTextAreaOutput.append(productIDs.toString());
jTextAreaOutput.append("\n");
}
socket.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "unable to contact Server");
Logger.getLogger(ClientGUI.class.getName()).log(Level.SEVERE, null, ex);
} // TODO add your handling code here:
}
}
公共类产品实现可序列化{
private String pID;
private String pDescription;
private String pPrice;
private int dataType;
/**
* @return the pID
*/
public String getpID() {
return pID;
}
/**
* @param pID the pID to set
*/
public void setpID(String pID) {
this.pID = pID;
}
/**
* @return the pName
*/
/**
* @return the pDescription
*/
public String getpDescription() {
return pDescription;
}
/**
* @param pDescription the pDescription to set
*/
public void setpDescription(String pDescription) {
this.pDescription = pDescription;
}
/**
* @return the pPrice
*/
public String getpPrice() {
return pPrice;
}
/**
* @param pPrice the pPrice to set
*/
public void setpPrice(String pPrice) {
this.pPrice = pPrice;
}
public int getDataType() {
return dataType;
}
/**
* @param dataType the dataType to set
*/
public void setDataType(int dataType) {
this.dataType = dataType;
}
@Override
public String toString() {
return pID + pDescription + pPrice;
}
/**
* @return the dataType
*/
}问题出在客户机类中,该类调用Customer类中toString方法的输出。我在toString方法中有多个返回,但是我只调用了一个返回,所以我接收了我调用的变量,以及我没有调用的变量的空值。因此,我的toString方法返回以下内容: @凌驾 公共字符串toString() { 返回cFirstName+cID+clatname+cPassword; } 然而,我的客户机类只调用该方法中的一个变量 for(客户名称:客户名称) { jTextAreaOutput.append(customerName.toString()) 所以我改为调用一个特定的变量 for(客户名称:客户名称) { append(customerName.getcFirstName())
你很快就会想看看<代码>客户>代码>什么样的?考虑提供一个演示你的问题的代码。这不是一个代码转储,而是你正在做的一个例子,它突出了你所面临的问题。这将导致更少的混乱和更好的响应,你可以发布客户和PR。产品类代码?或者至少是该类的toString方法。也许通过修改该方法可以解决问题。您永远不会关闭接受的套接字。我想我是在服务器类的switch语句末尾用命令socket.close()关闭它的;
private String cID;
private String cFirstName;
private String cLastName;
private String cPassword;
private int dataType;
/**
* @return the cID
*/
public String getcID() {
return cID;
}
/**
* @param cID the cID to set
*/
public void setcID(String cID) {
this.cID = cID;
}
/**
* @return the cFirstName
*/
public String getcFirstName() {
return cFirstName;
}
/**
* @param cFirstName the cFirstName to set
*/
public void setcFirstName(String cFirstName) {
this.cFirstName = cFirstName;
}
/**
* @return the cLastName
*/
public String getcLastName() {
return cLastName;
}
/**
* @param cLastName the cLastName to set
*/
public void setcLastName(String cLastName) {
this.cLastName = cLastName;
}
/**
* @return the cPassword
*/
public String getcPassword() {
return cPassword;
}
/**
* @param cPassword the cPassword to set
*/
public void setcPassword(String cPassword) {
this.cPassword = cPassword;
}
/**
* @return the dataType
*/
public int getDataType() {
return dataType;
}
/**
* @param dataType the dataType to set
*/
public void setDataType(int dataType) {
this.dataType = dataType;
}
@Override
public String toString() {
return cID + cFirstName + cLastName + cPassword + dataType ;
}
private String pID;
private String pDescription;
private String pPrice;
private int dataType;
/**
* @return the pID
*/
public String getpID() {
return pID;
}
/**
* @param pID the pID to set
*/
public void setpID(String pID) {
this.pID = pID;
}
/**
* @return the pName
*/
/**
* @return the pDescription
*/
public String getpDescription() {
return pDescription;
}
/**
* @param pDescription the pDescription to set
*/
public void setpDescription(String pDescription) {
this.pDescription = pDescription;
}
/**
* @return the pPrice
*/
public String getpPrice() {
return pPrice;
}
/**
* @param pPrice the pPrice to set
*/
public void setpPrice(String pPrice) {
this.pPrice = pPrice;
}
public int getDataType() {
return dataType;
}
/**
* @param dataType the dataType to set
*/
public void setDataType(int dataType) {
this.dataType = dataType;
}
@Override
public String toString() {
return pID + pDescription + pPrice;
}
/**
* @return the dataType
*/
}
}