1

I have a client/server program built. The object of the program is to send an array of data across to the client using sockets, display the data into a table then be able to edit it and send it back for the server to store. The program compiles fine but when run the client doesn't output the array to the table. It displays an error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to client.Network
    at client.MyTableModel.getValueAt(MyTableModel.java:77)



MyTableModel class: 

package client;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel {

        void setData(ArrayList<Network> serversArray) {
        data = serversArray;
        System.out.print(this.data +"\n");
        System.out.print(serversArray + "\n");

    }    

  ArrayList<Network> data = new ArrayList();
        String[] headers = {"NetworkID","Nodes","Hubs","Switches","Structure","Country","Status"};


    @Override
    public int getColumnCount() {
        return headers.length;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public void setValueAt(Object value, int row, int col) {

        switch (col) {
                case 0:
                    data.get(row).setNetworkID((int) value);
                    break;
                case 1:
                    data.get(row).setNodes((int) value);
                    break;
                case 2:
                    data.get(row).setHubs((int) value);
                    break;
                case 3:
                    data.get(row).setSwitches((int) value);
                    break;
                case 4:
                    data.get(row).setStructure("");
                    break;
                case 5:
                    data.get(row).setCountry("");
                    break;
                case 6:
                    data.get(row).setStatus("");
                    break;
                }

                fireTableCellUpdated(row, col);
    }
    @Override
    public String getColumnName(int col)
    { return headers[col];  }


    @Override
    public Object getValueAt(int row, int col) {

      switch (col) {
      case 0:
        return data.get(row).getNetworkID();
      case 1:
        return data.get(row).getNodes();
      case 2:
        return data.get(row).getHubs();
      case 3:
        return data.get(row).getSwitches();
      case 4:
        return data.get(row).getStructure();
      case 5:
        return data.get(row).getCountry();
      case 6:
        return data.get(row).getStatus();
      }
    return 0;
   }
}

Network class:

public class Network implements Serializable{

        private int NetworkID;
        private int Nodes;
        private int Hubs;
        private int Switches;
        private String Structure;
        private String Country;
        private String Status;


        public int getNetworkID(){
           System.out.println(this.NetworkID);
            return this.NetworkID;
        }

        public int getNodes(){
            return this.Nodes;
        }

        public int getHubs(){
            return this.Hubs;
        }

        public int getSwitches(){
            return this.Switches;
        }

        public String getStructure(){
            return this.Structure;
        }

        public String getCountry(){
            return this.Country;
        }

        public String getStatus(){
            return this.Status;
        }

        public void setNetworkID(int networkID){
            this.NetworkID = networkID;
            System.out.print(this.NetworkID);
        }

        public void setNodes(int nodes){
            this.Nodes = nodes;
        }

        public void setHubs(int hubs){
            this.Hubs = hubs;
        }

        public void setSwitches(int switches){
            this.Switches = switches;
        }

        public void setStructure(String structure){
            this.Structure = structure;
        }

        public void setCountry(String country){
            this.Country = country;
        }

        public void setStatus(String status){
            this.Status = status;
        }


    }

Any assistance would be much appreciated!

update:

private void displayNetworksActionPerformed(java.awt.event.ActionEvent evt) {                                                
   pw.println("SendCSV");

  // receieve data array from server     


  try {
      serversArray =  (ArrayList<Network>)ois.readObject();
      System.out.print(serversArray);

      statusBar.setText("Object Recieved from Server!");
  } catch (ClassNotFoundException ex) {
      statusBar.setText("Didnt Recieve Object");
  } catch (IOException ex) {
     statusBar.setText("Unable to Request");
  }

   // 4. update jtable
  this.myTableModel.setData(serversArray);
  this.myTableModel.fireTableDataChanged();


 }            

Update: I am still having an issue with this, is there any chance someone could assist with a solution?

B. Durrant
  • 11
  • 2

0 Answers0