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();
}
}
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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.