[将Collumn添加到JTable时为NullPointerException

问题描述 投票:0回答:1

我正在编写一个程序,当输入从业者编号(FHIR HAPI)时会显示患者记录,但这并不重要。我想显示基本的患者详细信息,然后通过复选框允许用户向JTable添加一个额外的列,以显示其详细信息。根据复选框的状态,应该可以添加和删除此额外的列。但是,当我尝试使用DefaultTableModel变量执行model.addColumn()方法时,总是会收到NullPointer异常。最后一种方法是创建问题。我还向其他调用的方法提供了代码。我尝试过使用各种类型的数组(对象和字符串)作为值,甚至是数组列表。您还将看到在方法getAllPatientObservationValues()的第二段代码中,我执行了一个println来查看它是否进入了方法,但没有。任何帮助将不胜感激。

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.text.ParseException;

public class GUIManager extends JFrame{

    private Operations op;

    private JPanel pnPnlMain;

    private JPanel pnPnlTop;
    private JCheckBox cbChkCholesterol;

    private JPanel pnPnlMiddle;
    private JTable tblLeft;
    private DefaultTableModel tblLeftModel;
    private JTable tblRight;
    private DefaultTableModel tblRightModel;
    private JButton btBtnAdd;
    private JButton btBtnRemove;

    private JPanel pnPnlBottom;
    private JButton btBtnExit;
    private JLabel lbLblTime;
    private JTextField tfTxtSeconds;
    private JLabel lbLblThreshhold;
    private JTextField tfTxtThreshold;

    public GUIManager(Operations inOP)
    {
        setSize(1000, 650);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("FHIR Patient Monitor");

        pnPnlMain = new JPanel();
        pnPnlMain.setBorder( BorderFactory.createTitledBorder( "FHIR" ) );
        GridBagLayout gbPnlMain = new GridBagLayout();
        GridBagConstraints gbcPnlMain = new GridBagConstraints();
        pnPnlMain.setLayout( gbPnlMain );

        pnPnlTop = new JPanel();
        pnPnlTop.setBorder( BorderFactory.createTitledBorder( "Column Details" ) );
        GridBagLayout gbPnlTop = new GridBagLayout();
        GridBagConstraints gbcPnlTop = new GridBagConstraints();
        pnPnlTop.setLayout( gbPnlTop );

        cbChkCholesterol = new JCheckBox( "Cholesterol");
        gbcPnlTop.gridx = 2;
        gbcPnlTop.gridy = 1;
        gbcPnlTop.gridwidth = 6;
        gbcPnlTop.gridheight = 1;
        gbcPnlTop.fill = GridBagConstraints.BOTH;
        gbcPnlTop.weightx = 1;
        gbcPnlTop.weighty = 0;
        gbcPnlTop.anchor = GridBagConstraints.NORTH;
        gbPnlTop.setConstraints( cbChkCholesterol, gbcPnlTop );
        pnPnlTop.add( cbChkCholesterol );
        gbcPnlMain.gridx = 0;
        gbcPnlMain.gridy = 0;
        gbcPnlMain.gridwidth = 20;
        gbcPnlMain.gridheight = 4;
        gbcPnlMain.fill = GridBagConstraints.BOTH;
        gbcPnlMain.weightx = 1;
        gbcPnlMain.weighty = 0;
        gbcPnlMain.anchor = GridBagConstraints.NORTH;
        gbPnlMain.setConstraints( pnPnlTop, gbcPnlMain );
        pnPnlMain.add( pnPnlTop );

        pnPnlMiddle = new JPanel();
        pnPnlMiddle.setBorder( BorderFactory.createTitledBorder( "Tables" ) );
        GridBagLayout gbPnlMiddle = new GridBagLayout();
        GridBagConstraints gbcPnlMiddle = new GridBagConstraints();
        pnPnlMiddle.setLayout( gbPnlMiddle );

        String [][]dataTblLeft = new String[1][2];
        String []colsTblLeft = new String[] { "ID", "Full Name" };
        tblLeftModel = new DefaultTableModel(dataTblLeft, colsTblLeft);
        tblLeft = new JTable(tblLeftModel);
        tblLeftModel.removeRow(0);
        JScrollPane scpLeftTable = new JScrollPane(tblLeft);
        scpLeftTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scpLeftTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        gbcPnlMiddle.gridx = 0;
        gbcPnlMiddle.gridy = 0;
        gbcPnlMiddle.gridwidth = 9;
        gbcPnlMiddle.gridheight = 12;
        gbcPnlMiddle.fill = GridBagConstraints.NONE;
        gbcPnlMiddle.weightx = 1;
        gbcPnlMiddle.weighty = 1;
        gbcPnlMiddle.anchor = GridBagConstraints.CENTER;
        gbPnlMiddle.setConstraints( scpLeftTable, gbcPnlMiddle );
        pnPnlMiddle.add(scpLeftTable);

        String [][]dataTblRight = new String[1][7] ;
        String []colsTblRight = new String[] { "ID", "Full Name", "Birthdate","Gender","City","State","Country"};
        tblRightModel = new DefaultTableModel(dataTblRight, colsTblRight);
        tblRight = new JTable(tblRightModel);
        tblRight.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tblRightModel.removeRow(0);
        JScrollPane scpRightTable = new JScrollPane(tblRight);
        scpRightTable.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scpRightTable.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        gbcPnlMiddle.gridx = 10;
        gbcPnlMiddle.gridy = 0;
        gbcPnlMiddle.gridwidth = 9;
        gbcPnlMiddle.gridheight = 12;
        gbcPnlMiddle.fill = GridBagConstraints.NONE;
        gbcPnlMiddle.weightx = 1;
        gbcPnlMiddle.weighty = 1;
        gbcPnlMiddle.anchor = GridBagConstraints.CENTER;
        gbPnlMiddle.setConstraints( scpRightTable, gbcPnlMiddle );
        pnPnlMiddle.add( scpRightTable );

        btBtnAdd = new JButton( "Add"  );
        gbcPnlMiddle.gridx = 2;
        gbcPnlMiddle.gridy = 12;
        gbcPnlMiddle.gridwidth = 5;
        gbcPnlMiddle.gridheight = 2;
        gbcPnlMiddle.fill = GridBagConstraints.NONE;
        gbcPnlMiddle.weightx = 1;
        gbcPnlMiddle.weighty = 0;
        gbcPnlMiddle.anchor = GridBagConstraints.NORTH;
        gbPnlMiddle.setConstraints( btBtnAdd, gbcPnlMiddle );
        pnPnlMiddle.add( btBtnAdd );

        btBtnRemove = new JButton( "Remove"  );
        gbcPnlMiddle.gridx = 12;
        gbcPnlMiddle.gridy = 12;
        gbcPnlMiddle.gridwidth = 5;
        gbcPnlMiddle.gridheight = 2;
        gbcPnlMiddle.fill = GridBagConstraints.NONE;
        gbcPnlMiddle.weightx = 1;
        gbcPnlMiddle.weighty = 0;
        gbcPnlMiddle.anchor = GridBagConstraints.NORTH;
        gbPnlMiddle.setConstraints( btBtnRemove, gbcPnlMiddle );
        pnPnlMiddle.add( btBtnRemove );
        gbcPnlMain.gridx = 0;
        gbcPnlMain.gridy = 4;
        gbcPnlMain.gridwidth = 20;
        gbcPnlMain.gridheight = 15;
        gbcPnlMain.fill = GridBagConstraints.BOTH;
        gbcPnlMain.weightx = 1;
        gbcPnlMain.weighty = 0;
        gbcPnlMain.anchor = GridBagConstraints.NORTH;
        gbPnlMain.setConstraints( pnPnlMiddle, gbcPnlMain );
        pnPnlMain.add( pnPnlMiddle );

        pnPnlBottom = new JPanel();
        GridBagLayout gbPnlBottom = new GridBagLayout();
        GridBagConstraints gbcPnlBottom = new GridBagConstraints();
        pnPnlBottom.setLayout( gbPnlBottom );

        btBtnExit = new JButton( "Exit"  );
        gbcPnlBottom.gridx = 16;
        gbcPnlBottom.gridy = 0;
        gbcPnlBottom.gridwidth = 4;
        gbcPnlBottom.gridheight = 2;
        gbcPnlBottom.fill = GridBagConstraints.NONE;
        gbcPnlBottom.weightx = 1;
        gbcPnlBottom.weighty = 0;
        gbcPnlBottom.anchor = GridBagConstraints.EAST;
        gbPnlBottom.setConstraints( btBtnExit, gbcPnlBottom );
        pnPnlBottom.add( btBtnExit );

        lbLblTime = new JLabel( "Refresh Rate:"  );
        gbcPnlBottom.gridx = 0;
        gbcPnlBottom.gridy = 0;
        gbcPnlBottom.gridwidth = 6;
        gbcPnlBottom.gridheight = 1;
        gbcPnlBottom.fill = GridBagConstraints.BOTH;
        gbcPnlBottom.weightx = 1;
        gbcPnlBottom.weighty = 1;
        gbcPnlBottom.anchor = GridBagConstraints.NORTH;
        gbPnlBottom.setConstraints( lbLblTime, gbcPnlBottom );
        pnPnlBottom.add( lbLblTime );

        tfTxtSeconds = new JTextField( );
        gbcPnlBottom.gridx = 6;
        gbcPnlBottom.gridy = 0;
        gbcPnlBottom.gridwidth = 8;
        gbcPnlBottom.gridheight = 1;
        gbcPnlBottom.fill = GridBagConstraints.BOTH;
        gbcPnlBottom.weightx = 1;
        gbcPnlBottom.weighty = 0;
        gbcPnlBottom.anchor = GridBagConstraints.WEST;
        gbPnlBottom.setConstraints( tfTxtSeconds, gbcPnlBottom );
        pnPnlBottom.add( tfTxtSeconds );

        lbLblThreshhold = new JLabel( "Threshold:"  );
        gbcPnlBottom.gridx = 0;
        gbcPnlBottom.gridy = 1;
        gbcPnlBottom.gridwidth = 6;
        gbcPnlBottom.gridheight = 1;
        gbcPnlBottom.fill = GridBagConstraints.BOTH;
        gbcPnlBottom.weightx = 1;
        gbcPnlBottom.weighty = 1;
        gbcPnlBottom.anchor = GridBagConstraints.NORTH;
        gbPnlBottom.setConstraints( lbLblThreshhold, gbcPnlBottom );
        pnPnlBottom.add( lbLblThreshhold );

        tfTxtThreshold = new JTextField( );
        gbcPnlBottom.gridx = 6;
        gbcPnlBottom.gridy = 1;
        gbcPnlBottom.gridwidth = 8;
        gbcPnlBottom.gridheight = 1;
        gbcPnlBottom.fill = GridBagConstraints.BOTH;
        gbcPnlBottom.weightx = 1;
        gbcPnlBottom.weighty = 0;
        gbcPnlBottom.anchor = GridBagConstraints.NORTH;
        gbPnlBottom.setConstraints( tfTxtThreshold, gbcPnlBottom );
        pnPnlBottom.add( tfTxtThreshold );
        gbcPnlMain.gridx = 0;
        gbcPnlMain.gridy = 19;
        gbcPnlMain.gridwidth = 20;
        gbcPnlMain.gridheight = 2;
        gbcPnlMain.fill = GridBagConstraints.BOTH;
        gbcPnlMain.weightx = 1;
        gbcPnlMain.weighty = 0;
        gbcPnlMain.anchor = GridBagConstraints.NORTH;
        gbPnlMain.setConstraints( pnPnlBottom, gbcPnlMain );
        pnPnlMain.add( pnPnlBottom );

        getContentPane().add(pnPnlMain);
    }

    public void addExitListener(ActionListener listen)
    {
        btBtnExit.addActionListener(listen);
    }

    public void addAddListener(ActionListener listen)
    {
        btBtnAdd.addActionListener(listen);
    }

    public void addRemoveListener(ActionListener listen)
    {
        btBtnRemove.addActionListener(listen);
    }

    public void addCholesterolListener(ActionListener listen) { cbChkCholesterol.addActionListener(listen);}

    public JCheckBox getChkCholesterol() { return cbChkCholesterol;}

    public DefaultTableModel getRightTableModel()
    {
        return this.tblRightModel;
    }

    public DefaultTableModel getLeftTableModel()
    {
        return this.tblLeftModel;
    }

    public JTable getLeftTable()
    {
        return this.tblLeft;
    }

    public JTable getRightTable()
    {
        return this.tblRight;
    }

    public void addRowToRightTable(Object[] newData)
    {
        tblRightModel.insertRow(tblRight.getRowCount(), newData);
    }

    public void populateLeftTable(String[][] data)
    {
        for(int i = 0; i < data.length; i++)
        {
            Object[] tempData = data[i];
            tblLeftModel.insertRow(tblLeft.getRowCount(), tempData);
        }
    }

    public void addColumnToRightTable(String code) throws ParseException {
        tblRightModel.addColumn("Cholesterol",op.getAllPatientObservationValues(code));
    }
}
public Object[] getAllPatientObservationValues(String code) throws ParseException {
        System.out.println("In this method");
        ArrayList<String> values = new ArrayList<>();
        for (int i = 0; i < allPatients.size(); i++)
        {
            values.add(allPatients.get(i).getObservationValue(code));
        }
        return values.toArray();
    }

更新:我看到当我创建表时,我有一个二维数组,数据集为一行7列,并认为这是问题所在,但是当我将其大小增加为8或保留为空白时,它仍然抛出错误

java nullpointerexception jtable
1个回答
0
投票

似乎您已经忘记将Operations对象op设置为inOP。

此:

    public GUIManager(Operations inOP)
    {
        setSize(1000, 650);

应该是:

    public GUIManager(Operations inOP)
    {
        op = inOP;
        setSize(1000, 650);

我不确定您打算使用inOP和op(inOP的深拷贝吗?),但是现在op为null。这行可能是您的NullPointerException来自哪里:tblRightModel.addColumn("Cholesterol",op.getAllPatientObservationValues(code));

© www.soinside.com 2019 - 2024. All rights reserved.