0

I am getting an error while parsing a Document. It is showing a Null document

Error:[Request processing failed; nested exception java.lang.NullPointerException] with root cause java.lang.NullPointerException

String subBuildFile = "E:\Data Sync 4.4\WEB-INF\Temp_Project\Project_3";
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new InputSource(subBuildFile));
Node s = doc.getElementsByTagName("userList").item(1);
for(int i =2; i < doc.getElementsByTagName("user").getLength(); i++) {
    doc.getElementsByTagName("userList").item(i)
        .removeChild(doc.getElementsByTagName("userList").item(i));
}
Draken
  • 3,134
  • 13
  • 34
  • 54
mukta
  • 11
  • 4
  • i tried:DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(subBuildFile)); Node s = doc.getElementsByTagName("userList").item(1); for(int i =2; i – mukta Oct 25 '18 at 12:17
  • showing :[Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException – mukta Oct 25 '18 at 12:17
  • Sorry to say, but a problem with your question is that you are showing `null` code, just the `Exception`... Please add your code that throws this `Exception`. – deHaar Oct 25 '18 at 12:23
  • DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(subBuildFile)); Node s = doc.getElementsByTagName("userList").item(1); for(int i =2; i – mukta Oct 25 '18 at 12:30
  • in String subBuildFile="E:\Data Sync 4.4\WEB-INF\Temp_Project\Project_3" – mukta Oct 25 '18 at 12:31
  • What is the null pointer exception pointing to? So we can determine what is null. – xtratic Oct 25 '18 at 12:50
  • Please do not use comments to expand on your question: **edit** your question to add details. – Mark Rotteveel Oct 25 '18 at 13:19
  • @MarkRotteveel I don't think this is an NPE duplicate. OP at least knew that the `Document` was null and was causing the NPE but was just unsure of *why* the `Document` was null. – xtratic Oct 25 '18 at 13:35
  • @xtratic Given the current state of the question that is not obvious, and not really relevant. Alternatively, I would have chosen 'unclear what you're asking' or 'needs MCVE' as close reason. If the question is edited into shape with all relevant information in the question, then reopening can be considered. – Mark Rotteveel Oct 25 '18 at 13:37
  • Ah, yes, my edits haven't gone through yet. I'd copied the code in comments to the question. But at least his original statement "it is showing null document" shows that OP at least knows *what* is null. – xtratic Oct 25 '18 at 13:38
  • Without the full exception with stack trace, and the document, it's hard to say what the problem is. – Emmanuel Oct 25 '18 at 17:16

1 Answers1

0

Your problem is that you are using InputSource incorrectly.

You are calling new InputSource(String) and that constructor says this:

/**
 * Create a new input source with a system identifier.
 * ...
 * If the system identifier is a URL, it must be fully
 * resolved (it may not be a relative URL).
 *
 * @param systemId The system identifier (URI).
 */
 public InputSource (String systemId){...}

Instead use this constructor:

/** Create a new input source with a character stream. */
public InputSource (Reader characterStream) { ... }

And use it like this:

Document doc = dBuilder.parse(new InputSource(new StringReader(subBuildFile)));
xtratic
  • 4,600
  • 2
  • 14
  • 32
  • now it is showing :1:1: Content is not allowed in prolog. – mukta Oct 25 '18 at 13:01
  • Please accept this answer, since it answered your original question, and I can help with the rest. I'll need to see what the content of the file looks like. – xtratic Oct 25 '18 at 13:05