Java.lang.NullPointerException…
-
Hi guys, I am involved in a project which I have to scrape data from the internet I currently have three main classes called TripAdvisorXpath, DOMParser and IValueExtractor, running in three different packages the problem is when running the class called TripAdvisorXpath an exception called java.lang.NullPointerException is thrown tried many times to find where is the object generating and where is pointing to but could not solve it. Here is the code for TripAdvisorXpath class. package WebXPathSearcher; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Vector; import javax.swing.text.Document; import javax.swing.text.html.parser.DocumentPar… import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException… import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathExpression; import org.w3c.dom.Node; import org.w3c.dom.NodeList; //import org.w3c.dom.xpath.XPathExpression; import tutorialDOMParser.DOMParser; import IValueExtractor.IValueExtractor; import javax.swing.text.*; import org.w3c.dom.*; import org.xml.sax.InputSource; public class TripAdvisorXpath implements IValueExtractor { public Vector<String> getRequiredValues(Document doc) throws XPathExpressionException{ // Using and declaring the website's XPath XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); // Declaring XPath XPathExpression expr= (XPathExpression) xpath.compile("/html/body/div[3]/div[2]/… Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(nodes.item(i).getNode… System.out.println(nodes.item(i).getAttr… + "" + nodes.item(i).getAttributes().item(0).ge… // the attributes ... } NodeList childNodes = nodes.item(0).getChildNodes(); for (int i=0; i<childNodes.getLength();i++){ if(childNodes.item(i).getNodeName().equ… String price = childNodes.item(i).getChildNodes().item(… System.out.println("The data extracted is : " + price); } } return null; } public void main (String [] args)throws FileNotFoundException, XPathExpressionException{ TripAdvisorXpath t = new TripAdvisorXpath(); DOMParser parser = new DOMParser(new FileInputStream("C:/RuleBasedPopunder.ht… Document d = (Document) parser.getDocumentFromHTMLInputStream(); t.getRequiredValues(d); } } // </TripAdvisorXPath> Here is the DOMPArser class: package tutorialDOMParser; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import javax.xml.xpath.*; import org.w3c.tidy.Tidy; public class DOMParser { private InputStream is = null; public DOMParser(InputStream is){ this.is = is; } public DOMParser(FileInputStream fileInputStream) { // TODO Auto-generated constructor stub } public Document getDocumentFromHTMLInputStream(){ Tidy tidy = new Tidy();// this is the object of the parsing library JTidy. Document doc = tidy.parseDOM(is, null);// provide the input stream. return doc; } public static void main(String[] args) throws FileNotFoundException{ // We want to parse the fiel: ????? DOMParser parser = new DOMParser( new FileInputStream("C:/RuleBasedPopunder.ht… Document d = parser.getDocumentFromHTMLInputStream(); // This is the root of the HTML document. In this case of the <HTML> element... Node root = d.getDocumentElement(); // Now checking the actual document ourselves System.out.println(root.getNodeName()); System.out.println("the root value is null... this is because it does not refer to a value "); System.out.println("'" + root.getNodeName() + "'"); } } // </DOMParser> And Here is the IValueExtractor class: package IValueExtractor; import java.util.Vector; import javax.swing.text.Document; import javax.xml.xpath.XPathExpressionException… // assuming that the vector is a collection of strings public interface IValueExtractor { Vector<String> getRequiredValues(Document doc) throws XPathExpressionException; } //</IValueExtractor> P.S.: It looks quite a lot but when you past into eclipse it goes simpler. I resume as much as possible but let me know any further details you may need. Thanks
-
Answer:
If you are using Eclipse, the debugger should show you where the error is happening. If it isn't happening in your code, you can look at the call stack and find what line of your code triggered the exception. You get a NullPointerException anytime you try to reference a pointer that is null. You have a number of lines of code here that call functions that return an object, but you have no tests for that object being null. Most likely, you are getting back a null pointer from one of these calls, then referencing it yourself or passing it to a library function assumes you don't pass in null. For example: Here: Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { What if evaluate returns null? Then nodes.getLength() will throw that exception. You should be testing for that error: if(result == null) // Error...DO SOMETHING else // Ok to do something with result.
Just Me at Yahoo! Answers Visit the source
Other answers
Every time I've gotten a null pointer exception it's told me exactly where the object is. As you know, that exception means that you're trying to use an object whose identifier has been defined but the object has not been constructed. This class might be the correct code where the exception occurred but it's not an object of this class non-constructed in this class that is the problem. The one place that an object is referred to is here: TripAdvisorXpath t = new TripAdvisorXpath(); which is certainly a costructor invokation. Copy & paste more details of the exactly error message. It will certainly tell you exactly where the error is.
modulo_function
Related Q & A:
- Where To Get Best AntiVirus For Java Mobile?Best solution by antivirus.com
- What is Java?Best solution by Yahoo! Answers
- How to Implement Gateway Service something similar to Oracle API gateway Using Java and Java based Open Source frameworks only?Best solution by Quora
- Why do I have a NullPointerException?Best solution by Stack Overflow
- Difference between Java 2 and Java 6?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.