0

I'm trying to login to a bank website using jsoup but I'm getting a NullPointerException when the line that prints the cookies is removed from the code. I know I should verify if the response is null but the problem is that the program works when I print the cookies. Here is the code:

private class GetHomeTask extends AsyncTask<Void, Void, BitmapDrawable> {
    protected BitmapDrawable doInBackground(Void... nothing) {
        try {

                Response home = Jsoup.connect(HOME_URL + "LoginKaptcha.jpg")
                        .userAgent(USER_AGENT)
                        .validateTLSCertificates(false)
                        .ignoreContentType(true)
                        .method(Method.GET)
                        .execute();
                cookies = home.cookies();
                //System.out.println(cookies); --> if commented, I get a NullPointerException when parsing the login page as pointed below.
                ByteArrayInputStream inputStream = new ByteArrayInputStream(home.bodyAsBytes());
                Bitmap bMap = BitmapFactory.decodeStream(inputStream);
                return new BitmapDrawable(getApplicationContext().getResources(), bMap);
            } catch (IOException e) {

            }
            return null;
        }

    @Override
    protected void onPostExecute(BitmapDrawable bMap) {
        setImage(bMap);
    }
}

public void loginAction(View view) {

    String[] userDetails = new String[3];
    EditText userIdText = (EditText) findViewById(R.id.userIdField);
    userDetails[0] = userIdText.getText().toString();
    EditText userPasswordText = (EditText) findViewById(R.id.userPasswordField);
    userDetails[1] = userPasswordText.getText().toString();
    EditText captchaText = (EditText) findViewById(R.id.captchaField);
    userDetails[2] = captchaText.getText().toString();

    new LoginTask().execute(userDetails);

}

private class LoginTask extends AsyncTask<String[], Void, Response> {
    protected Response doInBackground(String[]... userDetails) {
        Response login = null;
        try {
            login = Jsoup.connect(HOME_URL + "login.action")
                    .data("cardHolder.userId", userDetails[0][0])
                    .data("cardHolder.userPassword", userDetails[0][1])
                    .data("captchaResponse1", userDetails[0][2])
                    .data("instName", instName)
                    .data("__checkbox_rememberMe", "true")
                    .userAgent(USER_AGENT)
                    .cookies(cookies)
                    .referrer(HOME_URL)
                    .validateTLSCertificates(false)
                    .method(Method.POST)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return login;
    }

    @Override
    protected void onPostExecute(Response login) {
        String userName = null;
        try {
            // --> NullPointerException (login is null) occurs here only when I comment the line that prints the cookies.
            userName = login.parse().getElementsByClass("pageTitle").text();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String token = login.url().toString().substring(54);
        cookies = login.cookies();

        Intent intent = new Intent(getApplicationContext(), DisplaySummaryActivity.class);
        intent.putExtra(EXTRA_MESSAGE, userName + "&" + token);
        startActivity(intent);
    }
}

I tried to search the error but nothing is similar to this. Does anyone have an idea of what is the problem? Thank you.

joaopaulopaiva
  • 363
  • 3
  • 16
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Oct 05 '15 at 18:19
  • I know what is a NullPointerException, the problem is why a print in the middle of the code is affecting it. – joaopaulopaiva Oct 05 '15 at 18:22
  • you'll probably find out why if you find where this NPE is. – njzk2 Oct 05 '15 at 18:31
  • 1
    right now, it is not possible to answer this question, because you don't give any information about it. (stacktrace, pointer to the line that causes it, result of your own debugging to see what variable is null and why.) and anyway, there is a good chance that by doing those things and providing those information you will find the solution yourself. – njzk2 Oct 05 '15 at 18:33
  • one place to start would be the exception that is inevitably thrown by the LoginTask and caught the `catch (IOException)` – njzk2 Oct 05 '15 at 18:35
  • another thing would be to not ignore the first ioexception in `catch (IOException e) { }` – njzk2 Oct 05 '15 at 18:36
  • I found the solution. The timeout for the connection was set to default (3s), so I've changed it to 10s with `Jsoup.connect(url).timeout(10*1000)` and it worked. – joaopaulopaiva Oct 05 '15 at 20:11
  • But I still don't know why a print in the middle of the code was making it work. – joaopaulopaiva Oct 05 '15 at 20:12

1 Answers1

0

The problem was with the connection timeout. Jsoup uses 3s as default, so I've changed it to 10s using Jsoup.connect(url).timeout(10*1000).

joaopaulopaiva
  • 363
  • 3
  • 16