2016-06-20 128 views
-3

我試圖使用SQLite數據庫來存儲以及其他信息,圖像在我的Android應用程序,但我得到這個錯誤sqlite的給NullPointerException異常錯誤的Android

06-20 12:00:51.411 4132-4132/braindottech.com.fishid E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: braindottech.com.fishid, PID: 4132 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference 
     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223) 
     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 
     at braindottech.com.fishid.PhotoDBMS.<init>(PhotoDBMS.java:65) 
     at braindottech.com.fishid.CameraScan$6.onClick(UseSqliteDB.java:23) 
     at android.view.View.performClick(View.java:5204) 
     at android.view.View$PerformClick.run(View.java:21153) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

這裏是我的「UseSqliteDB.java」的代碼:

public class UseSqliteDB extends Fragment { 

    private Button saveResult; 

    public UseSqliteDB(){} 

    @Override 
    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.fragment_camera, container, false); 

     context = this.getActivity(); 
     saveResult = (Button) v.findViewById(R.id.button_save_CameraPreview); 

     saveResult.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 

      try { 
       PhotoDBMS photoDBMS = new PhotoDBMS(getActivity()); 
       String nameImage = "IMAGE"; 
       Calendar calendar = Calendar.getInstance(); 
       nameImage += calendar.get(Calendar.DATE) + calendar.get(Calendar.HOUR) + calendar.get(Calendar.MINUTE) + 
         calendar.get(Calendar.SECOND) + calendar.get(Calendar.MILLISECOND); 
       Boolean response = photoDBMS.insertData(nameImage, resultTV.getText().toString(), finalBitmap); 
       if (!response) { 
        Toast.makeText(context, "Database entry failed!", Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(context, "Database entry successful!", Toast.LENGTH_LONG).show(); 
       } 

      }catch(Exception e){ 
       Log.d("HIP","E = "+e); 
      } 

     } 
     }); 

     return v; 
    } 

} 

PhotoDBMS.java

public class PhotoDBMS { 

    private Context context; 

    public PhotoDBMS(Context c){ 
     context = c; 
    } 

    public static abstract class DBMS_Constants implements BaseColumns { 
     public static final String TABLE_NAME = "MY_COLLECTION"; 
     public static final String COLUMN_NAME_NAME = "Name"; 
     public static final String COLUMN_NAME_INFO = "Info"; 
     public static final String COLUMN_NAME_IMAGE = "Image"; 
    } 

    private static final String TEXT_TYPE = " TEXT"; 
    private static final String COMMA_SEP = ","; 
    private static final String BLOB = "BLOB"; 
    private static final String SQL_CREATE_TABLE = "CREATE TABLE " + DBMS_Constants.TABLE_NAME + " (" + 
     DBMS_Constants._ID + " INTEGER PRIMARY KEY," + 
     DBMS_Constants.COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP + 
     DBMS_Constants.COLUMN_NAME_INFO + TEXT_TYPE + COMMA_SEP + 
     DBMS_Constants.COLUMN_NAME_IMAGE + BLOB + ")"; 

    private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + DBMS_Constants.TABLE_NAME; 

    public class DbHelper extends SQLiteOpenHelper { 
    // If you change the database schema, you must increment the database version. 
     public static final int DATABASE_VERSION = 1; 
     public static final String DATABASE_NAME = "FeedReader.db"; 

     public DbHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(SQL_CREATE_TABLE); 
     } 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
      db.execSQL(SQL_DELETE_ENTRIES); 
      onCreate(db); 
     } 

     public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      onUpgrade(db, oldVersion, newVersion); 
     } 

    } 

    DbHelper dbHelper = new DbHelper(context); 
    SQLiteDatabase collectionDB = dbHelper.getWritableDatabase(); 

    public boolean insertData(String name, String info, Bitmap bm){ 

     bitmapUtility utility = new bitmapUtility(); 
     byte[] image = utility.getBytes(bm); 
     ContentValues cv = new ContentValues(); 
     cv.put(DBMS_Constants.COLUMN_NAME_NAME, name); 
     cv.put(DBMS_Constants.COLUMN_NAME_INFO, info); 
     cv.put(DBMS_Constants.COLUMN_NAME_IMAGE, image); 
     return collectionDB.insert(DBMS_Constants.TABLE_NAME, null, cv) > 0; 

    } 

    public boolean deleteData(String name){ 
     return collectionDB.delete(DBMS_Constants.TABLE_NAME, DBMS_Constants.COLUMN_NAME_NAME + " = " + name, null) > 0; 
    } 

} 

我正在上的try-catch節這裏面的錯誤UseSqliteDB.java。請幫我解決這個錯誤。 Thankx提前!

+0

可以提供異常的完整堆棧跟蹤更換你PhotoDBMS類? – SripadRaj

+0

將你的'context = this.getActivity();'移動到'onActivityCreated'中,並傳遞如'PhotoDBMS photoDBMS = new PhotoDBMS(context);'的內容。如果不需要,從'PhotoDBMS'類中刪除'dbHelper.getWritableDatabase();'。 –

+0

@SripadRaj yes上傳了logcat – Algor7

回答

2

問題是由於:

DbHelper dbHelper = new DbHelper(context); 
SQLiteDatabase collectionDB = dbHelper.getWritableDatabase(); 

線加入在類級,而不是任何方法內。

無論創建方法,並利用photoDBMS創建和獲取數據庫對象調用它:

public void createDB(){ 
    DbHelper dbHelper = new DbHelper(context); 
    SQLiteDatabase collectionDB = dbHelper.getWritableDatabase(); 
} 

,並在按鈕的onClick方法調用它:

PhotoDBMS photoDBMS = new PhotoDBMS(getActivity()); 
photoDBMS.createDB(); 

或移動內部PhotoDBMS類的構造兩條線

+0

Thankx @prosper K(抱歉,我不能像你這樣寫你的名字)。它現在正在工作,但現在我得到'布爾迴應'等於'假'。我應該爲這個問題創建另一個帖子嗎? – Algor7

+0

@ Algor7:檢查'collectionDB.insert(DBMS_Constants.TABLE_NAME,null,cv)'方法返回並在可能的情況下創建新問題 –

+0

@prosper K:返回-1 – Algor7

1

試試這個,

通過

public class DbHelper extends SQLiteOpenHelper { 
    public static final String TABLE_NAME = "MY_COLLECTION"; 
    public static final String COLUMN_NAME_NAME = "Name"; 
    public static final String COLUMN_NAME_INFO = "Info"; 
    public static final String COLUMN_NAME_IMAGE = "Image"; 
    private static final String _ID = "id"; 

    private static final String TEXT_TYPE = " TEXT"; 
    private static final String COMMA_SEP = ","; 
    private static final String BLOB = "BLOB"; 


    private static final String SQL_CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + 
      _ID + " INTEGER PRIMARY KEY," + 
      COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP + 
      COLUMN_NAME_INFO + TEXT_TYPE + COMMA_SEP + 
      COLUMN_NAME_IMAGE + BLOB + ")"; 

    private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + TABLE_NAME; 

     // If you change the database schema, you must increment the database version. 
     public static final int DATABASE_VERSION = 1; 
     public static final String DATABASE_NAME = "FeedReader.db"; 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_TABLE); 
    } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
     db.execSQL(SQL_DELETE_ENTRIES); 
     onCreate(db); 
    } 

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 

    public boolean insertData(String name, String info, Bitmap bm){ 
     SQLiteDatabase sqLiteDatabase = getWritableDatabase(); 
     bitmapUtility utility = new bitmapUtility(); 
     byte[] image = utility.getBytes(bm); 
     ContentValues cv = new ContentValues(); 
     cv.put(COLUMN_NAME_NAME, name); 
     cv.put(COLUMN_NAME_INFO, info); 
     cv.put(COLUMN_NAME_IMAGE, image); 
     return sqLiteDatabase.insert(TABLE_NAME, null, cv) > 0; 

    } 

    public boolean deleteData(String name){ 
     SQLiteDatabase sqLiteDatabase = getWritableDatabase(); 
     return sqLiteDatabase.delete(TABLE_NAME, COLUMN_NAME_NAME + " = " + name, null) > 0; 
    } 

} 

而且call

DbHelper dbHelper = new DbHelper(getActivity()); 
    dbHelper.insertData(nameImage, resultTV.getText().toString(), finalBitmap); 
+0

你的答案也是可觀的。非常感謝。 – Algor7

相關問題