0

For some reason, when I run the code below with eBay sold listings, I got java.lang.NullPointerException:

 Elements Listings = (Elements) document.getElementsByClass("s-item__details clearfix"); // Each box contains the deal info
    for (Element e : Listings) { // Within each box we would like to extract the listing' names and sold prices
        String listingName = e.select("h3.s-item__title s-item__title--has-tags").first().text();
        System.out.println(listingName);

If I remove the for loop, I will be able to display Listings. What I am trying to do is to extract the listings' names and sold prices. May I know what I am doing wrong please?

DiMario
  • 33
  • 1
  • 8
  • 2
    Please search for duplicates first before posting a common question. Had you done so, studied the duplicate and used the information that it contains, you'd have posted the stacktrace, indicated which line throws the exception, indicated which variable on that line is null, and why you think that it shouldn't, and without this information the question is not readily answerable – Hovercraft Full Of Eels Mar 14 '20 at 00:08
  • 2
    the stack trace of the error will tell you what line it's on; that's the place to start. Then experiment with printing values. My guess is you'll need to separate out the chained method calls on the e.select() line, because one of those methods, like maybe e.select(), is returning null for some of the listings. – Luke Mar 14 '20 at 05:50

0 Answers0