有人可以解释一下为什么我在代码中得到空指针异常
try
{
dao = new EmployeeDAO();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(
null,
"Creating Dao Error: " + e);
}
我收到对话消息,指出发生了空指针异常。
这是Swing类的完整代码
public class EmployeeSearchApp extends JFrame
{
private JPanel mainPanel;
private JPanel textLabelPanel;
private JTextField lastNameTxtField;
private JButton searchButton;
private JLabel lastNameLabel;
private EmployeeDAO dao;
public EmployeeSearchApp()
{
try
{
dao = new EmployeeDAO();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(
null,
"Creating Dao Error: " + e);
}
searchButton.addActionListener(e ->
{
try
{
String lastName = lastNameTxtField.getText();
List<Employee> employees = null;
if (lastName != null && lastName.trim().length() > 0)
{
employees = dao.searchEmployees(lastName);
}
else
{
employees = dao.getAllEmployee();
}
for (Employee emp : employees)
{
System.out.println(emp);
}
}
catch (Exception exc)
{
JOptionPane.showMessageDialog(
null,
"addActionListener Error: " + exc);
}
});
}
private void createUIComponents()
{
// TODO: place custom component creation code here
}
public static void main(String[] args)
{
try
{
EmployeeSearchApp frame = new EmployeeSearchApp();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
这是DAO类的代码
package DataAccesObject;
import Core.Employee;
import java.io.FileInputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class EmployeeDAO
{ 私人连接conn;
public EmployeeDAO()
{
Properties props = null;
//getting properties
try
{
props = new Properties();
props.load(new FileInputStream("props.properties"));
conn = DriverManager.getConnection(
props.getProperty("dbURL"),
props.getProperty("user"),
props.getProperty("password"));
}
catch (java.io.IOException e)
{
System.out.println("properties file is not found");
System.exit(-2);
}
catch (java.sql.SQLException e)
{
System.out.println("couldn't connect to DATABASE");
System.exit(-3);
}
System.out.println("Connection to DB successful");
}
public List<Employee> getAllEmployee() throws java.sql.SQLException
{
List<Employee> list = new ArrayList<>();
Statement stmt = null;
ResultSet resSet = null;
try
{
stmt = conn.createStatement();
resSet = stmt.executeQuery("Select * from employees");
while (resSet.next())
{
//list.add(convertToEmployee(resSet));
Employee tmp = convertToEmployee(resSet);
list.add(tmp);
}
return list;
}
finally//Execute ALWAYS after try block doesn't matter if try thrown exception
{
if (stmt != null)
stmt.close();
if (resSet != null)
resSet.close();
}
}
public List<Employee> searchEmployees(String lastName) throws Exception
{
List<Employee> list = new ArrayList<>();
PreparedStatement myStmt = null;
ResultSet myRs = null;
try
{
lastName = "%" + lastName + "%";
myStmt = conn.prepareStatement("select * from employees where last_name like ?");
myStmt.setString(1, lastName);
myRs = myStmt.executeQuery();
while (myRs.next())
{
Employee tempEmployee = convertToEmployee(myRs);
list.add(tempEmployee);
}
return list;
}
finally
{
if (myStmt != null)
myStmt.close();
if (myRs != null)
myRs.close();
}
}
private Employee convertToEmployee(ResultSet res) throws java.sql.SQLException
{
return new Employee(
res.getString("first_name"),
res.getString("last_name"),
res.getString("email"),
res.getBigDecimal("salary")
);
}
public static void main(String[] args) throws Exception
{
EmployeeDAO dao = new EmployeeDAO();
System.out.println(dao.searchEmployees("thom").size());
}
}