在 Derby 嵌入式模式下创建表时出现 NullPointerException
我收到错误:
Exception in thread "main" java.lang.NullPointerException
at com.deanchester.minos.utils.DatabaseManager.createTables(DatabaseManager.java:59)
at com.deanchester.minos.tests.testAddContestantMethod.main(testAddContestantMethod.java:21)
我使用此方法创建连接:
private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static final String protocol = "jdbc:derby:";
private static final Properties props = new Properties();
public static Connection getDatabaseConnection() {
try {
if(conn==null || conn.isClosed()) {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(protocol+"minos;create:true;",props);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
我使用以下方法创建表:
private static final String tablesSQL = "CREATE TABLE `contestants` (`id` int(11) NOT NULL AUTO_INCREMENT,`first_name` varchar(100) DEFAULT NULL,`last_name` varchar(100) DEFAULT NULL, `entry` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE `finalTimes` ( `id` int(11) NOT NULL AUTO_INCREMENT,`contestant` int(11) DEFAULT NULL,`time` int(11) DEFAULT NULL,PRIMARY KEY (`id`), KEY `contestant` (`contestant`), CONSTRAINT `finaltimes_ibfk_1` FOREIGN KEY (`contestant`) REFERENCES `contestants` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE `heatTimes` (`id` int(11) NOT NULL AUTO_INCREMENT,`contestant` int(11) DEFAULT NULL,`time` int(11) DEFAULT NULL,PRIMARY KEY (`id`),CONSTRAINT `heattimes_ibfk_1` FOREIGN KEY (`id`) REFERENCES `contestants` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
public static void createTables(){
try {
PreparedStatement ps = conn.prepareStatement(tablesSQL);
ps.executeUpdate();
System.out.println("Tables Created");
} catch (SQLException e) {
e.printStackTrace();
}
}
那么为什么我会收到此错误?我的创建表 SQL 直接来自 MYSQL 数据库的转储。我从 MYSQL 迁移过来,因为我的软件将在独立计算机上运行,并且我不想安装其他任何东西,这就是为什么 apache derby 是一个不错的选择。
编辑
这是我的小测试课:
public class testAddContestantMethod {
public static void main(String[] args){
Contestant cont = new Contestant("Dean","Chester","Mazey");
DatabaseManager.createTables();
cont.writeContestantToDatabase();
DatabaseManager.shutdownDatabase();
}
}
I'm getting the error:
Exception in thread "main" java.lang.NullPointerException
at com.deanchester.minos.utils.DatabaseManager.createTables(DatabaseManager.java:59)
at com.deanchester.minos.tests.testAddContestantMethod.main(testAddContestantMethod.java:21)
I create a connection using this method:
private static final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
private static final String protocol = "jdbc:derby:";
private static final Properties props = new Properties();
public static Connection getDatabaseConnection() {
try {
if(conn==null || conn.isClosed()) {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(protocol+"minos;create:true;",props);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
I create tables using the following method:
private static final String tablesSQL = "CREATE TABLE `contestants` (`id` int(11) NOT NULL AUTO_INCREMENT,`first_name` varchar(100) DEFAULT NULL,`last_name` varchar(100) DEFAULT NULL, `entry` varchar(100) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE `finalTimes` ( `id` int(11) NOT NULL AUTO_INCREMENT,`contestant` int(11) DEFAULT NULL,`time` int(11) DEFAULT NULL,PRIMARY KEY (`id`), KEY `contestant` (`contestant`), CONSTRAINT `finaltimes_ibfk_1` FOREIGN KEY (`contestant`) REFERENCES `contestants` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE `heatTimes` (`id` int(11) NOT NULL AUTO_INCREMENT,`contestant` int(11) DEFAULT NULL,`time` int(11) DEFAULT NULL,PRIMARY KEY (`id`),CONSTRAINT `heattimes_ibfk_1` FOREIGN KEY (`id`) REFERENCES `contestants` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
public static void createTables(){
try {
PreparedStatement ps = conn.prepareStatement(tablesSQL);
ps.executeUpdate();
System.out.println("Tables Created");
} catch (SQLException e) {
e.printStackTrace();
}
}
So why am I getting this error? My create tables SQL came straight from a dump of a MYSQL database. I moved from MYSQL because my software will be run on standalone machines and I wanted nothing else to be installed this is why apache derby is a good choice.
Edit
Here is my Little test class:
public class testAddContestantMethod {
public static void main(String[] args){
Contestant cont = new Contestant("Dean","Chester","Mazey");
DatabaseManager.createTables();
cont.writeContestantToDatabase();
DatabaseManager.shutdownDatabase();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
getDatabaseConnection
中出现异常,您只需将其打印出来 - 这意味着当您尝试创建连接时很可能出现异常,将conn
保留为null
,导致createTables
中出现NullPointerException
。此外,您还没有显示任何实际调用
getDatabaseConnection
的内容。我建议您让
getDatabaseConnection
传播异常(如果有),然后从createTables
调用它。(一般来说,简单地打印出异常然后假装它没有发生并不是一个合适的错误处理策略。)
If there's an exception in
getDatabaseConnection
, you're simply printing it out - which means it's perfectly possible that there was an exception when you tried to create the connection, leavingconn
asnull
, leading to theNullPointerException
increateTables
.Additionally, you haven't shown anything actually calling
getDatabaseConnection
to start with.I suggest you make
getDatabaseConnection
propagate the exception if there is one, and then call it fromcreateTables
.(In general, simply printing out an exception and then pretending it didn't happen isn't a suitable error-handling strategy.)