0

When user login i will get user id and i will get device id and i will check in database(server end) and if the response is SUCCESS the ui need to update. But i am getting null pointer exception. I am updating ui in thread. Whats is the issues in this.

public class MainActivity extends AppCompatActivity {

    final String LOG_TAG="TESTOPENAPP";
    OpenAppLock mSelectedLock;
    String email,devid;
    ArrayList<OpenAppLock> scanList;
    HttpPost httppost;
    HttpResponse response;
    HttpClient httpclient;
    AlertDialogManager alert = new AlertDialogManager();
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

    Button mScan,mConnect,mUnlock,mDisconnect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent i = getIntent();
        // Receiving the Data
//        String name = i.getStringExtra("name");
        email = i.getStringExtra("email");
        System.out.println("user mail id" + email);

        mScan=(Button)findViewById(R.id.scan);
        mConnect=(Button)findViewById(R.id.connect);
        mUnlock=(Button)findViewById(R.id.unlock);
        mDisconnect=(Button)findViewById(R.id.disconnect);

        OpenApp.initialize(this);

        mScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mScan.setText("Scanning ...");
                OpenApp.scanForLocks(new ScanFinishedCallback() {
                    @Override
                    public void onScanFinished(ArrayList<OpenAppLock> scanList) {
                        Log.d(LOG_TAG, "OnScanFinishedCallback");
                        for (int i = 0; i < scanList.size(); i++) {
                            Log.d(LOG_TAG, "Available Lock " + i + " - " + scanList.get(i).getLockName());
                            devid = scanList.get(i).getLockName();
                        }
                        dialog = ProgressDialog.show(MainActivity.this, "",
                                "Validating user...", true);
                        new Thread(new Runnable() {
                            public void run() {
                                login();
                            }
                        }).start();

                    }
                });
            }
        });

        mConnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mConnect.setText("Connecting ...");
                mSelectedLock.connect(new ConnectionCallback() {
                    @Override
                    public void onFinishedTrying(boolean success) {
                        if(success){
                            mConnect.setVisibility(View.GONE);
                            mUnlock.setVisibility(View.VISIBLE);
                            //mDisconnect.setVisibility(View.VISIBLE);
                        }else{
                            mConnect.setText("Connect");
                            Toast.makeText(MainActivity.this, "Unable to connect to lock.", Toast.LENGTH_LONG).show();
                        }
                    }
                });

            }
        });

        mUnlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //It should be already ensured that this mSelectedLock is something user is authorized to access
                if(mSelectedLock.unlock("RANDOM")){

                    mUnlock.setVisibility(View.INVISIBLE);
                    mUnlock.postDelayed(new Runnable() {
                        public void run() {
                            mUnlock.setVisibility(View.VISIBLE);
                            mUnlock.performClick();

                            Toast.makeText(getApplicationContext(),"Button Clicked",Toast.LENGTH_SHORT).show();

                        }
                    }, 6000);
                }else{
                    Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    void login() {
        try {

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://192.168.25.107:8080/ActCFWeb/login"); // make sure the url is correct.
            //add your data
            nameValuePairs = new ArrayList<NameValuePair>(2);
            // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
            nameValuePairs.add(new BasicNameValuePair("email", email));  // $Edittext_value = $_POST['Edittext_value'];
            nameValuePairs.add(new BasicNameValuePair("pass", devid));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            //Execute HTTP Post Request
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);
            System.out.println("Response : " + response);
            runOnUiThread(new Runnable() {
                public void run() {

                    dialog.dismiss();
                    System.out.println("Response : " + response);
                }
            });

            if (response.contains("success")) {

                runOnUiThread(new Runnable() {
                    public void run() {
                        if (scanList.size() > 0) {
                            Toast.makeText(MainActivity.this, "Scanning finished. Lock Found.", Toast.LENGTH_LONG).show();
                            mScan.setVisibility(View.GONE);
                            mConnect.setVisibility(View.VISIBLE);
                            mSelectedLock = scanList.get(0);
                            dialog.dismiss();

                        } else {
                            mScan.setText("Scan");
                            Toast.makeText(MainActivity.this, "No Lock Found while scanning. Please scan again.", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }

            else {
                showAlert();
            }

        } catch (Exception e) {
            dialog.dismiss();
            System.out.println("Exception : " + e.getMessage());
        }
    }

    public void showAlert() {
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Login Error.");
                builder.setMessage("User not Found.")
                        .setCancelable(false)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });
    }

    @Override
    protected void onStop() {
        OpenApp.destroy();
        if(mSelectedLock!=null)
            mSelectedLock.destroy();
        super.onStop();
    }
benarjee bojja
  • 348
  • 1
  • 3
  • 15

2 Answers2

1

Hey I found your solution.

I check your code and found that when your response is null and you check about response.contains("success"), you will get NullPointerException.

Just add one If condition more.

Check code below.

if(response != null){
    if (response.contains("success")) {
        runOnUiThread(new Runnable() {
            public void run() {
                if (scanList.size() > 0)
                {
                    try
                    {
                        Toast.makeText(MainActivity.this, "Scanning finished. Lock Found.", Toast.LENGTH_LONG).show();
                        mScan.setVisibility(View.GONE);
                        mConnect.setVisibility(View.VISIBLE);
                        mSelectedLock = scanList.get(0);
                    }
                    catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                } else {
                    mScan.setText("Scan");
                    Toast.makeText(MainActivity.this, "No Lock Found while scanning. Please scan again.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    else {
        showAlert();
    }
}

I just add if(response != null) for check response is null or not.

Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
0
if (response.contains("success")) {

                runOnUiThread(new Runnable() {
                    public void run() {

                        if (scanList.size() > 0)
                        {
                            try
                            {
                                Toast.makeText(MainActivity.this, "Scanning finished. Lock Found.", Toast.LENGTH_LONG).show();
                                mScan.setVisibility(View.GONE);
                                mConnect.setVisibility(View.VISIBLE);
                                mSelectedLock = scanList.get(0);
                            }
                            catch (NullPointerException e) {
                                e.printStackTrace();
                            }

                        } else {
                            mScan.setText("Scan");
                            Toast.makeText(MainActivity.this, "No Lock Found while scanning. Please scan again.", Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }

            else {
                showAlert();
            }
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
benarjee bojja
  • 348
  • 1
  • 3
  • 15