0

I want to make login button by sql. When I run the app, I have an onclick error:

Process: com.example.mosab.hostlent, PID: 31057
 java.lang.IllegalStateException: Could not execute method for android:onClick
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
     at android.view.View.performClick(View.java:5609)
     at android.view.View$PerformClick.run(View.java:22263)
     at android.os.Handler.handleCallback(Handler.java:751)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6077)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
  Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Method.invoke(Native Method)
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
     at android.view.View.performClick(View.java:5609) 
     at android.view.View$PerformClick.run(View.java:22263) 
     at android.os.Handler.handleCallback(Handler.java:751) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6077) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
     at com.example.mosab.hostlent.login.loginon(login.java:92)
     at java.lang.reflect.Method.invoke(Native Method) 
     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
     at android.view.View.performClick(View.java:5609) 
     at android.view.View$PerformClick.run(View.java:22263) 
     at android.os.Handler.handleCallback(Handler.java:751) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:154) 
     at android.app.ActivityThread.main(ActivityThread.java:6077) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

This is my onclick code:

 public void loginon(View v) {
    String Email = email.getText().toString().trim();
    String Password = password.getText().toString().trim();
    String type = "login";
    worker worker = new worker(this);
    worker.execute(type,Email,Password);
}

This is my xml file:

<ImageButton
            android:id="@+id/login"
            android:layout_width="205dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:background="@color/hostlent"
            android:baselineAlignBottom="false"
            android:contextClickable="false"
            android:cropToPadding="false"
            android:keepScreenOn="false"
            android:nestedScrollingEnabled="false"
            android:onClick="loginon"
            android:saveEnabled="false"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/login"
            tools:layout_editor_absoluteX="86dp"
            tools:layout_editor_absoluteY="332dp" />

My class its called by onclick code:

public class worker extends AsyncTask<String,Void,String> {
Context context ;
AlertDialog alertDialog;
worker (Context context){
    Context ctx;
}
@Override
protected String doInBackground(String... params) {
    String type = params[0];
    String login_url = "http://192.168.1.239:8080/login.php";
    if (type.equals("login")){
        try {
            String email = params[1];
            String password = params[2];
            URL url = new URL(login_url);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();
            httpsURLConnection.setRequestMethod("post");
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setDoInput(true);
            OutputStream outputStream = httpsURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String post_data = URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+URLEncoder.encode(password,"UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpsURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream,"iso-8859-1"));
            String result="";
            String line="";
            while ((line=bufferedReader.readLine())!=null){
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpsURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}

@Override
protected void onPreExecute() {
    alertDialog=new AlertDialog.Builder(context).create();
    alertDialog.setTitle("login status");
}

@Override
protected void onPostExecute(String result) {
    alertDialog.setMessage(result);
    alertDialog.show();
}

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

and I am adding this code in the manifest file:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>"  

Can you help me to understand which is my error?

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
Mosab Waleed
  • 21
  • 1
  • 2
  • 1
    Changed question to improve quality – Kalamarico Oct 05 '17 at 15:59
  • Didn't get exactly what you wanna say, first `checking your internet connection` and then run the app. – meyasir Jun 25 '18 at 05:57
  • 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) – Selvin Mar 13 '19 at 12:52
  • Did you change recently the name of any of your classes/activities? That cause this error to happen on my AS 4.1, where the name wasn't changed in the manifest file. I had to change the old name manually for fixing this problem. – carloswm85 Oct 17 '20 at 18:48

1 Answers1

0

The error is in either one of the following 2 lines,

String Email = email.getText().toString().trim();
String Password = password.getText().toString().trim();

Either the variable email or password is null. You need to set these variables to appropriate edittext fields in your onCreate() method.

If you are still unable to solve the issue, update the question with full code to your activity and the layout file xml.

Jayson Chacko
  • 2,388
  • 1
  • 11
  • 16