2012-06-14 98 views
0

我正在編寫Android,sql atm,我需要你的幫助。無法啓動應用程序,因爲我得到了一個空指針,我無法找到它...Android,SQL,Nullpointerexception

所以我試圖閱讀很多,但我不認爲我明白這裏的一切。所以,如果你可以告訴我什麼是錯的,那將是非常棒的。

你能幫我嗎?

感謝大家閱讀! :)

>public class DBAdapter 
>{ 
> //Variables and so on.. 
> public static final String KEY_ROWID = "_id"; 
> public static final String KEY_ISBN = "isbn"; 
> public static final String KEY_TITLE = "title"; 
> public static final String KEY_PUBLISHER ="publisher"; 
> private static final String TAG ="DBAdapter"; 
> private static final String DATABASE_NAME = "books"; 
> private static final String DATABASE_TABLE = "titles"; 
> private static final int DATABASE_VERSION = 1; 
> private static final String DATABASE_CREATE = "create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null);"; 
> 
> private final Context context; 
> 
> private DatabaseHelper DBHelper; 
> private SQLiteDatabase db; 
> 
> public DBAdapter(Context ctx) 
> { 
>  this.context = ctx; 
>  DBHelper = new DatabaseHelper(context); 
> } 
> 
> private static class DatabaseHelper extends SQLiteOpenHelper 
> { 
>  DatabaseHelper(Context context) 
>  { 
>   super(context, DATABASE_NAME, null, DATABASE_VERSION); 
>  } 
>  @Override 
>  public void onCreate(SQLiteDatabase db) 
>  { 
>   db.execSQL(DATABASE_CREATE); 
>  } 
>  @Override 
>  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
>  { 
>   Log.w(TAG, "Upgrading database from version "+oldVersion+" to >"+newVersion+" which will destory all old data"); 
>   db.execSQL("DROP TABLE IF EXISTS titles"); 
>   onCreate(db); 
>  } 
> } 
> 
> //opens database 
> public DBAdapter open() throws SQLException 
> { 
>  db = DBHelper.getWritableDatabase(); 
>  return this; 
> } 
> 
> //closes database 
> public void close() 
> { 
>  DBHelper.close(); 
> } 
> 
> //insert title to database 
> ContentValues initialValues = new ContentValues(); 
> public long insertTitle(String isbn, String title, String publisher) 
> { 
>  initialValues.put(KEY_ISBN, isbn); 
>  initialValues.put(KEY_TITLE, title); 
>  initialValues.put(KEY_PUBLISHER, publisher); 
>  return db.insert(DATABASE_TABLE, null, initialValues); 
> } 
> 
> //Delete specific title 
> public boolean deleteTitle(long rowId) 
> { 
>  return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
> } 
> 
> //Retrieves all titles 
> public Cursor getAllTitles() 
> { 
>  return db.query(DATABASE_TABLE, new String[] 
>  {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, null, null, null, null, >null); 
> } 
> 
> //Retrieves a particular title 
> public Cursor getTitle(Long rowId) throws SQLException 
> { 
>  Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] 
>    {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, KEY_ROWID >+ "=" + rowId, null, null, null, null, null); 
>  if(mCursor != null) 
>  { 
>   mCursor.moveToFirst(); 
>  } 
>  return mCursor; 
> } 
> 
> //Update a title 
> ContentValues args = new ContentValues(); 
> public boolean updateTitle(long rowId, String title, String isbn, String publisher) 
> { 
>  args.put(KEY_ISBN, isbn); 
>  args.put(KEY_TITLE, title); 
>  args.put(KEY_PUBLISHER, publisher); 
>  return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
> } 
>} 

編輯:

這裏是我的代碼是我使用將對DBAdapter類:

public class DatabasActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     DBAdapter db = new DBAdapter(this); 

     //Add 2 titles 
     db.open(); 
     long id; 
     id = db.insertTitle("0470285818", "C# 2008 Programmer's Reference", "Wrox"); 
     id = db.insertTitle("047017661X", "Professional Windows Vista Gadgets Programming", "Wrox"); 
     db.close(); 

     //Get all titles 
     db.open(); 
     Cursor c = db.getAllTitles(); 
     if(c.moveToFirst()) 
     { 
      do 
      { 
       DisplayTitle(c); 
      } 
      while(c.moveToNext()); 
      { 

      } 
     } 
     db.close(); 

     //get a title 
     db.open(); 
     Cursor c1 = db.getTitle((long) 2); 
     if(c1.moveToFirst()) 
     { 
      DisplayTitle(c1); 
     } 
     else 
     { 
      Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show(); 
     } 
     db.close(); 

     //Update a title 
     db.open(); 
     if(db.updateTitle(1, "0470285818", "C# 2008 Programmer's Reference", "Wrox Press")) 
     { 
      Toast.makeText(this, "Update successful", Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      Toast.makeText(this, "Update failed", Toast.LENGTH_LONG).show(); 
     } 
     db.close(); 
     //Retrive the updated title 
     Cursor c2 = db.getTitle((long) 1); 
     if(c.moveToFirst()) 
     { 
      DisplayTitle(c2); 
     } 
     else 
     { 
      Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void DisplayTitle(Cursor c) 
    { 
     Toast.makeText(this, "id: "+c.getString(0)+"\n"+"ISBN: "+c.getString(1)+"\n"+"Title: "+c.getString(2)+"\n"+"Publisher: "+c.getString(3), Toast.LENGTH_LONG).show(); 
    } 

} 

我設法從我的logcat得到一些輸出,那就是:

06-14 08:43:00.882: D/AndroidRuntime(426): Shutting down VM 
06-14 08:43:00.892: W/dalvikvm(426): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
06-14 08:43:00.912: E/AndroidRuntime(426): FATAL EXCEPTION: main 
06-14 08:43:00.912: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{databas.test.org/databas.test.org.DatabasActivity}: java.lang.NullPointerException 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:3683) 
06-14 08:43:00.912: E/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native Method) 
06-14 08:43:00.912: E/AndroidRuntime(426): at java.lang.reflect.Method.invoke(Method.java:507) 
06-14 08:43:00.912: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
06-14 08:43:00.912: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
06-14 08:43:00.912: E/AndroidRuntime(426): at dalvik.system.NativeStart.main(Native Method) 
06-14 08:43:00.912: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException 
06-14 08:43:00.912: E/AndroidRuntime(426): at databas.test.org.DBAdapter.insertTitle(DBAdapter.java:74) 
06-14 08:43:00.912: E/AndroidRuntime(426): at databas.test.org.DatabasActivity.onCreate(DatabasActivity.java:24) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
06-14 08:43:00.912: E/AndroidRuntime(426): ... 11 more 
+3

open()唯一潛在的NPE你能後的堆棧跟蹤嗎?否則NullPointer可能在任何地方。這將有助於我們縮小它到崩潰的路線。 – Austin

+0

粘貼你的logcat! –

+0

對不起,我的logcat不顯示任何東西,甚至在開始之前崩潰。 發生的唯一情況是Eclipse顯示一個彈出窗口,他說:「在發生內部錯誤期間: 」啓動數據塊「 java.lang.NullPointerException」 – user1455613

回答

0

我覺得在

create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null) 

「>」是無效的有........

+0

對不起:S當我發佈代碼時,那個人滑了進去。 – user1455613

+0

這給出了空指針異常? :s –

+0

發表代碼在哪裏使用這個DBAdapter類的形式 –

相關問題