0

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!

MM2
  • 29
  • 8
  • just give try by removing mysql.jar and add again – shreyansh jogi May 19 '14 at 10:37
  • I did and that did not solve the problem :( – MM2 May 19 '14 at 10:39
  • what line of code is 64? – Ashish May 19 '14 at 10:40
  • which version of jar you are using – shreyansh jogi May 19 '14 at 10:40
  • Where is _create_ assigned to anything other than null in your code? Also, if _dbConnectionObj_ is null, than _dbConnectionObj.createStatement()_ (supposing it is called somewhere) should be throwing the exception, before the call to _execute()_. When in doubt on these things, trace your objects with _System.out.println(create == null)_ for example. – schmop May 19 '14 at 10:40
  • @Ashish - the line which has, dbConnectionObj = new DBConnection(rootTagsConfigListObj); – MM2 May 19 '14 at 10:42
  • @shreyanshjogi - I'm using mysql-connector-java-5.1.20-bin.jar – MM2 May 19 '14 at 10:43
  • @schmop - My dbConnectionObj is indeed null but I'm not sure why this would be the case since every other data is read from rootTagsConfigList without a problem! – MM2 May 19 '14 at 10:45
  • @schmop - I was calling the create statement before I passed on the db url, username and password to conn which was why my dbConnectionObj was null. Your suggestion was a good starting point to figure out what I did wrong. Thanks very much! – MM2 May 19 '14 at 11:10
  • Would be great if the person who down-voted could state the reason as well. It would help me with my questions in the future! – MM2 May 19 '14 at 11:27

2 Answers2

3

Make connection first and than execute statement in constructor DBConnection.

    String url = "jdbc:" + jdbcServerType + "://" + dbserver + ":" + serverPort + "//" + database;
    conn = DriverManager.getConnection(url, userName, password);
    //I guess create is Object of Statement
    Statement create= conn.createStatement();
    create.execute("CREATE SCHEMA IF NOT EXISTS " +database);
akash
  • 22,664
  • 11
  • 59
  • 87
  • 1
    I guess my problem was that I was trying to call the create.execute statement before declaring the url and conn values. I changed the order of my code and included the conn.createStatement() line before create.execute and now it seems to be working fine. Thanks for the suggestion! – MM2 May 19 '14 at 11:04
2

Before creating statement check whether dbConnectionObj is null or not null. if null then issue is with connection creation class. debug and find out why connection is not being created.

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
  • I followed your advice and identified that I'm calling the create statement without actually passing any value to it, thereby making it null. Thanks for the help! – MM2 May 19 '14 at 11:06
  • please up vote if it solve your problems – Macrosoft-Dev May 19 '14 at 11:07
  • I'm sorry, I don't have enough reputation points to up vote but I'm gonna come back to the question once I'm eligible. – MM2 May 19 '14 at 11:15