2014-05-05 79 views
-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是初始化所有变量的位置。初始化期间的空变量

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

这是发生错误:

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

这剩下的只是的NetBeans GUI构建器来设置各种属性。

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>       

这里是我的变量声明:

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; 

最后,这里是输出和错误消息,我已经收到

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) 

从我一起收集起来,我的varible theTopTable为null。但我不确定这是为什么。我将变量声明在我的类的底部,然后当它给出一个空错误时,我正在初始化该变量。

我很困惑的是这个;为什么在尝试初始化TopTable时收到空错误?当然TopTable是null,我还没有初始化它。但它给我的错误,我试图初始化它在线

任何帮助将不胜感激。

回答

1

initComponents方法有下面这行代码:

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

theCTCntl这里是null因为调用initComponents在类的构造函数之后被初始化:

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

只要改变你的代码的顺序:

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

更多信息:

+0

嗯,这做到了。你知道为什么如果我的控制器是空的,它会触发吗? if(theCTCntl.getTopTableModel()== null){System.out.println(“get Top Table Model was null”); } – Tritonis

+0

@ user2966511,因为你不能调用任何方法,也不能调用'null'变量的属性。在这里执行'CTCntl.getTopTableModel()'就像调用'null.getTopTableModel()',这会得到'NullPointerException'。 –

+0

所以在未来,如果我想测试这样的东西,是否会更好地做tempVar = theCTCntl.getTopTableModel(),然后测试tempVar是否为空? – Tritonis