1

The code below is for the login authentication of the app I'm working on:

public class Login extends AppCompatActivity implements View.OnClickListener{
private EditText inputemail;
private EditText inputpassword;
private Button btnlogin;
private static final String LOGIN_URL = "http://wwww.example.com/foodify/login.php";

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

    inputemail = (EditText)findViewById(R.id.input_email);
    inputpassword = (EditText)findViewById(R.id.input_password);
    btnlogin = (Button)findViewById(R.id.btn_login);
    btnlogin.setOnClickListener(this);

    Button aboutapp = (Button)findViewById(R.id.learnmore);
    aboutapp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent learnmore = new Intent(Login.this, AboutApp.class);
            startActivity(learnmore);
        }
    });

   Button signup = (Button)findViewById(R.id.signupnow);
   signup.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Intent signup = new Intent(Login.this, SignUp.class);
           startActivity(signup);
       }
   });

   Button ccode = (Button)findViewById(R.id.ccodebtn);
   ccode.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           Intent ccode = new Intent(Login.this, Confirmation_Sign_Up.class);
           startActivity(ccode);
       }
   });

   btnlogin.setOnClickListener(this);

}


@Override
public void onClick(View v){
   if(v == btnlogin){
       LoginUser();
   }
}

private void LoginUser() {
    String email = inputemail.getText().toString();
    String password = inputpassword.getText().toString();
    login(email,password);
}

private void login(String email, String password) {
    String urlSuffix = "?email="+email+"&password="+password;
   class LoginUser extends AsyncTask<String, Void, String> {
       ProgressDialog loading;

       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           loading = ProgressDialog.show(Login.this, "Please Wait","Authenticating...", true, true);
       }

       @Override
       protected void onPostExecute(String s) {
           super.onPostExecute(s);
           loading.dismiss();
           Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
           if (s.matches("Login successful")) {
               Intent regcom = new Intent();
               regcom.setClass(getApplicationContext(), main_menu.class);
               startActivity(regcom);
           }
       }
       @Override
       protected String doInBackground(String... params) {
           String s = params[0];
           BufferedReader bufferedReader = null;
           try {
               URL url = new URL(LOGIN_URL+s);
               HttpURLConnection con = (HttpURLConnection) url.openConnection();
               bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
               String result;
               result = bufferedReader.readLine();
               return result;
           }catch(Exception e){
               return null;
           }
       }
   }
   LoginUser ru = new LoginUser();
   ru.execute(urlSuffix);
  }
}

On pressing the login button, the authenticating ProgressDialog shows but the app crashes at the post_execute stage, according to the logcat The logcat entry is shown below:

FATAL EXCEPTION: main
Process: mediatech3.foodifyplus, PID: 19099
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean 
java.lang.String.matches(java.lang.String)' on a null object reference
at mediatech3.foodifyplus.Login$1LoginUser.onPostExecute(Login.java:95)
at mediatech3.foodifyplus.Login$1LoginUser.onPostExecute(Login.java:81)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

I have checked the login.php file for errors and found none. I don't really understand what the error refers to and am not sure how to proceed. Any advice would be greatly appreciated. :)

sshashank124
  • 31,495
  • 9
  • 67
  • 76
J W
  • 29
  • 2
  • java.lang.NullPointerException: Attempt to invoke virtual method 'boolean You have a NullPointerException. This should be one of the first things in error handling you learn about – Stultuske Feb 20 '18 at 08:30
  • 5
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Feb 20 '18 at 08:30
  • 1
    doInBackground is returning null in some cases which is then attempted to call `.matches` upon in `postExecute`. Add if null checks on s in `postExecute` – sshashank124 Feb 20 '18 at 08:32
  • error is inside the try statement. make sure it is working. – user1506104 Feb 20 '18 at 09:43

1 Answers1

1

Change the return statement of doInBackground as mentioned below.

       @Override
       protected String doInBackground(String... params) {
           String s = params[0];
           BufferedReader bufferedReader = null;
           try {
               URL url = new URL(LOGIN_URL+s);
               HttpURLConnection con = (HttpURLConnection) url.openConnection();
               bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
               String result;
               result = bufferedReader.readLine();
               return result;
           }catch(Exception e){
//             return null;
//             change this to 
               e.printStackTrace();
               return "Error in login";
           }
       }

Printing the exception will be a good idea to check whether it is failing somewhere else or not.

arnab.p
  • 43
  • 7