2016-05-23 75 views
-2

所以我想要做的是,我有一個用戶名,userprofileimage和postimg ,我試圖將這些值插入SQLdatabase我上的長 Classes.DBController.addRecord(Classes.posts)‘:「嘗試調用虛擬方法的M個第一圖像轉換成字節數組,然後插入到數據庫中,但我得到錯誤java.lang.NullPointerException:試圖調用虛擬方法long類

顯示java.lang.NullPointerException’空對象 參考。

這裏是我的代碼

MainActivity

public class Main2Activity extends AppCompatActivity { 
DBController db; 
ListView datalist; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main2); 

} 

public void test(View v) { 
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.download); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] nimg = stream.toByteArray(); 


    Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.bc); 
    ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 
    bm1.compress(Bitmap.CompressFormat.PNG, 100, stream1); 
    byte[] nimg1 = stream.toByteArray(); 

    posts obj = new posts(); 
    obj.setUsername("gitesh"); 
    obj.setUserProfileImg(nimg); 
    obj.setPostImg(nimg1); 
    long result = db.addRecord(obj); 
    if(result==-1) { 
     Log.d("error","error"); 
    } 
    else { 
     datalist = (ListView) findViewById(R.id.listview); 
     db = new DBController(this); 
     List<posts> contacts = db.getAll(); 
     Adapter adp = new Adapter(this, R.layout.activity_main, contacts); 
     datalist.setAdapter(adp); 
     } 


     } 


} 

DBController.class

public class DBController implements DBConstants { 
DBHandler handler; 
SQLiteDatabase db; 

public DBController(Context context) { 
    handler = new DBHandler(context, dname, null, dversion); 
} 

public void open() { 
    db = handler.getWritableDatabase(); 

} 

public void close() { 
    db.close(); 
} 

public long addRecord(posts c) { 
    open(); 
    ContentValues cv = new ContentValues(); 
    cv.put(c2, c.getUsername()); 
    cv.put(c3, c.getUserProfileImg()); 
    cv.put(c4, c.getPostImg()); 
    long d = db.insert(tname, null, cv); 
    close(); 
    return d; 
} 

public List<posts> getAll() { 
    List<posts> l = new ArrayList<>(); 
    String q = "SELECT * FROM " + tname; 
    open(); 
    Cursor c = db.rawQuery(q, null); 
    posts obj = null; 
    if (c.moveToFirst()) { 
     do { 
      obj = new posts(); 
      obj.setUsername(c.getString(1)); 
      obj.setUserProfileImg(c.getBlob(2)); 
      obj.setPostImg(c.getBlob(3)); 
      l.add(obj); 
     } 
     while (c.moveToNext()); 
    } 
    db.close(); 
    return l; 
    } 
    } 

錯誤

05-23 23:16:29.034 2900-2900/com.example.gitesh.timeline E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.gitesh.timeline, PID: 2900 
                     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:5207) 
                      at android.view.View$PerformClick.run(View.java:21168) 
                      at android.os.Handler.handleCallback(Handler.java:746) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5443) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
                     Caused by: java.lang.reflect.InvocationTargetException 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5207)  
                      at android.view.View$PerformClick.run(View.java:21168)  
                      at android.os.Handler.handleCallback(Handler.java:746)  
                      at android.os.Handler.dispatchMessage(Handler.java:95)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5443)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long Classes.DBController.addRecord(Classes.posts)' on a null object reference 
                      at com.example.gitesh.timeline.Main2Activity.test(Main2Activity.java:45) 
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  
                      at android.view.View.performClick(View.java:5207)  
                      at android.view.View$PerformClick.run(View.java:21168)  
                      at android.os.Handler.handleCallback(Handler.java:746)  
                      at android.os.Handler.dispatchMessage(Handler.java:95)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5443)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
+1

那你認爲當你嘗試,你給它分配一個值之前對一個變量運行的方法會發生? – azurefrog

+0

建議:在'onCreate'內放置所有'findViewById'和其他任何初始化。你在'else'語句中成功地放入'db = new DBController(this);',而不是在它之外使用'db'之前。 –

回答

0

似乎db從未實例化。

onCreate方法,你需要具備以下

db = new DBController(this); 
+0

thanksi得到了,但是我在datalist中爲空 – gitesh

+0

不,你得到null是因爲你正在訪問db而沒有實例化它。 –

+0

是的,該問題已解決,但現在沒有找到listview,datalist對象在調試模式下顯示爲空 – gitesh

相關問題