尝试显示从服务器对象检索到的值时显示空值

Posted

技术标签:

【中文标题】尝试显示从服务器对象检索到的值时显示空值【英文标题】:Displaying null value when attempting to display value retrieved from Server object 【发布时间】:2016-01-11 23:19:57 【问题描述】:

为了更新问题,我已将 Product 和 Customer 类附加在 Server 和 Client 类下方

如果我不清楚,我会提前在这里道歉。我试图在JTextField 中显示ArrayList 中包含的String 值。 String 值确实显示,但 null 值也显示,我想显示 String 值。这是JTextField 中显示的示例String

[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]
[nullDamonTholsonnull0, nullSylvesterStallonenull0, nullMisterTnull0]

在我的程序中,我有客户端用户界面从 Server Object 请求 String 值,然后在 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:
    

这是服务器对象代码

public static void main(String[] args) throws IOException, ClassNotFoundException 

    ServerSocket server = new ServerSocket(67);
    while (true) 
        Socket socket = server.accept();
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());

        DataRequest request = (DataRequest) in.readObject();

        switch (request.getDataType()) 
            case DataRequest.NAME:
                out.writeObject(getCustomerName());
                out.flush();
                break;

            case DataRequest.ID:
                out.writeObject(getCustomerID());
                out.flush();
                break;

            case DataRequest.PASSWORD:
                out.writeObject(getCustomerPassword());
                out.flush();
                break;

            case DataRequest.DESCRIPTION:
                out.writeObject(getProductDescription());
                out.flush();
                break;
            case DataRequest.PRODUCTID:
                out.writeObject(getProductDescription());
                out.flush();
                break;

            case DataRequest.PRICE:
                out.writeObject(getProductDescription());
                out.flush();
                break;

        
    


private static List<Customer> getCustomerName() 
    List<Customer> names = new ArrayList<Customer>();
    in.readObject;
    Customer name = new Customer();
    name.setcFirstName("Damon");

    name.setcLastName("Tholson");

    names.add(name);

    name = new Customer();

    name.setcFirstName("Sylvester");
    name.setcLastName("Stallone");
    names.add(name);

    name = new Customer();

    name.setcFirstName("Mister");
    name.setcLastName("T");
    names.add(name);

    return names;

公共类客户实现可序列化

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
 */

【问题讨论】:

你很快就会想看看Concurrency in Swing Customer 类是什么样的?考虑提供一个runnable example 来证明您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将减少混乱并获得更好的响应 您可以发布客户和产品类代码吗?或者至少,该类的 toString 方法。也许可以通过修改这些方法来解决问题。 你永远不会关闭接受的套接字。 我以为我在 Server 类的 switch 语句的末尾用命令 socket.close(); 关闭了它; 【参考方案1】:

问题出在调用 Customer 类中 toString 方法的输出的 Client 类中。我在 toString 方法中有多个返回,但我只调用一个返回,所以我收到了我调用的变量和我没有调用的变量的空值。所以我的 toString 方法返回以下内容:

@覆盖 公共字符串 toString() 返回 cFirstName + cID + cLastName +cPassword;

但是我的客户端类只调用了该方法中的一个变量

for (客户 customerName : customerNames) jTextAreaOutput.append(customerName.toString());

            

所以我改为调用一个特定的变量

for (客户 customerName : customerNames) jTextAreaOutput.append(customerName.getcFirstName());

            

【讨论】:

以上是关于尝试显示从服务器对象检索到的值时显示空值的主要内容,如果未能解决你的问题,请参考以下文章

从数据库中获取记录时显示空表视图

蜂巢表在显示数据时显示空输出

[] 是在 json 结果中表示空值的有效方法吗?

JSON NSDictionary 在输出到日志时显示空描述

单击搜索结果时显示空的详细信息视图

DataGridView 绑定到已键入的 DataTable 时显示空单元格