简体   繁体   English

使用Web服务从xml文档中获取数据时,出现空指针异常

[英]I am getting null pointer exception when i get the data from xml document using web service

String name= "Nsss";
String resulturl ="http://ssss/res/get?sid="+name+"";



DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(resulturl));
Document doc = db.parse(resulturl);
System.out.println("sssssssssssssssssssssssssssssssssssssssssssssssssss"+doc.getDoctype().getTextContent());

I am getting this exception. 我收到这个例外。

java.lang.NullPointerException at com.controller.StudentsResultsController.main(ResultsController.java:130) com.controller.StudentsResultsController.main(ResultsController.java:130)上的java.lang.NullPointerException

You should check to see what the resulturl variable is before you use it, but the other thing you need to address is: You are not using the InputSource at all. 在使用resulturl变量之前,应该检查一下它是什么,但是您需要解决的另一件事是:您根本没有使用InputSource The following is likely to work better than what you have: 以下内容可能会比您拥有的更好:

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(resulturl));
Document doc = db.parse(is);

EDIT 编辑

The Fatal Error is caused by the following: 致命错误是由以下原因引起的:

StringReader(resulturl) takes a string argument that must be XML, not a filename or a URL. StringReader(resulturl)接受一个字符串参数,该参数必须是XML,而不是文件名或URL。 The parser is reading the value of the string variable, resulturl, and failing immediately because an XML document may not begin with an h character. 解析器正在读取字符串变量的值resultURL,并立即失败,因为XML文档可能不是以h字符开头。

Try changing the above to: 尝试将以上内容更改为:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();          
Document doc = dBuilder.parse(new InputSource(resulturl));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Firebase读取用户数据时,为什么会出现Null指针异常? - Why am I getting a Null Pointer Exception when reading user data from Firebase? 为什么会出现空指针异常 - Why am I getting a null pointer exception 为什么我会收到Null指针异常? - why am i getting Null Pointer Exception? 我在 android 中收到 null 指针异常 - I am getting null pointer exception in android 为什么在Java中使用Map时会出现空指针异常? - Why am i getting null pointer exception when using map in java? 使用增强的for循环时为什么在此程序中出现空指针异常 - Why am I getting null pointer exception in this program when using enhanced for loop 将字符串的ArrayList从一个活动传递到另一个活动时,为什么会出现空指针异常? - Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another? 为什么我会得到null指针异常:从带有表单url编码的视图中获取值时为null? - Why do I get null pointer exception: null when getting value from view with form url encoded? 没有什么是空的,但我仍然收到空指针异常 - Nothing is null but I am still getting a null pointer exception 向 ArrayList 添加项目时,为什么会出现空指针异常 - Why am I getting null pointer exception when I add an item to ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM