-7

sorry for expanding NullPointerException flood :D

I have read tons of questions about NullPointerException but I can't figure out where is problem in my code.

problematic line:

if(userAgent.doc.innerHTML().contains("haha")

I tried String x = userAgent.doc.innerHTML(); and to use condition on the next line but is there is still: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.jaunt.Document.innerHTML()' on a null object reference

Please any idea what I did wrong?

Surrounding code:

private class AT extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            UserAgent userAgent = new UserAgent();
            toSearch += 50;
            while (visited.size() < toSearch) {
                try {
                    userAgent.visit(currentUrl);
                    Elements elements = userAgent.doc.findEvery("<a href>");
                    for (Element e : elements) {
                        String url = e.getAt("href");
                        if (!toVisit.contains(url) && !visited.contains(url) && url.contains(stayAt))
                            toVisit.add(url);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                visited.add(currentUrl);
                if(userAgent.doc.innerHTML().contains(key))
                    recipes.add(currentUrl);
                toVisit.remove(0);
                currentUrl = toVisit.get(0);
            }
            return null;
        }
    }

Thanks a lot for advice! :)

  • Could you please share stack trace – Gaurav Dhiman May 07 '20 at 08:21
  • Have you checked where the NPE is by reading the stack trace? Have you tried using your debugger? – akuzminykh May 07 '20 at 08:21
  • Welcome to Stackoverflow. Did you try debugging your code? It is often a great way to discover a bug in your code. Moreover, you should mention what you tried to solve this bug. – D. Lawrence May 07 '20 at 08:21
  • 3
    userAgent.doc is null – Naan May 07 '20 at 08:22
  • I'd guess that `userAgent.visit(currentUrl);` throws a `ResponseException` which you catch as if nothing happened. You need to find out why it fails. Something wrong with `currentUrl`? Something wrong with your app's internet permission? Maybe you are trying to access a HTTP URL and not HTTPS and recent Android versions by default refuse to do that? – Markus Kauppinen May 07 '20 at 08:29

1 Answers1

0

you doc object in userAgent is null.

Add null check validation userAgent.doc != null while you try to add currentUrl in recipes :

 protected Void doInBackground(Void... voids) {
        UserAgent userAgent = new UserAgent();
        toSearch += 50;
        while (visited.size() < toSearch) {
            try {
                userAgent.visit(currentUrl);
                Elements elements = userAgent.doc.findEvery("<a href>");
                for (Element e : elements) {
                    String url = e.getAt("href");
                    if (!toVisit.contains(url) && !visited.contains(url) && url.contains(stayAt))
                        toVisit.add(url);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            visited.add(currentUrl);
            if(userAgent.doc != null && userAgent.doc.innerHTML().contains(key))
                recipes.add(currentUrl);
            toVisit.remove(0);
            currentUrl = toVisit.get(0);
        }
        return null;
    }
Nilesh B
  • 937
  • 1
  • 9
  • 14
  • thank you! it works :) although now i get runtimeException Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at the very next line but I'm gonna handle it :D – haboiabo May 07 '20 at 08:50