JDBC java null pointer exception on my resultset while loop -


this question has answer here:

i getting java.lang.nullpointerexception on while loop on aftermigration method not working properly. , getting warnings on if statement preparedstatements.setstring(..) , setint(..). please appreciate responses.

in jdbc code connecting local db. first selecting records in 1 method updating on next aftermigration method.

public static void main (string[] args) throws exception  {      resultset rs = null;     connection bidsconnection = null;     mydatabasetest migration = new mydatabasetest();      try {          bidsconnection = migration.getoracleconnection();         rs = migration.selectrecordsbids(bidsconnection);         migration.aftermigration(bidsconnection, rs);       } catch (exception e){         e.printstacktrace();     }     {         if (migration.scanner != null) {             migration.scanner.close();             system.out.println("close scanner");         }         if (rs != null) {             rs.close();             system.out.println("close resultset");         }         if (migration.preparedstatement != null) {             migration.preparedstatement.close();             system.out.println("preparedstatement closed");         }          if (bidsconnection != null) {             bidsconnection.close();             system.out.println("bids connection closed");         }      } }    public resultset selectrecordsbids(connection dbconnection) throws sqlexception, classnotfoundexception {       statement statement = null;      string selecttablesql = "select traffic_profile_id traffic_profile_temp"             + "   traffic_profile_id >= ? , traffic_profile_id <= ? ";        system.out.println("bids select statement: " +  selecttablesql);     preparedstatement = dbconnection.preparestatement(selecttablesql);     preparedstatement.setint(1, 1);     preparedstatement.setint(2, 1000);      // execute select sql statement     resultset res = preparedstatement.executequery();      //gets max profile_id record     statement = dbconnection.createstatement();     resultset r = statement.executequery("select traffic_profile_id rowcount traffic_profile_temp pe_egress_flag null "              +"  , pe_ingress_flag null "              +"  , ce_ingress_flag null "              +"  , ce_egress_flag null "              +"  , cos_profile_type null "              +"  , traffic_profile_id >= 1 , traffic_profile_id <= 1000 ");            r.next();     int nullvalues = r.getint("rowcount");     system.out.println("query null valies " + nullvalues);     r.close();     statement.close();     system.out.println("traffic_profile_temp table has null traffic_profile_id of " + nullvalues );      return res;   }   public void aftermigration(connection dbconnection, resultset rs) throws exception {        preparedstatement preparedstatement = null;      string update1 = "update traffic_profile_temp set cos_profile_type = ?, pe_ingress_flag = ?, pe_egress_flag = ?, ce_ingress_flag = ?, ce_egress_flag = ?"             + "  traffic_profile_id >= ? , traffic_profile_id <= ?";      string update2 = "update traffic_profile_temp set cos_profile_type = ?, pe_ingress_flag = ?, pe_egress_flag = ?, ce_ingress_flag = ?, ce_egress_flag = ? "             +  " cosmodel = ? ";              system.out.println("bids manual updating after migration statement: " + update1);              system.out.println("updating bids database in process ...");             preparedstatement = dbconnection.preparestatement(update1);               while (rs.next()) {                   string cosprofiletype = rs.getstring("cos_profile_type");                 string peingressflag = rs.getstring("pe_ingress_flag");                 string peegressflag = rs.getstring("pe_egress_flag");                       string ceingressflag = rs.getstring("ce_ingress_flag");                  string ceegressflag = rs.getstring("ce_egress_flag");                   int trafficprofileid = rs.getint("traffic_profile_id");                   string cosmodel = rs.getstring("cosmodel");                    if ((trafficprofileid > 0 && trafficprofileid < 25 ) && cosprofiletype == null && cosprofiletype == "" &&               peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null) {             system.out.println("first if statement");             preparedstatement.setstring(1, "legacy");                            preparedstatement.setstring(2, "t");             preparedstatement.setstring(3, "t");                 preparedstatement.setstring(4, "f");             preparedstatement.setstring(5, "f");             preparedstatement.setint   (6, 1);             preparedstatement.setint   (7, 24);              preparedstatement.addbatch();              }               else if ((trafficprofileid > 126 && trafficprofileid < 138 ) && cosprofiletype == null && cosprofiletype == ""  &&                       peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null)  {                 system.out.println("2 if statement");             preparedstatement.setstring(1, "standard");             preparedstatement.setstring(2, "t");             preparedstatement.setstring(3, "t");                 preparedstatement.setstring(4, "f");             preparedstatement.setstring(5, "f");             preparedstatement.setint   (6, 127);             preparedstatement.setint   (7, 137);              preparedstatement.addbatch();             }             else if ((trafficprofileid > 799 && trafficprofileid < 900 ) && cosprofiletype == null && cosprofiletype == ""  &&                       peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null ) {                 system.out.println("3 if statement");             preparedstatement.setstring(1, "standard");             preparedstatement.setstring(2, "t");             preparedstatement.setstring(3, "t");                 preparedstatement.setstring(4, "f");             preparedstatement.setstring(5, "f");             preparedstatement.setint   (6, 800);             preparedstatement.setint   (7, 899);              preparedstatement.addbatch();             }             system.out.println("bids manual update2 statement: " + update2);              system.out.println("updating bids database in process ...");             preparedstatement = dbconnection.preparestatement(update2,  resultset.type_scroll_insensitive,                      resultset.concur_updatable);              if  ((cosmodel == "optb" ) && cosprofiletype == null && cosprofiletype == ""  &&                       peingressflag == null  && peegressflag == null  && ceingressflag == null && ceegressflag == null )  {                 system.out.println("4 if statement");             preparedstatement.setstring(1, "optionb");             preparedstatement.setstring(2, "t");             preparedstatement.setstring(3, "t");                 preparedstatement.setstring(4, "f");             preparedstatement.setstring(5, "f");             preparedstatement.setstring(6, "optb");              preparedstatement.addbatch();             }     }             int[] affectedrecords =preparedstatement.executebatch();             dbconnection.commit(); } 

the java.lang.nullpointerexception , warnings getting due logic of if-statements.

for instance, take first if-statement:

if ((trafficprofileid > 0 && trafficprofileid < 25)         && cosprofiletype == null && cosprofiletype == ""         && peingressflag == null && peegressflag == null         && ceingressflag == null && ceegressflag == null) {      //dead code warning here } 

when cosprofiletype null, condition cosprofiletype == null true, code continues , hits cosprofiletype == "", triggers nullpointerexception.

this logic makes if-statement never true (an object null never empty). means code inside if-statements never executed, hence warnings.

edit:

as pointed out mark rotteveel, cosprofiletype == null not throw nullpointerexception.

i recommend reading on use of == compare strings:

java string.equals versus ==


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -