在JDBC代码中获取NullpointerException

时间:2012-06-11 20:38:55

标签: java jdbc nullpointerexception

以下代码抛出NullPointerException - 我不知道问题出在哪里。我使用Excel电子表格作为后端。

//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

1 个答案:

答案 0 :(得分:0)

sqld的字段值可能为null。现在不幸的是sqlDate.compareTo(null)将抛出NullPointerException。 (其他compareTo表现得更好。)