【问题标题】:NullPointerException when creating tables in Derby Embedded mode在 Derby Embedded 模式下创建表时出现 NullPointerException
【发布时间】:2011-07-10 13:25:39
【问题描述】:

我收到了错误:

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();
  }
}

【问题讨论】:

    标签: java derby


    【解决方案1】:

    如果getDatabaseConnection 中有异常,您只是将其打印出来 - 这意味着当您尝试创建连接时很可能出现异常,将 conn 保留为 null,导致NullPointerExceptioncreateTables

    此外,您还没有显示任何实际调用 getDatabaseConnection 的内容。

    如果有异常,我建议你让getDatabaseConnection 传播异常,然后从createTables 调用它。

    (一般来说,简单地打印出一个异常然后假装它没有发生不是一个合适的错误处理策略。)

    【讨论】:

    • 这是一个NullPointerException,因为我使用了错误的连接变量。当我打电话给conn.prepareStatement(""); 时还没有被分配/初始化
    猜你喜欢
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2018-02-08
    • 2015-03-28
    相关资源
    最近更新 更多