2013-08-05 29 views
1

我一直在嘗試通過DocumentBuilder.Parse解析一些XML:爲什麼DocumentBuilder Parse在Eclipse中工作正常,但不是IntelliJ IDEA?

try 
    { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse response = httpClient.execute(httpPost, localContext); 
     InputStream in = response.getEntity().getContent(); 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = builder.parse(in); 
     return doc; 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

這工作在Eclipse罰款,並返回Document

然而,IntelliJ IDEA的運行代碼打Document doc = builder.parse(in);然後跳過直接過去整個try/catch語句並在try/catch語句後直接返回null(或其他代碼可能會在那裏)。

它沒有拋出任何異常,也沒有提供任何跡象表明它爲什麼會失敗!

有沒有人知道爲什麼會發生這種情況?我真的不想回到Eclipse

編輯 - 演示:

 try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse response = httpClient.execute(httpPost, localContext); 
     InputStream in = response.getEntity().getContent(); 
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     Document doc = builder.parse(in); //if we step through to here, this line executes then goes directly to "x" below 
     return doc; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; //x 

還試圖加入以下捕獲錯誤,這些都不被擊中之一:

  builder.setErrorHandler(new ErrorHandler() { 
      @Override 
      public void error(SAXParseException arg0) throws SAXException 
      { 
       throw arg0; 
      } 

      @Override 
      public void fatalError(SAXParseException arg0) throws SAXException 
      { 
       throw arg0; 
      } 

      @Override 
      public void warning(SAXParseException arg0) throws SAXException 
      { 
       throw arg0; 
      } 
     }); 

EDIT 2 :進口

import java.io.InputStream; 
import java.util.ArrayList; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.BasicHttpContext; 
import org.apache.http.protocol.HttpContext; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 

import com.google.android.gms.maps.model.LatLng; 

import android.content.Context; 
import android.util.Log; 
+0

我會失效理念緩存(見行動迅速菜單'的失效caches'),重建項目('重建project'),重新部署並再試一次。另外''catch'塊中的Log.e(「」,「Exception」,e)'可能有助於診斷。 – 2013-08-05 05:53:15

+0

喜RC - 感謝清理後,是清晨;)我試着做了這一點,但遺憾的是它還是直接跳轉過去的回報。我已經更新的問題,以進一步展示與 –

回答

0

(對不起,此評論失控!)

什麼是您的JRE?此外,你可以粘貼你的進口這個類?從根本上說,兩個IDE在相同的JVM上運行字節碼,所以這聽起來像是一個構建路徑問題(缺少jar /備用源)。你應該看到異常點擊(儘管我的子類Throwable,而不是在我的捕獲異常)。

http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.io.InputStream)。檢查您的輸入源爲空源。拋出:IllegalArgumentException應該在一個空打印,然後(在

看你的代碼:

try { 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpPost httpPost = new HttpPost(url); 
    HttpResponse response = httpClient.execute(httpPost, localContext); 
    InputStream in = response.getEntity().getContent(); 
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); //**<--What class is this?** 
    Document doc = builder.parse(in); //if we step through to here, this line executes then goes directly to "x" below 
    return doc; 
} catch (Exception e) { //<-- Does not catch Error 
    e.printStackTrace(); 
} 
return null; //x 

嘗試這樣做:

try 
{ 
    //Your code to read the document 
} 
catch (Throwable t) 
{ 
    t.printStackTrace(); //Check your output 
    //Set your breakpoint HERE 
    throw new RuntimeException(t.getMessage(), t); //Check the ExceptionHandler for the application. 
} 

我建議你的工廠ISN」 t時的工廠,你認爲這是OR,你讓你的代碼之外的錯誤類東西吞下例外。有一個很好的機會你的構建路徑設置不同。

+0

既JDK 1.6和1.7(兩個不同的開發機)試過了,都表現出了同樣的問題。將導入添加到原始問題 –

+0

如果將有問題的實體替換爲您知道有效的測試文件,這是否可行? –

+0

出於某種原因,只是註釋掉我的代碼,粘貼在你的try/catch語句的版本和彈出我的代碼早在嘗試了「解決」問題(即代碼現在可以正確執行)。然而,從來沒有發現任何東西被捕獲 –

相關問題