public static void AddNew(Employeea aEmployee)
{
Statement aStatement=null;
String strSQL;
strSQL="INSERT INTO Employees";
strSQL+=" (Address,BirthDate,Country,Extension,FirstName,LastName,HireDate,";
strSQL+=" HomePhone,Notes,PostalCode,Region,City,ReportsTO,Title,TitleOfCourtesy,photoPath)";
strSQL+=" VALUES (";
strSQL+="'"+ aEmployee.getAddress()+"'"+",";
strSQL+="'"+ aEmployee.getBirthDate()+"'"+",";
strSQL+="'"+ aEmployee.getCountry()+"'"+",";
strSQL+="'"+ aEmployee.getExtension()+"'"+",";
strSQL+="'"+ aEmployee.getFirstName()+"'"+",";
strSQL+="'"+ aEmployee.getLastName()+"'"+",";
strSQL+="'"+ aEmployee.getHireDate()+"'"+",";
strSQL+="'"+ aEmployee.getHomePhone()+"'"+",";
strSQL+="'"+ aEmployee.getNotes()+"'"+",";
strSQL+="'"+ aEmployee.getPostalCode()+"'"+",";
strSQL+="'"+ aEmployee.getRegion()+"'"+",";
strSQL+="'"+ aEmployee.getCity()+"'"+",";
strSQL+="'"+ aEmployee.getReportsTo()+"'"+",";
strSQL+="'"+ aEmployee.getTitle()+"'"+",";
strSQL+="'"+ aEmployee.getTitleOfCourtesy()+"'"+",";
strSQL+="'"+ aEmployee.getphotoPath()+"')";
try
{
getDBConnection();
aStatement.executeUpdate(strSQL);
i=Employees.size()-1;
}
catch(SQLException e)
{
JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR",JOptionPane.ERROR_MESSAGE);
}
finally
{

closeStatement(aStatement);
closeConnection();
}

}
不知道这样做添加程序有什么错误吗?我在Eclipse运行报出这样的错误
java.lang.NullPointerException
at gui.EmployeeDA.AddNew(EmployeeDA.java:220)
向各位路过人请教。

解决方案 »

  1.   

     Connection con = getDBConnection();   
     aStatement  = con.createStatement();
     aStatement.executeUpdate(strSQL);
    i=Employees.size()-1;
      

  2.   

    哪行是你的220行啊 ?
    btw:没对aEmployee是否为null做过判断
    ..还有些其他问题。。嘿嘿
      

  3.   

    Statement aStatement=null;
    ......
    try
            {
                getDBConnection();        
                aStatement.executeUpdate(strSQL);
                i=Employees.size()-1;
            }
    应该是
    try
            {
                
                aStatement=getDBConnection().createStatement(); 
                aStatement.executeUpdate(strSQL);
                i=Employees.size()-1;
            }
      

  4.   

    getDBConnection()是我自己写好的连接数据库的方法,在查询的代码中用了没问题。
    其中的220行是aStatement.executeUpdate(strSQL);
      

  5.   

    樓上的似乎都有道理。
    应该是 
    try 
            { 
                 
                aStatement=getDBConnection().createStatement();  
                aStatement.executeUpdate(strSQL); 
                i=Employees.size()-1; 
            } 
    另外,那一行是220行啊?這個對于解決問題很重要的。
    還有就是插入的數據NULL什么的判斷,也要根據DB定義狀況注意一下。
      

  6.   

    好,這樣的話應該是這樣的
    try  
            {  
                  
                aStatement=getDBConnection().createStatement();   
                aStatement.executeUpdate(strSQL);  
                i=Employees.size()-1;  
            }
      

  7.   

    也是啊
    Statement 類型的實例就應該是getDBConnection().createStatement();生成。為什么樓主直接就
    【Statement aStatement=null;】
    然后就
    【aStatement.executeUpdate(strSQL);】這個時候aStatement還是null呢,怎么可以?
      

  8.   

    问题找到了
    原来没有初始化
    aStatement
    添加这句话就没问题了,谢谢各位的提示。
    aStatement=aConnection.createStatement();