I'm trying to generate a set of tables in my database for each month of the entire year. I have a separate class called 'DBConnection.java' where I'm fetching details like jdbcServerType, UserName, password, serverPort and the name of the database where the tables are supposed to be created, from a config file which follows an XML format after processing it through a class called RootTagsConfigList. Below this I have a validation to check if the database exists or not and in case it does not exist, create the database before moving on to creating the tables. During my debugging session, I can see that my config file is being read perfectly and all the above said values are being stored in the respective variables after its being read from RootTagsConfigList.
public DBConnection(RootTagsConfigList rootTagsConfigListObj) {
try {
String jdbcServerType = rootTagsConfigListObj.getRootTagsConfigList().get(0).getJDBCServerType();
String userName = rootTagsConfigListObj.getRootTagsConfigList().get(0).getUsername();
String password = rootTagsConfigListObj.getRootTagsConfigList().get(0).getPassword();
String dbserver = rootTagsConfigListObj.getRootTagsConfigList().get(0).getDbserver();
String serverPort = rootTagsConfigListObj.getRootTagsConfigList().get(0).getServerPort();
database = rootTagsConfigListObj.getRootTagsConfigList().get(0).getDatabaseName();
Class.forName("com.mysql.jdbc.Driver").newInstance();
create.execute("CREATE SCHEMA IF NOT EXISTS " +database);
String url = "jdbc:" + jdbcServerType + "://" + dbserver + ":" + serverPort + "/" + database;
conn = DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
e.printStackTrace();
}
}
My code worked fine a few days back and I was able to generate the entire tables for a year. However, today I ran my code and caught a null pointer exception in the create statement. During debugging, everytime I encountered create.execute, the program went into exception. I tried printing the StackTrace and got the following result,
at program.main.DBConnection.(DBConnection.java:40) at program.Main.main(Main.java:64)
When I traced back to Main:64, it pointed me to dbConnectionObj which is supposed to store all my data as null. I then moved on to DBConnection:40 (line which has create.execute in the above code) which points to my create.execute statement which is also null. Alternatively, I tried assigning the sql statement to a variable and printing the query which printed the sql statement to be executed correctly but then again in this case when it encounters create.execute, it throws me a null pointer exception.
Below is my code for Main:64 (where dbConnectionObj seems to be null)
try {
Statement create = null;
new ReadXMLConfig();
RootTagsConfigList rootTagsConfigListObj = new RootTagsConfigList();
dbConnectionObj = new DBConnection(rootTagsConfigListObj);
String tableAction = rootTagsConfigListObj.getRootTagsConfigList().get(0).getTableAction();
String deleteProgramId = rootTagsConfigListObj.getRootTagsConfigList().get(0).getDeleteProgramId();
String createForTheYear = rootTagsConfigListObj.getRootTagsConfigList().get(0).getcreateForTheYear();
String strStartDate = rootTagsConfigListObj.getRootTagsConfigList().get(0).getStartDateTime();
String strEndDate = rootTagsConfigListObj.getRootTagsConfigList().get(0).getEndDateTime();
Again in the above code, all my variables are read perfectly and stored in the respective variables.I'm surprised how the code could throw me an error when it previously worked fine and I haven't made any changes whatsoever but I'd really like to know where I'm going wrong cause I mostly code in python and am not very familiar with Java yet.
Please let me know if any additional information is required in case I haven't explained my problem well enough. Any help is appreciated!
Thanks in advance!