忙于JDBC项目并遇到问题。我正在尝试从主类调用插入方法。但是当我运行时,我不断收到一个空指针异常。
public static final String INSERT_ONE_RECORD = "INSERT INTO " + TABLE_AUTHORS + "(" + COLUMN_AUTHORS_AUTHOR + ")" +
" VALUE (?)";
private Connection connection;
public void insertOneRecord (String author){
try {
insertOneRecordPrepStmt = connection.prepareStatement(INSERT_ONE_RECORD);
insertOneRecordPrepStmt.setString(1,author);
insertOneRecordPrepStmt.executeUpdate();
System.out.println("insert author went");
} catch (SQLException e) {
System.out.println("insert author failed"+ e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
AuthorRepository authorRepository = new AuthorRepository();
authorRepository.insertOneRecord("George");
}
运行时,我得到:
Exception in thread "main" java.lang.NullPointerException
at unasat.Repositories.AuthorRepository.insertOneRecord(AuthorRepository.java:57)
at unasat.Repositories.AuthorRepository.main(AuthorRepository.java:85)