java.net.URI处的java.lang.NullPointerException错误

java.net.URI处的java.lang.NullPointerException错误,java,nullpointerexception,http-get,Java,Nullpointerexception,Http Get,我正在尝试通过Java访问一些URL 在我的代码中,我提供了服务器身份验证的凭据。我还提取了服务器上可用URL的列表(xml格式),并将它们放入一个数组中,在该数组中我可以自动调用列表中的每个URL 这是我的代码: import java.io.File; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.cl

我正在尝试通过Java访问一些URL

在我的代码中,我提供了服务器身份验证的凭据。我还提取了服务器上可用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()));
                }
            }
    }
}  
}
导入java.io.File;
导入org.apache.http.auth.AuthScope;
导入org.apache.http.auth.UsernamePasswordCredentials;
导入org.apache.http.client.CredentialsProvider;
导入org.apache.http.client.methods.CloseableHttpResponse;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.BasicCredentialsProvider;
导入org.apache.http.impl.client.CloseableHttpClient;
导入org.apache.http.impl.client.HttpClients;
导入org.apache.http.util.EntityUtils;
导入java.io.FileOutputStream;
导入java.lang.reflect.Array;
导入org.apache.http.HttpEntity;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.xpath.xpath;
导入javax.xml.xpath.XPathConstants;
导入javax.xml.xpath.XPathExpression;
导入javax.xml.xpath.XPathFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Node;
公共类客户端身份验证{
公共静态void main(字符串[]args)引发异常{
/*创建文件*/
File myFile=新文件(“C:/input.xml”);
/*定义凭据*/
CredentialsProvider credsProvider=新的BasicCredentialsProvider();
setCredentials(新的AuthScope(“localhost”,80),新的UsernamePasswordCredentials(“,”);
try(CloseableHttpClient httpclient=HttpClients.custom().setDefaultCredentialsProvider(credProvider.build()){
HttpGet HttpGet=新的HttpGet(“http://localhost/app/rest/buildTypes");
/*写入文件*/
try(CloseableHttpResponse response=httpclient.execute(newhttpget(“http://localhost/app/rest/buildTypes"))){
HttpEntity=response.getEntity();
如果(实体!=null){
try(FileOutputStream-outstream=newfileoutputstream(myFile)){
实体写入(超出范围);
}
}
}
/*显示执行力*/
System.out.println(“正在执行请求”+httpget.getRequestLine());
try(CloseableHttpResponse-response=httpclient.execute(httpget)){
System.out.println(“--------------------------------------------------------”;
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity());
}
}
/*从XML文件中提取所有生成ID*/
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdoc=builder.parse(“C:/input.xml”);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression expr=xpath.compile(“//buildTypes/buildType[@id]”);
NodeList nl=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
/*创建url并保存到数组中*/
字符串数组[]=新字符串[100];
对于(int i=0;i
输出错误为:

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)
线程“main”java.lang.NullPointerException中的异常 位于java.net.URI$Parser.parse(URI.java:3042) 位于java.net.URI。(URI.java:588) 创建(URI.java:850) 位于org.apache.http.client.methods.HttpGet.(HttpGet.java:66) 在ClientAuthentication.main(ClientAuthentication.java:85) Java返回:1生成失败(总时间:0秒)
我认为问题在于代码的最后部分(获取URL)。但我不知道问题出在哪里。有人能帮忙吗

String array[] = new String[100];
for (int i = 0; i < nl.getLength(); i++){
    ...
这将一直工作,直到您到达数组中剩余的
null


使用
新字符串[nl.getLength()]
实例化数组。既然你现在不需要更多了。(或更少)。

使用调试器并缩小到导致空指针的确切原因ClientAuthentication.java中第85行是什么?你真是个天才!谢谢你解决了这个问题!欢迎你@CassandraLehmann。你应该学会使用调试器。这个问题本地化并不复杂,所以你可以在短时间内自己解决。我只是说了编码,不知道有调试器。但我现在肯定会调查的。再次感谢!
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]);