2012-12-04 121 views
0

調用方法fillDBWeek(String mName)時發生NullPointerException。我不能發現我哪裏錯了,有人可以幫我嗎?調用方法時發生NullPointerException

這是我得到了下面的方法錯誤

*Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at View.LectureReport.fillDBWeek(LectureReport.java:49) 
    at View.LectureReport$2.actionPerformed(LectureReport.java:96)* 

方法fillDBWeek

public void fillDBWeek(String mName) 
    { 
     tableModel.setDataVector(lRHand.popuSDataWeek(mName), columnNames); 
     table.setModel(tableModel); 
     table.repaint(); 
    } 

凡使用來電按下按鈕

JButton btnViewReport = new JButton("View Report"); 
    btnViewReport.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String moduleName = (String)comboBox.getSelectedItem(); 
      if(comboBox_1.getSelectedItem().equals("Week")) 
      { 
       fillDBWeek(moduleName); 
      } 
      else if (comboBox_1.getSelectedItem().equals("Semester")) 
      { 
       fillDBSem(moduleName); 
      } 
      else if (comboBox_1.getSelectedItem().equals("Year")) 
      { 
       fillDBYear(moduleName); 
      } 
     } 
    }); 

查詢時調用該方法來自SQL數據庫的數據

基於棧3210
public Object[][] popuSDataWeek(String moduleName) 
{ 
    data = new Object[30][9]; 

    try{ 
      Class.forName(DRIVER_CLASS); 

      connection = DriverManager.getConnection(url,"polk", "mire"); 

      statement = connection.createStatement(); 
      results = statement.executeQuery("SELECT * FROM Group6_StudAttWeek WHERE module = '"+ moduleName +"'"); 
      int i=0; 
      while(results.next()) 
      { 
      data[i][0]= results.getString("firstname"); 
      data[i][1]= results.getString("lastname"); 
      data[i][2]= results.getString("module"); 
      data[i][3] = results.getString("yearOfStudy"); 
      data[i][4]= results.getInt("workshop%"); 
      data[i][5]= results.getInt("tutorial%"); 
      data[i][6] = results.getInt("lecture%"); 
      data[i][7] = results.getInt("avg%"); 
      data[i][8] = results.getInt("number"); 
      i++; 
      } 
     results.close(); 
     statement.close(); 
     connection.close(); 
     } catch (SQLException sqlException) { 
     sqlException.printStackTrace(); 
     System.exit(1); 
     }catch(Exception exception) { 
     System.err.println("An error happened"); 
     System.exit(1); 
     } 

    return data; 
} 
+2

什麼是第49行? –

+0

根據堆棧,如果異常在'fillDBWeek'而不是'popuSDataWeek'中出現,那麼'tableModel'或'table'都是null。如果'popuSDataWeek'拋出異常,我看到的唯一可能的空對象是'connection'。你有沒有調試你的應用程序來檢查這三種可能性? – Gamb

+0

和youi沒有得到任何其他的執行?它是一個執行,在你進入你的程序時出現,還是你捕獲了這個Nullpointer? – SomeJavaGuy

回答

1

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at View.LectureReport.fillDBWeek(LectureReport.java:49) 
    at View.LectureReport$2.actionPerformed(LectureReport.java:96) 

的異常上升fillDBWeek,要麼是因爲tableModeltable爲空。如果異常被popuSDataWeek拋出,而應該出現在堆棧跟蹤中,但無論如何,我看到的唯一可能的空對象是connection

請確保在使用它們之前聲明和初始化正確的對象。

相關問題