0

I have a problem that I do not know how to solve. While fetching data I get NPE. It is weird, because for other categories of book it works normally.

String romancesCategoryEmpikURL = "https://www.empik.com/ksiazki/poradniki";
        Document document = Jsoup.connect(romancesCategoryEmpikURL).get();

        List<Element> siteElements = document.select("div.productBox__info");
        List<Book> romanceCategoryBooks = new ArrayList<>();

        for (int i = 0; i < 15; i++) {
            String author = siteElements.get(i).select("span > a").first().ownText();

            romanceCategoryBooks.add(new Book.BookBuilder()
                    .withAuthor(author)
                    .withPrice(price)
                    .withTitle(title)
                    .withProductID(productID)
                    .withBookURL(BookURL)
                    .build());
        }

NPE occurs with fetching author from site: https://www.empik.com/ksiazki/poradniki

HTML code:

<div class="productBox__info">
<a href="/jak-uratowac-swiat-czyli-co-dobrego-mozesz-zrobic-dla-planety-szpura-areta,p1223701396,ksiazka-p" class="productBox seoTitle" title="Jak uratować świat? Czyli co dobrego możesz zrobić dla planety &nbsp;-&nbsp;Szpura Areta" data-product-id="p1223701396">
<span class="productBox__title">
<span class="productBox__number">1</span>
Jak uratować świat? Czyli co dobrego możesz zrobić dla planety
</span>
</a>
<span class="productBox__subtitle">
<a href="/szukaj/produkt?author=szpura+areta" class="smartAuthor" title="Szpura Areta - wszystkie produkty">
Szpura Areta </a>
</span>
<div class="rating">
<ul class="ratingStars"><li class="rate"><i class="fa fa-fw fa-star active"></i></li><li class="rate"><i class="fa fa-fw fa-star active"></i></li><li class="rate"><i class="fa fa-fw fa-star active"></i></li><li class="rate"><i class="fa fa-fw fa-star active"></i></li><li class="rate"><i class="fa fa-fw fa-star active"></i></li></ul>
<div class="score">
4.7/5
</div>
</div>
<div class="productBox__price">
<div class="productBox__priceItem productBox__priceItem--promotion ta-productlist-price ">
37,49&nbsp;zł </div>
<div class="productBox__priceItem productBox__priceItem--old ta-productlist-oldprice">
49,99&nbsp;zł </div>
</div>
</div>

I want to fetch author which is Szpura Areta.

mara122
  • 313
  • 3
  • 10
  • When you get a NPE on a line with chained method calls, you need to un-chain those methods to see what is causing the NPE and why -- you haven't done this yet, not good. – Hovercraft Full Of Eels Jul 21 '19 at 20:35
  • Do this and you'll see that sometimes there are no child elements and `.first()` will return null. You need to test for this before trying to extract text. – Hovercraft Full Of Eels Jul 21 '19 at 20:35
  • @HovercraftFullOfEels I don't get it. When I go with `.first()` it prints well, then I'm trying to add `.text()` or `.ownText()'` and it prints NPE. – mara122 Jul 21 '19 at 20:43
  • Exactly -- `.first()` sometimes *returns null*. This does not throw a NPE, but if you try to use the return value by calling `.text()` or `.ownText()`, the NPE is thrown. This is basic NPE debugging, and I recommend strongly that you fully read the answer to the 2nd duplicate used to close your question so you understand it well. – Hovercraft Full Of Eels Jul 21 '19 at 20:46
  • Nevermind. I see now, that only for this category `.first()` returns null. That's what confused me. – mara122 Jul 21 '19 at 20:51

0 Answers0