空指针异常 弄了好久也没明白到底是出问题了,重点是抛出错误但是没提示具体是哪里出错了
        inquire3 = new JPanel(new BorderLayout());
String[] title3 = {"顾客姓名","身份证号","手机号码"};
Object[][] datas3 = DataGained.getInstance().getGuestVipYes();
table3 = new JTable(datas3,title3);//在这一句抛出空指针异常
int columncount3 = table3.getColumnCount();
for(int i=1;i<columncount3;i++){
table3.getColumnModel().getColumn(i).setPreferredWidth(50);
}
table3.setRowHeight(25);
inquire3.add(table3.getTableHeader(),BorderLayout.NORTH);
inquire3.add(table3,BorderLayout.CENTER);

                public Object[][] getGuestVipYes(){
return selectGuestVipYes(" select  * from hm_guest where isVIP = 'YES' ");
}
protected Object[][] selectGuestVipYes(String sql){
Object[][] obj1 ={};
Connection dbConn = DataLinked.getConnection();
try{
Statement stmt = dbConn.createStatement();
ResultSet rs = stmt.executeQuery(" select count(*) from hm_guest where isVIP = 'YES' ");
int row = 0;
if(rs.next()){
row = rs.getInt(1);
}
rs = stmt.executeQuery(sql);
for(int i=0;i<row;i++){
obj1 = new Object[row][];
while(rs.next()){
obj1[i] = new Object[3];
for(int j=0;j<3;j++){
obj1[i][j] = rs.getObject(j+2);
}
}
}
rs.close();
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
return obj1;
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable$1.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JSplitPane.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
异常指针

解决方案 »

  1.   

    我查了没有null的字段。调试了,就是Object[][] datas3 = DataGained.getInstance().getGuestVipYes();出现异常
      

  2.   

    方法 selectGuestVipYes中obj1 = new Object[row][];这条语句报的空指针,在实例化Object对象的时候
    应该是new Object[i][j],后面的中括号中必须有值,否则就会报空指针的。
      

  3.   

    空指针会出在DataGained、getInstance()、getGuestVipYes()这三者之一,尤其是DataGained更需要细心检查一下