为什么我在这段代码中得到NullPointerException

时间:2016-07-14 10:57:32

标签: java android-studio

这是错误:

07-14 16:13:53.385 19921-19921/studentzone.studentzone E/AndroidRuntime: FATAL EXCEPTION: main
Process: studentzone.studentzone, PID: 19921
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:4444)
   at android.view.View$PerformClick.run(View.java:18457)
   at android.os.Handler.handleCallback(Handler.java:733)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5113)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   at android.view.View.performClick(View.java:4444) 
   at android.view.View$PerformClick.run(View.java:18457) 
   at android.os.Handler.handleCallback(Handler.java:733) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:136) 
   at android.app.ActivityThread.main(ActivityThread.java:5113) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:515) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
   at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.database.sqlite.SQLiteException: near "tableuser": syntax error (code 1): , while compiling: create tableuser(abctextemailemailtextemail12345text)
   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
   at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
   at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
   at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
   at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
   at studentzone.studentzone.DataBaseHelper.onCreate(DataBaseHelper.java:30)
   at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
   at studentzone.studentzone.DataBaseHelper.insertData(DataBaseHelper.java:43)
   at studentzone.studentzone.SignUp.register(SignUp.java:31)
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:515) 
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
   at android.view.View.performClick(View.java:4444) 
   at android.view.View$PerformClick.run(View.java:18457) 
   at android.os.Handler.handleCallback(Handler.java:733) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:136) 
   at android.app.ActivityThread.main(ActivityThread.java:5113) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:515) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) 
   at dalvik.system.NativeStart.main(Native Method) 

我的数据库类是:

public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String Database_name="studentzone.db";
    public static final String Table_name ="user";
    public static final String Type ="text";
    public static final String comma ="email";
    public static final String Name ="abc";
    public static final String Email ="email";
    public static final String Pass ="12345";
    public static final String sql_create="create table"+Table_name+"("+Name+Type+comma+Email+Type+comma+Pass+Type+")";
    public static final String sql_delete="drop table if exists" + DataBaseHelper.Table_name;

    public DataBaseHelper(Context context) {
        super(context,Database_name, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(sql_create);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL(sql_delete);
        onCreate(db);
    }

    public Boolean insertData(String name , String email , String password)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Name,name);
        contentValues.put(Email,email);
        contentValues.put(Pass,password);
        long rslt =db.insert(Table_name,null,contentValues);
        if(rslt==-1)
        {
            return false;

        }
            else
        {
            return true;
        }

    }
}

我的主要课程是:

public class SignUp extends AppCompatActivity {

DataBaseHelper Database;

    Button login;

    EditText editName, editEmail,editPassword;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sign_up);

        login= (Button) findViewById(R.id.button_login);

        editName= (EditText) findViewById(R.id.edit_text_full_name);

        editEmail= (EditText) findViewById(R.id.edit_text_email);

        editPassword= (EditText) findViewById(R.id.edit_text_password);

        Database=new DataBaseHelper(this);

    }
    public void register(View v)
    {
        Toast.makeText(SignUp.this,editName.getText().toString(), Toast.LENGTH_SHORT).show();
        Toast.makeText(SignUp.this,editEmail.getText().toString(), Toast.LENGTH_SHORT).show();
        Toast.makeText(SignUp.this,editPassword.getText().toString(), Toast.LENGTH_SHORT).show();
        boolean isInserted=Database.insertData(editName.getText().toString(),editEmail.getText().toString(),editPassword.getText().toString());
       if(isInserted==true)
        {

            Toast.makeText(getApplicationContext(),"value inserted",Toast.LENGTH_SHORT).show();
        }
        else
        {

            Toast.makeText(getApplicationContext(),"no value inserted",Toast.LENGTH_SHORT).show();
        }

    }

    public void login(View v)

    {
        Intent intent = new Intent(SignUp.this,LoginActivity.class);
        startActivity(intent);


    }
}

2 个答案:

答案 0 :(得分:1)

您的代码中存在几个问题。其中很少有人遵循:

public static final String sql_create="create table"+Table_name+"("+Name+Type+comma+Email+Type+comma+Pass+Type+")";

您在常量字符串和变量之间缺少空格: 表&#34; + Table_name 应为表&#34; + Table_name

此外,数据库类中的这一行也存在同样的问题:

public static final String sql_delete="drop table if exists" + DataBaseHelper.Table_name;

存在&#34; + Table_name

public static final String comma =&#34; email&#34 ;;应该是逗号,因此字符串逗号=&#34;,&#34 ;;

public static final String Table_name ="user";
public static final String Type ="text";
public static final String comma ="email";
public static final String Name ="abc";
public static final String Email ="email";
public static final String Pass ="12345";

这对于create语句是错误的,因为这显然包含CREATE TABLE语句的值而不是列名。我认为你的意图是在这些变量中有列名。所以看起来应该是这样的:

public static final String Table_name ="user";
public static final String Type ="type";
public static final String comma =",";
public static final String Name ="name";
public static final String Email ="email";
public static final String Pass ="password";

最后一点是,CREATE TABLE语句缺少数据类型。 See CREATE TABLE statement here

答案 1 :(得分:0)

将以下两行更改为

public static final String sql_create="create table "+Table_name+"("+Name+" "+Type+","+Email+" "+Type+","+Pass+" "+Type+")";


public static final String sql_delete="drop table if exists " + DataBaseHelper.Table_name;