数据库项目中线程“main”java.lang.NullPointerException中的异常

时间:2013-11-19 13:22:14

标签: java jquery database database-connection

我试图查看数据库中的所有数据或将数据插入我的数据库。两种方法都给出了相同的响应。 我的pojo课程:

public class Title {

private Integer id;
private String name;
private String code;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

}

我的添加和显示方法的服务类:

public abstract class TitleService {

 public static void addTitle(Title title) throws Exception {

    Connection c = Global.createConnection();
    //Title title=new Title();
    PreparedStatement ps = c.prepareStatement(
            "insert into Title (id, name, code) values (?, ?, ?)");
    ps.setInt(1, title.getId());
    ps.setString(2, title.getName());   
    ps.setString(3, title.getCode());

    ps.executeUpdate();
    c.commit();
    ps.close();
    c.close();
  }
public static List<Title> allTitles() throws SQLException {
    Connection c = Global.createConnection();
    Statement st = c.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM Title");
    List <Title> titles = new ArrayList<>();
    Title title;
        while (rs.next()) {
            title = new Title();
            title.setId(rs.getInt(1));
            title.setName(rs.getString(2));
            title.setCode(rs.getString(3));
            titles.add(title);
        }

    st.close();
    c.close();
    return titles;
    }
}

测试类:

public class ServiceTest {


public static void main(String[] arga) throws SQLException, Exception {

    Title t = new Title();
    t.setId(1);
    t.setCode("Prof");
    t.setName("Profesor");

    TitleService.addTitle(t);

    //TitleService.allTitles();

}

}

当ı运行它给出的测试类时,

Exception in thread "main" java.lang.NullPointerException

at service.TitleService.addTitle(TitleService.java:22)

at test.ServiceTest.main(ServiceTest.java:26)

Java Result: 1

TitleService.java:22 ---> PreparedStatement ps = c.prepareStatement(
                "insert into Title (id, name, code) values (?, ?, ?)");

ServiceTest.java:26)---> TitleService.addTitle(t);
this exception.

我不知道问题是什么

我的全局类敌人数据库连接:

公共抽象类全球{

public static Connection createConnection() {
     Connection conn;
    System.out.println("Checking if Driver is registered with DriverManager.");
try{
        Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e){
        System.out.println("Couldn't find the driver!");
        System.out.println("exit.");
        System.exit(1);
}
System.out.println("make a connection.");
try{
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/uni_db", "postgres", "postgres");
        System.out.println("Success.");
} catch(SQLException e){
        System.out.println("Couldn't connect: exit.");
        System.exit(1);}
    return null;
}    

}

3 个答案:

答案 0 :(得分:2)

来自returning null方法的createConnection()值 您应该返回Connection个对象。

 return conn;

答案 1 :(得分:1)

看起来您的Connection对象为null。 方法createConnection返回null,它应该返回对象conn。

try{
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/uni_db", "postgres", "postgres");
        System.out.println("Success.");
} catch(SQLException e){
        System.out.println("Couldn't connect: exit.");
        System.exit(1);}
    return conn;
} 

答案 2 :(得分:0)

此错误表示Connection c = Global.createConnection();不工作。 首先,如果你不使用这个类作为抽象,你为TitleService使用抽象类?删除抽象关键字

我不知道Global.createConnection(),但为什么你没有使用正常的方式来获得连接。请检查以下网址 http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm