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 9171409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:09:32+00:00 2026-06-17T16:09:32+00:00

i have activity login package com.app.DatabaseSample; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog;

  • 0

i have activity login

package com.app.DatabaseSample;


import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.app.library.DatabaseHandler;
import com.app.library.UserFunctions;



public class LoginActivity extends Activity {
    Button btnLogin;
    Button btnLinkToRegister;
    EditText inputUsername;
    EditText inputPassword;
    TextView loginErrorMsg;


    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    private static String KEY_USER_ID = "userid";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_CREATED_AT = "created_at";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        // Importing all assets like buttons, text fields
        inputUsername = (EditText) findViewById(R.id.loginUsername);
        inputPassword = (EditText) findViewById(R.id.loginPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        loginErrorMsg = (TextView) findViewById(R.id.login_error);

     // Login button Click Event
        btnLogin.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                readLogin(view);
            }
        });

    }

    private class ProcessLogin extends AsyncTask<String, String, JSONObject> {

        private ProgressDialog dialog;
        private ProgressDialog pDialog;
        protected Context applicationContext;
String email,password;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            inputUsername = (EditText) findViewById(R.id.loginUsername);
            inputPassword = (EditText) findViewById(R.id.loginPassword);
             email = inputUsername.getText().toString();
             password = inputPassword.getText().toString();
            pDialog = new ProgressDialog(LoginActivity.this);
            pDialog.setMessage("Loading User ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {


            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.loginUser(email, password);

            Log.d("Button", "Login");


            return json;

        }

        @Override
        protected void onPostExecute(JSONObject json) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();
            UserFunctions userFunction = new UserFunctions();

            try {
                if(!json.has(KEY_SUCCESS)){

                Toast.makeText(getApplicationContext(), "No more data",
                        Toast.LENGTH_LONG).show();   
         }


            else if (json.getString(KEY_SUCCESS) != null){
                    loginErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_USER_ID), json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DatabaseSample.class);

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

                        // Close Login Screen
                        finish();
                    }else{
                        // Error in login
                        loginErrorMsg.setText("Username atau Password salah!");
                    }
                }
                           else {
                               loginErrorMsg.setText("koneksi gagal ke server");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
      }

    public void readLogin(View view) {
        new ProcessLogin().execute();

    // check for login response

  }
}

before, onPostExecute is force close cause the json result is null, how to check json is null in onPostExecute before execute if (json.getString(KEY_SUCCESS) != null) ?so the app not force close when the result is null?

this is my logcat

01-23 01:05:29.851: E/AndroidRuntime(4513): FATAL EXCEPTION: main
01-23 01:05:29.851: E/AndroidRuntime(4513): java.lang.NullPointerException
01-23 01:05:29.851: E/AndroidRuntime(4513):     at com.app.DatabaseSample.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:108)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at com.app.DatabaseSample.LoginActivity$ProcessLogin.onPostExecute(LoginActivity.java:1)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at android.os.AsyncTask.finish(AsyncTask.java:417)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at android.os.Looper.loop(Looper.java:130)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at java.lang.reflect.Method.invoke(Method.java:507)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-23 01:05:29.851: E/AndroidRuntime(4513):     at dalvik.system.NativeStart.main(Native Method)
  • 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-17T16:09:32+00:00Added an answer on June 17, 2026 at 4:09 pm
    if (json != null && json.getString(KEY_SUCCESS) != null){
        // PARSE RESULT 
    }else{
        // SHOW NOTIFICIATION: URL/SERVER NOT REACHABLE
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code: package com.elfapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log;
package com.login.android; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import com.login.android.R; import android.app.Activity;
Hy!! I have a problem with the progressdialog. My used code: package com.android.skiptvad; import
I have a login page (an activity in Android), during which I run an
In my app have a sign in activity. If login is successful, I start
so i have an application which has a default package as com.android . within
I have this class: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have this code: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
In my activity I have the 15% of the screen for login, it's a
I have login- and game-layout in my activity. I would use AndroidAnnotations in my

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.