2012-06-11 63 views
0

以下代碼引發NullPointerException - 我不知道問題出在哪裏。我正在使用Excel電子表格作爲後端。在JDBC代碼中獲取NullpointerException

//actionlistener for btnfetch 
btnfetch.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvent ae) 
    { 
     try 
     { 
      String name=""; 
      d1 = (java.util.Date)formatter.parse(textdate.getText()); 
      java.sql.Date sqlDate = new java.sql.Date(d1.getTime()); 
      java.sql.Date sqld=null; 

      ResultSet rs=st.executeQuery("select * from [ARNUM$]"); 
      String getname=null; 

      while(rs.next()) 
      { 
       getname=rs.getString(1); 
       sqld=rs.getDate(2); 

       System.out.println(getname+" "+sqld+" "+sqlDate); 

       if(sqlDate.compareTo(sqld)==0) 
       { 
        name=name+" "+getname; 
        name=name.toUpperCase(); 
       } 
       System.out.println(name); 

      } 


      if(name.length()==0) 
      { 
       JOptionPane.showMessageDialog(
        pnlp4, 
        "No Name having "+textdate.getText(), 
        "Warning", 
        JOptionPane.WARNING_MESSAGE 
       ); 
      } 
      else 
      { 
       textname.setText(name); 
      } 
     } 
     catch(Exception e) 
     {   
      e.printStackTrace(); 
     } 
    }//end actionPerformed 
}); //end addActionListener 
+6

哪條線是例外? – Dan

回答

0

sqld的字段值可能爲空。現在不幸sqlDate.compareTo(null)會拋出一個NullPointerException。 (其他比較表現更好)