Problem with Login SQLITE Android Studio

0

I have been with this problem a few hours ago I do not understand in which failure I will attach the class MainActivity and the class Helper corresponding:

package com.example.note.sv2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btn_login,btn_registrar;
    EditText t_user,t_pass;
    BaseHelper db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_login = (Button)findViewById(R.id.btn_login);
        btn_registrar=(Button)findViewById(R.id.btn_registrar);
        t_user = (EditText)findViewById(R.id.t_usuario);
        t_pass = (EditText)findViewById(R.id.t_pass);

        btn_registrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,Registrar.class));
            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String user = t_user.getText().toString();
                String password=t_pass.getText().toString();
                boolean chkuserpassword = db.userpassword(user,password);

                if (chkuserpassword==true){
                    Toast.makeText(getApplicationContext(),"login success",Toast.LENGTH_SHORT).show();
                }else
                {
                    Toast.makeText(getApplicationContext(),"THE USER OR PASSWORD ARE WRONG",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}

Helper Class:

public boolean userpassword(String user, String password){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor;
cursor = db.rawQuery("select * from usuarios where usuario=? and password=?",new String  []{user,password});

if (cursor.getCount()>=0) return true;
else return false;
}

the error is as follows:

  

FATAL EXCEPTION: main                     Process: com.example.note.sv2, PID: 10397                     java.lang.NullPointerException: Attempt to invoke virtual method 'boolean   com.example.note.sv2.BaseHelper.userpassword (java.lang.String,   java.lang.String) 'on a null object reference

    
asked by Anomalo 12.11.2018 в 22:57
source

1 answer

0

Inside the class MainActivity you declare the variable, but you never initialize the variable:

BaseHelper db;
...
...
boolean chkuserpassword = db.userpassword(user,password);

You must start the variable of class BaseHelper :

BaseHelper db;
...
...
db = new BaseHelper(...);
boolean chkuserpassword = db.userpassword(user,password);
    
answered by 13.11.2018 в 16:27