2016-07-14 159 views
-6

這是錯誤:爲什麼我在這段代碼中得到一個NullPointerException

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); 


    } 
} 
+4

您的堆棧跟蹤任何地方都不會顯示一個NullPointerException。它顯示了一個SQLiteException。 –

回答

1

有你的代碼中有幾個問題。他們中很少有人如下:

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

你缺少常量字符串和變量之間的空間: 表「+ table_name的應該表」 + table_name的

而且,同樣的問題是在這條線在你的數據庫類:

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

應該是存在「+ TABLE_NAME

public static final String comma =「email」;還有另一個問題。應該是真的逗號,所以String comma =「,」;

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

+0

'由於:android.database.sqlite.SQLiteException:near「tableuser」:語法錯誤(代碼1):,編譯時:create tableuser(abctextemailemailtextemail12345text)'...並且也適用於列定義。 – Fildor

+0

我不認爲你已經做出了正確的改變。你能在這裏粘貼更改嗎?公共靜態最終字符串comma =「email」還有另一個問題;應該是真的逗號,所以String comma =「,」; – holmicz

+0

ErrorReport:發送HttpRequestorg.json.JSONException失敗:值java.lang.String類型的<!DOCTYPE無法在org.json.JSON.typeMismatch(JSON.java:111)上的org.json.JSONObject上轉換爲JSONObject。 (JSONObject.java:159)at org.json.JSONObject。 Tarun

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; 
+0

******************這個問題occour現在********************** ********** studentzone.studentzone E/AndroidRuntime:致命例外:main 進程:studentzone.studentzone,PID:31740 java.lang.IllegalStateException:無法在父級中找到方法reg(View)或祖先上下文的android:onClick屬性定義在視圖類android.support.v7.widget.AppCompatButton id爲'button_register' – Tarun

+0

先生請幫助我。 我必須在明天之前提交作業 – Tarun

+0

由於問題出在onClick事件上,因此我已經對我的申請 – Tarun

相關問題