-2
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ClosetTableUI extends JFrame{

public ClosetTableUI(ClosetTableCntl theCreatingCntl){
    initComponents(); 
 theCTCntl = theCreatingCntl;

}

InitComponents is where all variables are initialized.

private void initComponents() {

     if(theTopTable == null){
        System.out.println("Top Table was null");
    }
    if(theCTCntl.getTopTableModel() == null){
        System.out.println("get Top Table Model was null");
    }

This is where the error occurs:

    theTopTable = new JTable(theCTCntl.getTopTableModel());


    topNewButton = new javax.swing.JButton();
    topDeleteButton = new javax.swing.JButton();
    topScrollPane = new javax.swing.JScrollPane(theTopTable);
    theBottomTable = new JTable(theCTCntl.getBottomModel());
    bottomScrollPane = new javax.swing.JScrollPane(theBottomTable);
    bottomNewButton = new javax.swing.JButton();
    bottomDeleteButton = new javax.swing.JButton();
    theAccessoryTable = new JTable(theCTCntl.getAccessoryModel());
    accessoryScrollPane = new javax.swing.JScrollPane(theAccessoryTable);
    topTextField = new javax.swing.JTextField();
    bottomTextField = new javax.swing.JTextField();
    accessoryTextField = new javax.swing.JTextField();

The rest of this is just netbeans GUI builder setting various attributes.

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    topNewButton.setText("New Top");

    topDeleteButton.setText("Delete Top");

    bottomNewButton.setText("New Bottom");

    bottomDeleteButton.setText("Delete Bottom");

    accessoryNewButton.setText("New Accessory");

    accessoryDeleteButton.setText("Delete Accessory");

    topTextField.setText("Enter New Top Here");

    bottomTextField.setText("Enter New Bottom Here");

    accessoryTextField.setText("Enter New Accessory Here");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(topDeleteButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
                                .addComponent(topNewButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(bottomNewButton)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(bottomScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                                    .addComponent(topScrollPane))))
                        .addComponent(bottomDeleteButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(bottomTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(topTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(accessoryScrollPane, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(accessoryDeleteButton)
                            .addGap(55, 55, 55)
                            .addComponent(accessoryNewButton)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(accessoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(60, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(topNewButton)
                .addComponent(topDeleteButton)
                .addComponent(topTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(topScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(bottomDeleteButton)
                .addComponent(bottomNewButton)
                .addComponent(bottomTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(bottomScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(accessoryDeleteButton)
                .addComponent(accessoryNewButton)
                .addComponent(accessoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(accessoryScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    accessoryDeleteButton.addActionListener(new DeleteAccessoryListener());
    accessoryNewButton.addActionListener(new NewAccessoryListener());
    topNewButton.addActionListener(new NewTopListener());
    topDeleteButton.addActionListener(new DeleteTopListener());
    bottomDeleteButton.addActionListener(new DeleteBottomListener());
    bottomNewButton.addActionListener(new NewBottomListener());


    this.setVisible(true);
    pack();

}// </editor-fold>                        

Here is where my variables are declared:

private javax.swing.JButton accessoryDeleteButton;
private javax.swing.JButton accessoryNewButton;
private javax.swing.JScrollPane accessoryScrollPane;
private javax.swing.JTextField accessoryTextField;
private javax.swing.JButton bottomDeleteButton;
private javax.swing.JButton bottomNewButton;
private javax.swing.JScrollPane bottomScrollPane;
private javax.swing.JTextField bottomTextField;
private javax.swing.JButton topDeleteButton;
private javax.swing.JButton topNewButton;
private javax.swing.JScrollPane topScrollPane;
private javax.swing.JTextField topTextField;
JTable theTopTable;
JTable theBottomTable;
JTable theAccessoryTable;
ClosetTableCntl theCTCntl;

And finally, here is the output along with the error message I have been receiving

Top Table was null
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at fashionforecast2.ClosetTableUI.initComponents(ClosetTableUI.java:35)
at fashionforecast2.ClosetTableUI.<init>(ClosetTableUI.java:22)
at fashionforecast2.ClosetTableCntl.<init>(ClosetTableCntl.java:15)
at fashionforecast2.MainMenuCntl.getClosetTableCntl(MainMenuCntl.java:29)
at fashionforecast2.MainMenuUI$ClosetListener.actionPerformed(MainMenuUI.java:70)

From what I've gathered, my varible theTopTable is null. But I'm unsure as to why that is. I declare the variable at towards the bottom of my class and then I am in the process of initializing it when it gives me a null error.

What I'm confused about is this; why am I getting a null error when trying to initialize theTopTable? Of course theTopTable is null, I haven't initialized it yet. But it is giving me that error in the line I'm trying to initialize it in.

Any help would be greatly appreciated.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Tritonis
  • 1
  • 1
  • 2

1 Answers1

1

initComponents method has this line of code:

if(theCTCntl.getTopTableModel() == null){
    System.out.println("get Top Table Model was null");
}

But theCTCntl here is null since is being initialized after calling initComponents in your class constructor:

public ClosetTableUI(ClosetTableCntl theCreatingCntl){
    initComponents(); 
    theCTCntl = theCreatingCntl;
}

Just change the order of your code:

public ClosetTableUI(ClosetTableCntl theCreatingCntl){
    theCTCntl = theCreatingCntl;
    initComponents(); 
}

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Hm, that did it. Do you know why this did trigger if my controller was null? if(theCTCntl.getTopTableModel() == null){ System.out.println("get Top Table Model was null"); } – Tritonis May 05 '14 at 16:12
  • @user2966511 because you cannot call any method nor attribute of a `null` variable. Doing `theCTCntl.getTopTableModel()` here was like calling `null.getTopTableModel()`, this getting the `NullPointerException`. – Luiggi Mendoza May 05 '14 at 16:13
  • So in the future, if I wish to test something like this, would it be better to do tempVar = theCTCntl.getTopTableModel() and then test if tempVar is null? – Tritonis May 05 '14 at 16:16
  • @user2966511 you **CANNOT** call any method or field in a variable which has `null` value. If you have `SomeClass foo = null;` and do `foo.whatever()` it will throw a `NullPointerException` because `foo` is `null`, not `whatever`. – Luiggi Mendoza May 05 '14 at 16:17