2014-03-12 16 views
0

我正在使用一個oracle連接來處理一些文件。在這裏,我有一個xml文件,它提供了一些條件,可以是true或false.Oracle連接將被加載或不加載。所以,我能夠處理很多,但問題是,當條件爲false時,連接關閉,但調用連接的類正在獲取NULLPointerException。我必須用適當的消息處理Nullpointer ...我張貼的代碼....如何以自定義的方式處理NullPointerException

供應連接...

try { 
     String s; 
     if ("DB2".equalsIgnoreCase(db)) { 
      s = "jdbc:db2://" + host + ':' + port + '/' + sid; 
      Class.forName("com.ibm.db2.jcc.DB2Driver"); 
      conn = DriverManager.getConnection(s, user, pwd); 
     } else if ("ORACLE".equalsIgnoreCase(db)) { 
      if(isProcessed.equalsIgnoreCase("true")){ 
      s = "jdbc:oracle:thin:@//" + host + ':' + port + '/' + sid; 
      //s = "jdbc:oracle:thin:@" + host + ":" + port + ":" + sid; 
      Class.forName("oracle.jdbc.driver.OracleDriver"); 
      conn = DriverManager.getConnection(s,user,pwd); 
      }else{ 
       try{ 

        conn.close(); 
       }catch(Exception e){ 
        System.out.println("Aborted by user"); 
       } 

      } 
     } else { 
      throw new Exception("Error : Unsupported DataBase : " + db); 
     } 
    } catch (ClassNotFoundException e) { 
     throw new Exception("Unable to load Driver class ", e); 
    } 
    return conn; 
} 

被調用connect類離子...

public class HuaGPRSFileExecutor extends FileExecutor { 

protected void processFile(PathHandler output, FileStatus fileStatus) throws Exception { 
    LinkedHashMap<String, Object> record = HuawieGPRSParser.persefile(fileStatus); 
    for (String eventName : record.keySet()) { 
     if (eventName == null || "null".equals(eventName)) continue; 
     ArrayList<LinkedHashMap<String, Object>> events = (ArrayList<LinkedHashMap<String, Object>>) record.get(eventName); 
     try { 
      Connection conn = getConnection(); 
      new HuawieGPRSCDRHandler().populateDB(eventName, events, fileStatus, conn); 
      conn.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     try { 
      new HuawieGPRSCDRHandler().generateFile(output, events, fileStatus); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

'連接康恩=的getConnection(); ' 這是獲取空指針異常的行嗎? – anirudh

+0

是的,行 – Subho

+0

我可以打印一個適當的消息,而不是此stacktrace – Subho

回答

0
try { 
      Connection conn = getConnection(); 
      if(conn== null) 
       { 

     //You can either print the message saying 

//連接爲空或火災的自定義
//異常的情況下,你需要通過例外處理它

   } 
      else{ 

      new HuawieGPRSCDRHandler().populateDB(eventName, events, fileStatus, conn); 

       } 
      conn.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }