Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:35:28+00:00 2026-06-04T15:35:28+00:00

I am making a simple logging system in android. I am following this tutorial

  • 0

I am making a simple logging system in android. I am following this tutorial. While testing my app I am keep getting NullException error. This error is on this line if(json.getString(KEY_SUCCESS) != null) in LoginScreensActivity below. I have no idea why is json is null. Please help me where am I making mistake.

Here is my code

function which triggers when login button is clicked. This is LoginScreensActivity.java class

btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.loginUser(email,password);

            try {
                if(json.getString(KEY_SUCCESS) != null) {
                    loginErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res) == 1) {
                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);

                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);

                        // Close Login Screen
                        finish();
                    }
                } else {
                    Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

loginUSer function which is triggered in above code

public JSONObject loginUser(String email, String password) {
    //Toast.makeText(context, "loginUser function", Toast.LENGTH_LONG).show();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));

    JSONObject json = jsonParser.getJSONFromUrl(loginUrl, params);
    //Log.e("JSON", json.toString());
    //Log.d("TAG", json.toString());
    return json;
}

here is my JSONPareser class

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {

}

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;

        while((line = reader.readLine()) != null) {
            sb.append(line + "n");
        }

        is.close();
        json = sb.toString();
        Log.e("JSON",json);
    } catch (Exception e) {
        //e.printStackTrace();
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;
}
}

here is my logcat

05-26 20:16:52.658: I/Process(31217): Sending signal. PID: 31217 SIG: 9
05-26 20:16:59.793: D/dalvikvm(31361): GC_EXTERNAL_ALLOC freed 41K, 50% free 2725K/5379K, external 0K/0K, paused 31ms
05-26 20:16:59.843: D/dalvikvm(31361): GC_EXTERNAL_ALLOC freed 7K, 50% free 2743K/5379K, external 455K/518K, paused 32ms
05-26 20:16:59.973: D/CLIPBOARD(31361): Hide Clipboard dialog at Starting input: finished by someone else... !
05-26 20:17:06.883: W/System.err(31361): java.net.UnknownHostException: zafarsaleem.info
05-26 20:17:06.883: W/System.err(31361):    at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
05-26 20:17:06.883: W/System.err(31361):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
05-26 20:17:06.883: W/System.err(31361):    at java.net.InetAddress.getAllByName(InetAddress.java:256)
05-26 20:17:06.883: W/System.err(31361):    at    org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
05-26 20:17:06.883: W/System.err(31361):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-26 20:17:06.883: W/System.err(31361):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-26 20:17:06.883: W/System.err(31361):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
05-26 20:17:06.883: W/System.err(31361):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-26 20:17:06.888: W/System.err(31361):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-26 20:17:06.888: W/System.err(31361):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-26 20:17:06.888: W/System.err(31361):    at library.JSONParser.getJSONFromUrl(JSONParser.java:38)
05-26 20:17:06.888: W/System.err(31361):    at library.UserFunctions.loginUser(UserFunctions.java:37)
05-26 20:17:06.888: W/System.err(31361):    at com.zafar.loginscreens.LoginScreensActivity$1.onClick(LoginScreensActivity.java:48)
05-26 20:17:06.888: W/System.err(31361):    at android.view.View.performClick(View.java:2538)
05-26 20:17:06.888: W/System.err(31361):    at android.view.View$PerformClick.run(View.java:9152)
05-26 20:17:06.888: W/System.err(31361):    at android.os.Handler.handleCallback(Handler.java:587)
05-26 20:17:06.888: W/System.err(31361):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 20:17:06.888: W/System.err(31361):    at android.os.Looper.loop(Looper.java:130)
05-26 20:17:06.888: W/System.err(31361):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-26 20:17:06.888: W/System.err(31361):    at java.lang.reflect.Method.invokeNative(Native Method)
05-26 20:17:06.888: W/System.err(31361):    at java.lang.reflect.Method.invoke(Method.java:507)
05-26 20:17:06.888: W/System.err(31361):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-26 20:17:06.888: W/System.err(31361):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-26 20:17:06.888: W/System.err(31361):    at dalvik.system.NativeStart.main(Native Method)
05-26 20:17:06.893: E/Buffer Error(31361): Error converting result java.lang.NullPointerException
05-26 20:17:06.893: E/JSON Parser(31361): Error parsing data org.json.JSONException: End of input at character 0 of 
05-26 20:17:06.893: D/AndroidRuntime(31361): Shutting down VM
05-26 20:17:06.893: W/dalvikvm(31361): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-26 20:17:06.893: E/AndroidRuntime(31361): FATAL EXCEPTION: main
05-26 20:17:06.893: E/AndroidRuntime(31361): java.lang.NullPointerException
05-26 20:17:06.893: E/AndroidRuntime(31361):    at com.zafar.loginscreens.LoginScreensActivity$1.onClick(LoginScreensActivity.java:51)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at android.view.View.performClick(View.java:2538)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at android.view.View$PerformClick.run(View.java:9152)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at android.os.Handler.handleCallback(Handler.java:587)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at android.os.Looper.loop(Looper.java:130)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at java.lang.reflect.Method.invokeNative(Native Method)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at java.lang.reflect.Method.invoke(Method.java:507)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-26 20:17:06.893: E/AndroidRuntime(31361):    at dalvik.system.NativeStart.main(Native Method)

Update

After adding if(json != null && json.getString(KEY_SUCCESS) != null) { I get following logcat

05-26 20:44:46.243: W/System.err(32680): java.net.UnknownHostException: zafarsaleem.info
05-26 20:44:46.243: W/System.err(32680):    at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
05-26 20:44:46.243: W/System.err(32680):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
05-26 20:44:46.243: W/System.err(32680):    at java.net.InetAddress.getAllByName(InetAddress.java:256)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-26 20:44:46.248: W/System.err(32680):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-26 20:44:46.248: W/System.err(32680):    at library.JSONParser.getJSONFromUrl(JSONParser.java:38)
05-26 20:44:46.248: W/System.err(32680):    at library.UserFunctions.loginUser(UserFunctions.java:37)
05-26 20:44:46.248: W/System.err(32680):    at com.zafar.loginscreens.LoginScreensActivity$1.onClick(LoginScreensActivity.java:49)
05-26 20:44:46.248: W/System.err(32680):    at android.view.View.performClick(View.java:2538)
05-26 20:44:46.248: W/System.err(32680):    at android.view.View$PerformClick.run(View.java:9152)
05-26 20:44:46.248: W/System.err(32680):    at android.os.Handler.handleCallback(Handler.java:587)
05-26 20:44:46.248: W/System.err(32680):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 20:44:46.248: W/System.err(32680):    at android.os.Looper.loop(Looper.java:130)
05-26 20:44:46.248: W/System.err(32680):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-26 20:44:46.248: W/System.err(32680):    at java.lang.reflect.Method.invokeNative(Native Method)
05-26 20:44:46.248: W/System.err(32680):    at java.lang.reflect.Method.invoke(Method.java:507)
05-26 20:44:46.248: W/System.err(32680):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-26 20:44:46.248: W/System.err(32680):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-26 20:44:46.248: W/System.err(32680):    at dalvik.system.NativeStart.main(Native Method)
05-26 20:44:46.248: E/Buffer Error(32680): Error converting result java.lang.NullPointerException
05-26 20:44:46.248: E/JSON Parser(32680): Error parsing data org.json.JSONException: End of input at character 0 of 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:35:29+00:00Added an answer on June 4, 2026 at 3:35 pm

    The root cause of your trouble is a UnknownHostException, it seems the email EditText doesn’t link to a valid URL which returns a null value for json causing the NullPointerException.

    What are you entering in email?

    Addition

    To be explicit, the tutorial code:

    if(json.getString(KEY_SUCCESS) != null) {
    

    only checks if the String returned from getString() is not null. In your case json is null so you would need a test like this:

    if(json != null && json.getString(KEY_SUCCESS) != null) {
    

    to detect the null pointer and gracefully exit your app.

    Addition 2

    As the User Functions Class states, if your server is hosted online you need to change these lines of code in your UserFunctions class:

    private static String loginURL = "http://10.0.2.2/ah_login_api/";
    private static String registerURL = "http://10.0.2.2/ah_login_api/";
    

    If you were Stack Overflow you could try something like:

    private static String loginURL = "http://www.stackoverflow.com/ah_login_api/";
    private static String registerURL = "http://www.stackoverflow.com/ah_login_api/";
    

    But you are not Stack Overflow, of course, you need to point these URLs to your server and path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making simple tree structure and in the tutorial is following: Build cache:
I'm making a simple fractal viewing app for Android, just for fun. I'm also
Im making a simple alternative to the default linux system monitor. Im looking to
I’m making a simple LOB app which loads data from an XML file and
Good afternoon. I have been making a small openGL based app for android that
Making a simple map app, plan on adding buttons to mark specific locations But
Iam making a simple app, I display pdf file with some text and images
I am making a simple PHP rest service, I am calling this service with
Right now I'm getting this exception from a simple JMS client I wrote to
I am making simple connection between two table - first one called users has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.