1

I am trying to retrieve tablename, count of all rows in a table, max updated timestamp and from a corresponding source system in a database using java jdbc connection. So far this is what I came up with.

public Map<String,Long> getGpTableCount() throws SQLException {
    List<String> gpTableList        = getChunks();                          // sourcedb.tablenamename,targetdb.tablename:SSN
    Iterator<String> keySetIterator_gpTableList = gpTableList.iterator();   // sourcedb.tablename,targetdb.tablename:SSN
    Connection sourcedbCon      = (Connection) DbManager.getGpConnection();
    PreparedStatement gp_pstmnt     = null;
    String gpExcpnMsg;
    while(keySetIterator_gpTableList.hasNext()) {
        String gpTabSchemakey   = keySetIterator_gpTableList.next();    // sourcedb.tablename,targetdb.tablename:SSN
        String[] tablesnSSNs    = gpTabSchemakey.split(",");            // tablesnSSNs[0]: sourcedb.tablename, tablesnSSNs[1]: targetdb.tablename:SSN
        String[] target         = tablesnSSNs[1].split(":");            // target[0]=targetdb.tablename, target[1]=SSN
        String[] sourcedbTable  = target[0].split("\\.");               // sourcedbTable[0] = targetdb, sourcedbTable[1]=table
        String gpCountQuery      = "select '" + sourcedbTable[1] + "' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from " + tablesnSSNs[0] + " where source_system_name = '" + target[1] + "' group by source_system_name";
        System.out.println("\nGP Query: " + gpCountQuery);
        try {
            gp_pstmnt            = sourcedbCon.prepareStatement(gpCountQuery);
            ResultSet gpCountRs  = gp_pstmnt.executeQuery();        // gpCountRs(1): table, gpCountRs(2): count, gpCountRs(3): source_system_name, gpCountRs(4): max(xx_last_update_tms)
            while(gpCountRs.next()) {
                System.out.println("sourcedbTable[1]: " + sourcedbTable[1] + " gpCountRs.getString(3): " + gpCountRs.getString(3) + " gpCountRs.getLong(2): " + gpCountRs.getLong(2));
                gpCountMap.put(sourcedbTable[1] + "," + gpCountRs.getString(3), gpCountRs.getLong(2));  // (tableName,ssn,12345678)
            }
        } catch(org.postgresql.util.PSQLException e) {
            System.out.println("In SQL Exception block");
            gpExcpnMsg = e.getMessage();
            gpTabNotFound.put(gpCountQuery, gpExcpnMsg + "\n");
        } catch(SQLException e) {
            e.printStackTrace();
        } catch(Exception e) {
            System.out.println("In Exception block");
            gpExcpnMsg = e.getMessage();
            e.printStackTrace();
        }
    }
    System.out.println("GP Connection closed");
    gp_pstmnt.close();
    sourcedbCon.close();
    return gpCountMap;
}

The gpTableList is an arrayList which receives the data in the form of arraylist from: the method getChunks() I have broken down the data format present in the arralist in the comments next to the code. The problem here is when I run this method, I get correct data from the table but it also produces a null pointer exception straight away. The query formed, result and the exception message can be seen below:

GP Query: select 'xx_table_name' as TableName, count(*) as Count, source_system_name, max(xx_last_update_tms) from sourcedb.xx_table_name where source_system_name = 'ORACLE' group by source_system_name
sourcedbTable[1]: xx_table_name gpCountRs.getString(3): ORACLE gpCountRs.getLong(2): 5181260
In Exception block
Exception Message: null

java.lang.NullPointerException
        at com.recordcount.dao.ChunkData.getGpTableCount(ChunkData.java:117)
        at com.recordcount.entry.StartCount.main(StartCount.java:53)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

The line the exception corresponds in the:

gpCountMap.put(analyticsTable[1] + "," + gpCountRs.getString(3), gpCountRs.getLong(2));

This is my main class:

public class StartCount {

    public static void main(String[] args) throws SQLException, IOException, AddressException, InterruptedException {
        ChunkData chData = new ChunkData();
        chData.getGpTableCount();
    }
}

I tried to debug the code but I am unable to understand the mistake I did. Could anyone let me know how can I fix this ?

Metadata
  • 2,127
  • 9
  • 56
  • 127

1 Answers1

1

According to your code,gpCountMap is null, you have make it as a global variable but you not initialize it.

In order to solve it, just add below code at the beginning of getGpTableCount

gpCountMap = new HashMap<String,Long>();

Or add it in the main method:

public static void main(String[] args) throws SQLException, IOException, AddressException, InterruptedException {
    ChunkData chData = new ChunkData();
    chData.getGpTableCount();
    gpCountMap = new HashMap<String,Long>();
}
flyingfox
  • 13,414
  • 3
  • 24
  • 39