[英]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.