I have the following code for a login, but when executing the application, it is dropped by the OnClickListener method (server, and users of the bdd omitted for obvious reasons)
package com.ciclomapp.ciclomapp.ciclomapp;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Login extends AppCompatActivity {
Button login;
EditText username, password;
Connection con;
String un, pass, db, sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_Login);
login = (Button) findViewById(R.id.button);
username = (EditText) findViewById(R.id.editText2);
password = (EditText) findViewById(R.id.editText4);
sv = "";
db = "";
un = "";
pass = "";
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckLogin checkLogin = new CheckLogin();
checkLogin.execute("");
}
});
}
public class CheckLogin extends AsyncTask <String, String, String> {
String z = "";
Boolean isSuccess = false;
String usernam = username.getText().toString();
String passwordd = password.getText().toString();
@Override
protected String doInBackground(String... params) {
if (usernam.trim().equals("") || passwordd.trim().equals(""))
z = "Please enter Username and Password";
else {
try {
con = connectionclass(sv, db, un, pass);
if (con == null)
{
z = "Check Your Internet Access!";
}
else
{
String query = "select * from login where email= '" + usernam.toString() + "' and contraseña = '"+ passwordd.toString() +"' ";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next())
{
z = "Login successful";
isSuccess=true;
con.close();
Toast.makeText(Login.this, "correcto", Toast.LENGTH_SHORT).show();
}
else
{
z = "Invalid Credentials!";
isSuccess = false;
}
}
}
catch (Exception ex)
{
isSuccess = false;
z = ex.getMessage();
}
}
return z;
}
}
@SuppressLint("NewApi")
public Connection connectionclass(String user, String password, String database, String server)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String ConnectionURL = null;
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ConnectionURL = "jdbc:jtds:sqlserver://" + server + database + ";user=" + user+ ";password=" + password + ";";
connection = DriverManager.getConnection(ConnectionURL);
}
catch (SQLException se)
{
Log.e("error here 1 : ", se.getMessage());
}
catch (ClassNotFoundException e)
{
Log.e("error here 2 : ", e.getMessage());
}
catch (Exception e)
{
Log.e("error here 3 : ", e.getMessage());
}
return connection;
}
}
here I leave the layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ciclomapp.ciclomapp.ciclomapp.Login">
<TextView
android:text="E-Mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="230dp"
android:id="@+id/textView11"
android:textSize="25sp" />
<TextView
android:text="No tienes cuenta? Registrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:id="@+id/textView9"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textSize="20sp" />
<TextView
android:text="Contraseña"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:id="@+id/textView2"
android:textSize="25sp"
android:layout_below="@+id/textView11"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/editText2"
style="@android:style/Widget.AutoCompleteTextView"
android:inputType="textEmailAddress"
android:background="@android:color/darker_gray"
android:layout_alignBottom="@+id/textView11"
android:layout_alignTop="@+id/textView11"
android:layout_alignParentEnd="true"
android:layout_alignStart="@+id/editText4"
android:textColor="@android:color/black"
tools:ignore="LabelFor" />
<Button
android:text="Iniciar sesión"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginBottom="13dp"
android:layout_above="@+id/textView9"
android:layout_centerHorizontal="true"/>
<EditText
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText4"
android:background="@android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textColor="@android:color/black"
android:layout_alignBottom="@+id/textView2"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/logo"
android:id="@+id/imageView2"
android:layout_above="@+id/editText2"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and the logcat
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ciclomapp.ciclomapp.ciclomapp, PID: 10120
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ciclomapp.ciclomapp.ciclomapp/com.ciclomapp.ciclomapp.ciclomapp.Login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.ciclomapp.ciclomapp.ciclomapp.Login.onCreate(Login.java:51)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
pd: Sorry to publish so much code