-3

I am working on login page when internet connection is off it shows me that incorrect email and password but when internet connection is on and i enter the wrong login email and password app stops. I don't know how to solve this please help me.

this is my code :

  @Override
    protected String doInBackground(String... params) {
        String login_email = params[0];
        String login_password = params[1];

        try {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String data = URLEncoder.encode("login_email", "UTF-8") + "=" + URLEncoder.encode(login_email, "UTF-8") + "&" +
                    URLEncoder.encode("login_password", "UTF-8") + "=" + URLEncoder.encode(login_password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            // String response = "";
            // JSONString = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSONString = bufferedReader.readLine()) != null) {
                // response += JSONString;
                stringBuilder.append(JSONString + "\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        loading.dismiss();
        if (result != null) {
            ParseJson(result);
        }
        else {
            Toast.makeText(Login.this, "Incorrect Email and Password", Toast.LENGTH_LONG).show();
        }


    }

LOGCAT:

05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime: Process: com.example.mubbasher.howdy_sendgreetings, PID: 26814
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:  Caused by: java.lang.NullPointerException
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at libcore.net.UriCodec.encode(UriCodec.java:132)   
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.net.URLEncoder.encode(URLEncoder.java:57)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at com.example.mubbasher.howdy_sendgreetings.NewsFeedFragment$BackgroundSearch.doInBackground(NewsFeedFragment.java:109)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at com.example.mubbasher.howdy_sendgreetings.NewsFeedFragment$BackgroundSearch.doInBackground(NewsFeedFragment.java:89)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:841) 
bebo
  • 73
  • 7
  • 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) – JimmyB May 02 '16 at 18:05
  • sir i have studied all these things but my problem is different its json problem .. @JimmyB if u can't help someone then don't convince others to stay away – bebo May 02 '16 at 18:08
  • 2
    No, your problem is not different. Debug your code and let us know *which* field or variable is null and causes the exception. – JimmyB May 02 '16 at 18:10
  • @JimmyB Being dismissive is not helpful. His code is not causing the null ref exception, so it might not be as obvious especially if OP is a newer developer. He's passing a null value into a framework method that is throwing the exception. – Rich May 02 '16 at 18:13
  • I already told everything in description of my question. it only shows the exception when i entered the wrong values. i am confused how to check json is null or not because when internet connection is off it shows the toast of incorrect email and password but when connection is on .. it stopped @JimmyB – bebo May 02 '16 at 18:17
  • @Rich All right, I'll leave it to you to explain where the `encode()` call is and which variables may be the erroneous input and that apparently "wrong login" somehow means `null` user name and/or password. – JimmyB May 02 '16 at 18:18
  • Although I'm curious as to why `doInBackground` appears twice in the stack trace. – JimmyB May 02 '16 at 18:21
  • can u please tell me the solution of this? @JimmyB because i don't know how to solve it – bebo May 02 '16 at 18:23
  • @bebo It would certainly help us to know which lines `NewsFeedFragment.java:109` and `NewsFeedFragment.java:89` are because the exception comes from these lines. – JimmyB May 02 '16 at 18:23
  • line 109: StringBuilder stringBuilder = new StringBuilder(); And and 89 is backgroundSearch class line in which doInBackground and other functions are described @JimmyB – bebo May 02 '16 at 18:28
  • I think you have modified the code since you captured the stack trace and line numbers don't match anymore. Let your current code run and check which lines are referred to in the new stack trace. – JimmyB May 02 '16 at 18:33
  • Newsfeed fragment is in another class which will show after successful login. if you tell me how to check the json result is null or not it will solve my problem @JimmyB – bebo May 02 '16 at 18:35
  • and can you please remove the duplicate mark so that others help me . please @JimmyB – bebo May 02 '16 at 18:38
  • Don't worry about that "duplicate", it won't keep others from looking at your question. But still, make sure you know how to find the cause for a `NullPointerException` by yourself, because you will encounter those over and over again during development. – JimmyB May 02 '16 at 18:41

1 Answers1

1

Always look for the "caused by" in the stack trace

05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:  Caused by: java.lang.NullPointerException
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at libcore.net.UriCodec.encode(UriCodec.java:132)   
05-02 23:01:19.103 26814-29816/com.example.mubbasher.howdy_sendgreetings E/AndroidRuntime:     at java.net.URLEncoder.encode(URLEncoder.java:57)

If you look at the source of UriCodec.encode, you'll see that you can't pass a null string because the inner code calls s.length() without null checking s.

public final String encode(String s, Charset charset) {
    // Guess a bit larger for encoded form
    StringBuilder builder = new StringBuilder(s.length() + 16);
    appendEncoded(builder, s, charset, false);
    return builder.toString();
}

source:

https://android.googlesource.com/platform/libcore/+/fe39951ed36353002263aefef7963a8c8b9c6441/luni/src/main/java/libcore/net/UriCodec.java#130

UPDATE:

The short answer is this: you can't pass a null String to UrlEncoder.encode because this method internally calls UriCodec.encode which accesses a length() method on the String without null checking it. If you need clarification on that, then Jimmy is correct that you need to read the link he posted as the first comment. The solution should be that you either null check the string first, or create a wrapper method that returns an empty string if the input is null or calls UrlEncoder.encode if it's not null or empty.

Rich
  • 36,270
  • 31
  • 115
  • 154