0

I am writing a program that displays patient records when entering a practitioner number (FHIR HAPI), but that is besides the point. I want to display basic patient details, and then through a checkbox, allow the user to add an extra column to the JTable that displays their details.

This extra column should be able to be added and removed depending on the state of the checkbox. However, when I try to use my DefaultTableModel variable to perform the model.addColumn() method, I always get a NullPointer exception.

The very last method is the one creating issues. I also provided code to the other methods that are called. I have tried using various types of arrays (Object and String) for the values and even arraylists. You will also see that in my second piece of code for the method getAllPatientObservationValues() I performed a println to see if it goes into the method and it does not. Any help will be much appreciated.

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();
    }

Update: I saw that when I create my table I have a 2D array for the data set as one row 7 columns and thought that was the problem, but when I increased the size of it to say 8 or left it blank, then it still threw an error

3
  • Can you include the stacktrace you are receiving? I see you're calling op.getAllPatientObservationValues(code), and that private Operations op is not instantiated in your constructor. You also do not have getters or setters for it. If I had to guess, this is the issue. It's hard to say definitively without the stacktrace. Commented May 25, 2020 at 3:23
  • The very last method is the one creating issues. - The stack trace tells you the statement causing the problem. So you figure out which variable at that statement in that method is null and then fix the problem. Commented May 25, 2020 at 3:37
  • Oh man, I didn't see that I didn't initialise op, it worked, thank you both very much. Commented May 25, 2020 at 7:50

1 Answer 1

2

It seems like you've forgotten to set the Operations object op to inOP.

This:

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

Should be this:

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

I'm not sure what you were planning to do with inOP and op (deep copy of inOP?), but right now op is null. That's probably where your NullPointerException is coming from, in the line: tblRightModel.addColumn("Cholesterol",op.getAllPatientObservationValues(code));

Sign up to request clarification or add additional context in comments.

2 Comments

@JamesFoster Don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved.
Ahhhh sorry about that, I'm still new to the whole StackOverflow thing. Thanks for the heads up

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.