【问题标题】:NullPointerException when adding a column to JTable向 JTable 添加列时出现 NullPointerException
【发布时间】:2020-05-25 02:49:59
【问题描述】:

我正在编写一个程序,在输入医生编号 (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 或将其留空时,然后还是报错

【问题讨论】:

  • 你能包括你收到的堆栈跟踪吗?我看到您正在调用op.getAllPatientObservationValues(code),并且private Operations op 没有在您的构造函数中实例化。您也没有获取器或设置器。如果我不得不猜测,这就是问题所在。没有堆栈跟踪,很难明确地说出来。
  • 最后一种方法是产生问题的方法。 - 堆栈跟踪告诉您导致问题的语句。因此,您找出该方法中该语句中的哪个变量为空,然后解决问题。
  • 天哪,我没有看到我没有初始化 op,它起作用了,非常感谢你们。

标签: java swing nullpointerexception jtable


【解决方案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));

【讨论】:

  • @JamesFoster 不要忘记通过单击复选标记“接受”答案,以便人们知道问题已解决。
  • 啊,对不起,我对整个 StackOverflow 还是很陌生。感谢您的提醒
猜你喜欢
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
相关资源
最近更新 更多