2017-10-19 111 views
-2

我試圖通過Java訪問某個URL。java.net.URI上的java.lang.NullPointerException錯誤

在我的代碼中,我提供了服務器身份驗證的憑據。我還提取了服務器上可用URL的列表(以xml形式),並將它們放入一個數組中,我可以自動調用列表中的每個URL。

這是我的代碼:

import java.io.File; 
import org.apache.http.auth.AuthScope; 
import org.apache.http.auth.UsernamePasswordCredentials; 
import org.apache.http.client.CredentialsProvider; 
import org.apache.http.client.methods.CloseableHttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.BasicCredentialsProvider; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClients; 
import org.apache.http.util.EntityUtils; 
import java.io.FileOutputStream; 
import java.lang.reflect.Array; 
import org.apache.http.HttpEntity; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathFactory; 
import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 
import org.w3c.dom.Node; 

public class ClientAuthentication { 

public static void main(String[] args) throws Exception { 
    /*CREATING FILE*/ 
    File myFile = new File("C:/input.xml"); 

    /*DEFINING CREDENTIALS*/ 
    CredentialsProvider credsProvider = new BasicCredentialsProvider(); 
    credsProvider.setCredentials(new AuthScope("localhost", 80),new UsernamePasswordCredentials(" ", " ")); 

    try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()){ 
     HttpGet httpget = new HttpGet("http://localhost/app/rest/buildTypes"); 
     /*WRITE TO FILE*/ 
     try (CloseableHttpResponse response = httpclient.execute(new HttpGet("http://localhost/app/rest/buildTypes"))){ 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       try (FileOutputStream outstream = new FileOutputStream(myFile)) { 
       entity.writeTo(outstream); 
       } 
      } 
     } 

     /*SHOW EXECUTION*/ 
     System.out.println("Executing request " + httpget.getRequestLine()); 
     try (CloseableHttpResponse response = httpclient.execute(httpget)) { 
      System.out.println("----------------------------------------"); 
      System.out.println(response.getStatusLine()); 
      System.out.println(EntityUtils.toString(response.getEntity())); 
     } 
    } 

    /*Extract all Build ID from XML file*/ 
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    Document doc = builder.parse("C:/input.xml"); 
    XPathFactory xPathfactory = XPathFactory.newInstance(); 
    XPath xpath = xPathfactory.newXPath(); 
    XPathExpression expr = xpath.compile("//buildTypes/buildType[@id]"); 
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); 

    /*Creating url and saving into array*/ 
    String array[] = new String[100]; 
    for (int i = 0; i < nl.getLength(); i++){ 
     Node currentItem = nl.item(i); 
     String key = currentItem.getAttributes().getNamedItem("id").getNodeValue(); 
     String pathing = "http://localhost/app/rest/" + (key); 
     System.out.println(pathing); 
     array[i] = pathing; 
    } 

    /*Getting the url*/ 
    int size = Array.getLength(array); 
    for (int i = 0; i < size; i++){ 
     try (CloseableHttpClient areq = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()){ 
       HttpGet httpget = new HttpGet(array[i]); 

       /*SHOW EXECUTION*/ 
       System.out.println("Executing request " + httpget.getRequestLine()); 
       try (CloseableHttpResponse response = areq.execute(httpget)) { 
        System.out.println("----------------------------------------"); 
        System.out.println(response.getStatusLine()); 
        System.out.println(EntityUtils.toString(response.getEntity())); 
       } 
      } 
    } 
} 
} 

輸出錯誤是:

Exception in thread "main" java.lang.NullPointerException 
    at java.net.URI$Parser.parse(URI.java:3042) 
    at java.net.URI.<init>(URI.java:588) 
    at java.net.URI.create(URI.java:850) 
    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66) 
    at ClientAuthentication.main(ClientAuthentication.java:85) 
Java returned: 1 BUILD FAILED (total time: 0 seconds) 

我認爲這個問題是在代碼的最後部分(獲取URL)。我不知道問題是什麼。任何人都可以幫忙嗎?

+2

使用調試器並縮小至*精確*引起空指針的原因 – notyou

+0

ClientAuthentication.java中的第85行是什麼? – Henry

回答

0
String array[] = new String[100]; 
for (int i = 0; i < nl.getLength(); i++){ 
    ... 

爲什麼使用100而不是nl.getLength

既然你以後遍歷這個數組來實例化的HttpGet,每一個最後的細胞沒有實例化仍然null,導致new HttpGet(array[i])拋出異常beause參數不能null

int size = Array.getLength(array); 
for (int i = 0; i < size; i++){ 
    try (CloseableHttpClient areq = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build()){ 
      HttpGet httpget = new HttpGet(array[i]); 

這將工作,直到您到達null保留在您的陣列中。

new String[nl.getLength()] Instanciate您的陣列。既然你現在你不需要更多。 (或更少)。

+0

你是天才!謝謝你解決了這個問題! –

+1

歡迎@CassandraLehmann。你應該學會使用調試器。這些問題不會侷限於本地化,所以您可以在短時間內自行修復此問題。 – AxelH

+0

我剛剛說過編碼,並不知道有一個調試器。但我現在肯定會研究它。再次感謝! –

相關問題