在JTable中添加列时出现NullPointer异常。

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

我正在编写一个程序,当输入医师号(FHIR HAPI)时显示病人记录,但这不是重点。我想显示病人的基本信息,然后通过一个复选框,允许用户在JTable中添加一个额外的列来显示他们的详细信息。

这个额外的列应该可以根据复选框的状态进行添加和删除。然而,当我尝试使用我的 DefaultTableModel 变量来执行 model.addColumn() 方法,我总是得到一个 NullPointer 异常。

最后一个方法是产生问题的方法。我还提供了被调用的其他方法的代码。我尝试了使用各种类型的数组(Object和String)作为值,甚至是数组列表。你还会看到,在我的第二个方法的代码中,你会看到 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();
    }

更新:我看到当我创建我的表时,我有一个数据集的2D数组为一行7列,并认为这是问题,但当我增加它的大小,如8或留空,然后它仍然抛出错误

java nullpointerexception jtable
1个回答
1
投票

似乎你忘了将Operations对象op设置为inOP。

这个。

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

应该是这个

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

我不知道你打算用inOP和op做什么(inOP的深层拷贝?),但现在op是空的。这可能就是你的 NullPointerException 是来自,在行。tblRightModel.addColumn("Cholesterol",op.getAllPatientObservationValues(code));

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