java.lang.NullPointerException 在 java.net.URI 错误
java.lang.NullPointerException at java.net.URI error
我正在尝试通过 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()));
}
}
}
}
}
输出错误为:
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)。我不知道问题是什么。有人可以帮忙吗?
String array[] = new String[100];
for (int i = 0; i < nl.getLength(); i++){
...
您为什么使用 100
而不是 nl.getLength
?
由于您稍后迭代此数组以实例化 HttpGet
,每个未实例化的最后一个单元格仍然是 null
,导致 new HttpGet(array[i])
抛出异常,因为参数不能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()]
实例化您的数组。既然你现在不需要更多。 (或更少)。
我正在尝试通过 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()));
}
}
}
}
}
输出错误为:
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)。我不知道问题是什么。有人可以帮忙吗?
String array[] = new String[100];
for (int i = 0; i < nl.getLength(); i++){
...
您为什么使用 100
而不是 nl.getLength
?
由于您稍后迭代此数组以实例化 HttpGet
,每个未实例化的最后一个单元格仍然是 null
,导致 new HttpGet(array[i])
抛出异常,因为参数不能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()]
实例化您的数组。既然你现在不需要更多。 (或更少)。