在 Derby 嵌入式模式下创建表时出现 NullPointerException

发布于 2024-11-19 16:43:58 字数 2834 浏览 8 评论 0原文

我收到错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一向站在原地 2024-11-26 16:43:59

如果 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, leaving conn as null, leading to the NullPointerException in createTables.

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 from createTables.

(In general, simply printing out an exception and then pretending it didn't happen isn't a suitable error-handling strategy.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文