0

I'm trying to write a code that will open multiple URLs that require authentication and parse the text of the webpage for a SUCCESS message. If I supply the username and password in the authentication pop up, then the rest will go through without having to supply authentication every time. Right now, my authentication class looks like so:

public class CustomAuthenticator extends Authenticator {
    protected PasswordAuthentication getPasswordAuthentication() {
        String username = "********";
        String password = "********";
        return new PasswordAuthentication(username, password.toCharArray());
    }
}

And my URL parsing method is:

public static String urlSuccessUnknown(String url) {
        Document doc;
        String res = null;
        Authenticator.setDefault(new CustomAuthenticator());
        try {
            doc = Jsoup.connect(url).get();
            res = doc.body().text();
            if(res.indexOf("SUCCESS") >= 0)
                return "SUCCESS";
            else if(!(res.indexOf("SUCCESS") >= 0))
                return "FLAG";
        } 
        catch (Exception e) { e.printStackTrace(); }
        return "Unable to correctly parse";
    }

Whenever I test this with one URL, I receive a "java.lang.NullPointerException" at the line "res = doc.body().text();" and two other lines in other parts of my code. What am I doing wrong?

Also, when the webpage is opened, it asks for the username and password in a pop-up window instead of embedded in the page. I just wanted to include this in case a CustomAuthenticator is not the correct way to supply the username and password. Thank you for your time.

JMV12
  • 965
  • 1
  • 20
  • 52
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Ken White Nov 06 '17 at 18:01
  • I understand what a NullPointerException is, it's just I cannot figure out how to fix it in this situation. This is the first time I've tried to use an Authenticator when opening URLs through java. – JMV12 Nov 06 '17 at 18:08
  • You missed the *and how do I fix it* part of the dupe I linked. – Ken White Nov 06 '17 at 18:09
  • I'll see what I can do with it. I'm sorry, just also don't know if I set the Authenticator up correctly and if it's working as intended. – JMV12 Nov 06 '17 at 18:10

0 Answers0