java.lang.NullPointerException:尝试调用虚拟方法'void android.database.sqlite.SQLiteDatabase.execSQL

时间:2019-01-24 19:13:44

标签: java android sqlite

我正在尝试在android中注册和登录数据库。但是我一直收到此错误,我一直在检查任何内容,但仍然找不到代码中的错误。当我单击“注册”时,总是会出现此错误。我试图将数据从注册到我的数据库

    01-24 18:54:58.616 12606-12606/com.example.ranjiv.pigken E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ranjiv.pigken, PID: 12606
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4698)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    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:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4693)
    at android.view.View.performClick(View.java:5610) 
    at android.view.View$PerformClick.run(View.java:22265) 
    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:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
    at com.example.ranjiv.pigken.DatabaseHelper.onCreate(DatabaseHelper.java:36)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at com.example.ranjiv.pigken.DatabaseHelper.insertContact(DatabaseHelper.java:44)
    at com.example.ranjiv.pigken.signup.onSignUpClick(signup.java:48)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.view.View$DeclaredOnClickListener.onClick(View.java:4693) 
    at android.view.View.performClick(View.java:5610) 
    at android.view.View$PerformClick.run(View.java:22265) 
    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:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

这是我的数据库帮助程序代码:

package com.example.ranjiv.pigken;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contacts.db";
public static final String TABLE_NAME = "contacts";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_UNAME = "uname";
public static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
DatabaseHelper myDb;
private static final String TABLE_CREATE = "CREATE TABLE contacts (id integer primary key AUTOINCREMENT not null , " +
       "name text not null , email text not null , uname text not null , pass text not null) ; " ;

public DatabaseHelper(Context context)
{
    super(context , DATABASE_NAME , null , DATABASE_VERSION );

}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
    db.execSQL(TABLE_CREATE);
    this.db = db;
}

public void insertContact(Contact c)
{
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        // String query = "select * from contacts";
        // Cursor cursor = db.rawQuery(query, null);
        //int count = cursor.getCount();

        //values.put(COLUMN_ID , count);
        values.put(COLUMN_NAME, "goons");
        values.put(COLUMN_EMAIL,  "goons");
        values.put(COLUMN_UNAME,  "goons");
        values.put(COLUMN_PASS,  "goons");

        db.insert(TABLE_NAME, null, values);
        db.close();
}

public String searchPass(String uname)
{
    db = this.getReadableDatabase();
    String query = "select uname, pass from "+TABLE_NAME;
    Cursor cursor = db.rawQuery(query , null);
    String a, b;
    b = "not found";
    if(cursor.moveToFirst())
    {
        do{
            a = cursor.getString(0);

            if(a.equals(uname))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while(cursor.moveToNext());
    }
    return b;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
    db.execSQL(query);
    this.onCreate(db);

}

}

这是我的注册代码:

    package com.example.ranjiv.pigken;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class signup extends Activity {
        DatabaseHelper helper = new DatabaseHelper(this);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.signup);
        }
        public void onSignUpClick(View v)
        {
            if(v.getId()==R.id.signupBtn2)
            {
        EditText name = (EditText)findViewById(R.id.TFName);
        EditText email = (EditText)findViewById(R.id.TFEmail);
        EditText uname = (EditText)findViewById(R.id.TFuname);
        EditText pass1 = (EditText)findViewById(R.id.TFpassword);
        EditText pass2 = (EditText) findViewById(R.id.TFconfirm);

        String namestr = name.getText().toString();
        String emailstr = email.getText().toString();
        String unamestr = uname.getText().toString();
        String pass1str = pass1.getText().toString();
        String pass2str = pass2.getText().toString();

        if(!pass1str.equals(pass2str))
        {
            //popup msg
            Toast pass = Toast.makeText(signup.this, "Passwords don't match!", Toast.LENGTH_SHORT);
            pass.show();
        }
        else
        {
            //insert values
            Contact c = new Contact();
            c.setName(namestr);
            c.setEmail(emailstr);
            c.setUname(unamestr);
            c.setPass(pass1str);

           helper.insertContact(c);
        }
    }
}}

0 个答案:

没有答案