DefaultTableModel中的getColumnClass()在java中抛出NullPointerException

时间:2017-10-31 06:51:59

标签: java swing nullpointerexception

我尝试在DefaultTableModel中添加数据,如下面的代码:

public class ConfigureCustomer extends JPanel {

    private static final int CHECK_COL = 1;
    private Object[][] DATA;
    private final String[] COLUMNS = { "Customer", "Selected Customer" };
    private DataModel dataModel;
    private JTable table;
    private DefaultListSelectionModel selectionModel;

    public ConfigureCustomer() {
        super(new BorderLayout());
        ArrayList<String> items = new ArrayList<String>();
        JSONParser parser = new JSONParser();
        try {

            Object obj = null;
            try {
                obj = parser.parse(new FileReader(new File("json/customer_list.json")));
            } catch (org.json.simple.parser.ParseException e1) {
                e1.printStackTrace();
            }
            JSONObject jsonObject = (JSONObject) obj;
            JSONArray listOfBranches = (JSONArray) jsonObject.get("customers");
            for (int i = 0; i < listOfBranches.size(); i++) {
                JSONObject item = (JSONObject)listOfBranches.get(i);
                Set keys = item.keySet();
                Iterator a = keys.iterator();
                while(a.hasNext()) {
                    String key = (String)a.next();
                    System.out.print("key : "+key);
                    items.add(key);
                }
                System.out.println("Customer :" + listOfBranches.get(i)+"\n item "+item);
            }

            System.out.println("items array size :" + items.size());

            String[] customerArr = new String[items.size()];
            customerArr = items.toArray(customerArr);

            System.out.println("customerArr array size :" + customerArr.length);

            DATA = new Object[customerArr.length][customerArr.length];
            for (int i = 0; i < customerArr.length; i++) {
                System.out.println("customerArr array size :" + customerArr.length);
                    DATA[i][i] = new Object[]{customerArr[i], Boolean.FALSE};
                    dataModel = new DataModel(DATA, COLUMNS);
                    table = new JTable(dataModel);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        add(new JScrollPane(table));
        add(new ControlPanel(), BorderLayout.SOUTH);
        table.setPreferredScrollableViewportSize(new Dimension(250, 175));
        selectionModel = (DefaultListSelectionModel) table.getSelectionModel();
        // add(new CheckABunch());

        tableUIChanges();

    }

    private void tableUIChanges() {
        table.getTableHeader().setFont(new Font("SansSerif", Font.BOLD, 18));

        // Table header alignment, backgorund, text color
        for (int i = 0; i < COLUMNS.length; i++) {
            TableCellRenderer renderer = new DefaultTableCellRenderer();
            ((DefaultTableCellRenderer) renderer).setHorizontalAlignment(SwingConstants.CENTER);
            table.getColumnModel().getColumn(i).setHeaderRenderer(renderer);
            ((DefaultTableCellRenderer) renderer).setBackground(new Color(219, 6, 6));
            ((DefaultTableCellRenderer) renderer).setForeground(Color.WHITE);
        }

        DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
        rightRenderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        table.getColumn(COLUMNS[0]).setCellRenderer( rightRenderer );

        Font font = new Font("SansSerif", Font.PLAIN, 18);
        table.setFont(font);
        table.setRowHeight(30);

    }

    private class DataModel extends DefaultTableModel {

        public DataModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == CHECK_COL) {
                return getValueAt(0, CHECK_COL).getClass();
            }
            return super.getColumnClass(columnIndex);
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return column == CHECK_COL;
        }
    }

    private class ControlPanel extends JPanel {

        public ControlPanel() {
            this.add(new JLabel("Selection:"));
            this.add(new JButton(new SelectionAction("Clear", false)));
            this.add(new JButton(new SelectionAction("Finish", true)));
            this.add(new JButton(new SelectionAction("Back", true)));
        }
    }

    private class SelectionAction extends AbstractAction {

        boolean value;
        String name;

        public SelectionAction(String name, boolean value) {
            super(name);
            this.name = name;
            this.value = value;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (name.equalsIgnoreCase("Finish")) {
                boolean isSelected = false;
                for (int i = 0; i < dataModel.getRowCount(); i++) {
                    if (selectionModel.isSelectedIndex(i)) {
                        System.out.println("selected index " + " " + i + " " + name);
                        isSelected = true;
                        break;
                    }
                }

                if (isSelected) {
                    JPanel parent = (JPanel) getParent();
                    CardLayout cardLayout = (CardLayout) parent.getLayout();
                    cardLayout.next(parent);
                } else {
                    JOptionPane.showMessageDialog(null, "Select customer to proceed", "InfoBox: " + "warning",
                            JOptionPane.INFORMATION_MESSAGE);
                }
            } else if (name.equalsIgnoreCase("Back")) {
                JPanel parent = (JPanel) getParent();
                CardLayout cardLayout = (CardLayout) parent.getLayout();
                cardLayout.previous(parent);
            } else {
                for (int i = 0; i < dataModel.getRowCount(); i++) {
                    if (selectionModel.isSelectedIndex(i)) {
                        System.out.println("selected index " + " " + i + " " + name);
                    }
                    // if (selectionModel.isSelectedIndex(i)) {
                    System.out.println("actionPerformed value : " + value + " " + i + " " + CHECK_COL + " " + name);
                    dataModel.setValueAt(value, i, CHECK_COL);
                    // }
                }
            }
        }
    }

}

但它会抛出NullPointerException

在我使用相同代码中的静态Object[][]项之前。

它对我有用,但在添加动态项后会抛出NullPointerException

您能否就我如何解决这个问题向我提出建议?

0 个答案:

没有答案