-6

here is my code by which I entering the data into the database and I'm receiving currentrowId on which the data is entered and the currentrow_id is updating but cursor.getcount() is still null

     SQLiteDatabase dbr = help.getReadableDatabase();

String[] projection = {
        columnid,
        columnimage,
        columnttitle,
        columndesription,
        columnurl
};
Cursor cursor = dbr.query(tablename, projection, null, null, null, null, null);
Cursor cur = dbr.query(tablename, projection, null, null, null, null, null);  
    for (int i = 0; i < mModelList1.size(); i++) {
cursor.moveToFirst();
        if (cursor.getCount()==0){
            Insert_in_a_row(i,1,mModelList1,help);
            cur.moveToLast();
            Log.d("uniq cursor.getCount()=",""+cursor.getCount()); //here i get the cursor.getcount()  null
        }

        Log.d("fist cursor.getCount()=",""+cursor.getCount());
        while (!cursor.isAfterLast()) {

            if(cursor.getCount()>0) {
                cur.moveToLast();
                int Id = cur.getInt(cur.getColumnIndex(columnid)) + 1;
                Log.d("size of mModellist ", "" + mModelList1.size());
                Insert_in_a_row(i, Id, mModelList1, help);
                Log.d("scnd cursor.getCount()=", "" + cur.getCount()); //here i get the cursor.getcount()  null

            }
    cursor.moveToLast();
            cursor.moveToNext();
        }if(i==mModelList1.size()-1){cursor.close();
            cur.close();
            dbr.close();}

    }

Method which I call for inserting Insert_in_a_row()

     public void Insert_in_a_row(int i,int Id,List<NewsModel> 
     mModelList1,News_dbhelper help){
     final SQLiteDatabase dbw = help.getWritableDatabase();
    Log.d("size of mModellist ",""+mModelList1.size());
    try{ URL ur=new URL(mModelList1.get(i).getimage());

   bmp=BitmapFactory.decodeStream(ur.openConnection().getInputStream());

    }catch (IOException e){e.printStackTrace();}
    byte[] byteimage1 = getBytes(bmp);

    ContentValues values = new ContentValues();
    values.put(columnid, Id);
    values.put(columnimage, byteimage1);
    values.put(columnttitle, mModelList1.get(i).getTitle());
    values.put(columndesription,mModelList1.get(i).getDescription());
    values.put(columnurl, mModelList1.get(i).getUrl());
    long currentRow_ID = dbw.insert(tablename, null, values);
    if (Id != currentRow_ID) {
        //Toast.makeText(con, "Error with saving news", Toast.LENGTH_LONG).show();
        Log.d("news was not saved",""+currentRow_ID );// here I'm recieving an updated currentrow_id but cursor.getcount() is still null
    } else {
       // Toast.makeText(con, "You are having " + currentRow_ID + " News", Toast.LENGTH_LONG).show();
        Log.d("news saved on title", ""+currentRow_ID);
    }
}

after that when I fetch records via getreadabledatabase() for receiving data from the database I'm getting error Invalid tables means data is not stored in the database

Updated Error log

  D/news saved on title: 1
 D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7 
I/art: Background partial concurrent mark sweep GC freed 278(23KB) AllocSpace objects, 6(481KB) LOS objects, 37% free, 6MB/10MB, paused 5.591ms total 19.775ms
D/news was not saved: 2
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 3
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 4
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 5
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 6
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
D/size of mModellist: 7
D/news was not saved: 7
D/uniq cursor.getCount()=: 0
D/fist cursor.getCount()=: 0
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
W/System.err:     at com.Newsforest.android.Newsforest.MainActivity.gatherInformation(MainActivity.java:356)
W/System.err:     at com.Newsforest.android.Newsforest.MainActivity.onCreate(MainActivity.java:179)
W/System.err:     at android.app.Activity.performCreate(Activity.java:5990)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:135)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

MainActivity log Message is below

  W/System.err: java.lang.IllegalStateException: Invalid tables
 W/System.err:     at android.database.sqlite.SQLiteDatabase.findEditTable(SQLiteDatabase.java:973)
W/System.err:     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
W/System.err:     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
W/System.err:     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)  
W/System.err:     at com.Newsforest.android.Newsforest.MainActivity.gatherInformation(MainActivity.java:346)
W/System.err:     at com.Newsforest.android.Newsforest.MainActivity.onCreate(MainActivity.java:179)
W/System.err:     at android.app.Activity.performCreate(Activity.java:5990)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:135)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

MainActivity code in which I got the error is below

      try {//News_dbhelper mdbHelper= new News_dbhelper(this);
      SQLiteDatabase db = mdbHelper.getReadableDatabase();

     SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
      queryBuilder.setTables(tablename);
      String[] projection = {
              columnid,
              columnimage,
              columnttitle,
              columndesription,
              columnurl
      };
      Cursor C= db.query(tablename,
              projection,
              null,
              null,
              null,
              null,
              null);

     // Log.d( "gatherInformation: ",""+C.getCount());

      C.moveToFirst();
      while (C.isAfterLast()){
          NewsModel model = new NewsModel();
          //Cursor cursor1 = db.rawQuery(" SELECT * FROM " + tablename + " WHERE " + columnid + "=?", new String[]{String.valueOf(j)});
          model.setTitle(C.getString(C.getColumnIndex(columnttitle)));
          model.setDescription(C.getString(C.getColumnIndex(columndesription)));
          model.setUrl(C.getString(C.getColumnIndex(columnurl)));
          model.setimage(C.getString(C.getColumnIndex(columnimage)));
          mModelList.add(model);
          C.moveToNext();
      }
      C.close();
  }catch (SQLiteException e){e.printStackTrace();
   Toast.makeText(this,"error reason"+e.toString(),Toast.LENGTH_LONG).show();}
   catch (Exception e){e.printStackTrace();}
   finally {
      Log.d("size of mModellist ",""+mModelList.size());
      recyclerView.setAdapter(new Adapter(MainActivity.this,mModelList));

  }

Still, black screen is showing Instead of recyclerView

W/CursorWindow: Window is full: requested allocation 892521 bytes, free space 572848 bytes, window size 2097152 bytes
W/CursorWindow: Window is full: requested allocation 617180 bytes, free space 565848 bytes, window size 2097152 bytes
W/CursorWindow: Window is full: requested allocation 675170 bytes, free space 91920 bytes, window size 2097152 bytes
D/size of mModellist: 8
W/ResourcesManager: Asset path '/system/framework/com.android.media.remotedisplay.jar' does not exist or contains no resources.
W/ResourcesManager: Asset path '/system/framework/com.android.location.provider.jar' does not exist or contains no resources.
D/DynamitePackage: Instantiated singleton DynamitePackage.
D/DynamitePackage: Instantiating com.google.android.gms.ads.ChimeraAdManagerCreatorImpl
  • @ALTegani I updated the Error log have a look – bharat singh Feb 21 '18 at 11:47
  • see this https://stackoverflow.com/questions/45907116/java-lang-nullpointerexception-attempt-to-invoke-interface-method-boolean-andr – EL TEGANI MOHAMED HAMAD GABIR Feb 21 '18 at 11:53
  • @ALTegani thank you but this is not the answer for which I'm looking for. please have a look at MainActivity Error log and MainActivity code in which I'm getting the error. I have updated the question. – bharat singh Feb 21 '18 at 18:30
  • @bharatsingh Questions on SO are meant to be about a specific programming problem. Questions are **not** for the provision of an ongoing debugging service. You should ask a new question. P.S. your issue is very likely due to storing large images in DB when you should store the path in the DB and retrieve the image itself from a file. Plenty of Answers in this regard on SO, so a search should suffice. – MikeT Feb 21 '18 at 21:15
  • @MikeT thank you, but I'm storing the byte[] in the database does it make the UI thread wait for some time. so whats your answer to issue (1) to make a new thread (2) Or saves the path of the image instead of saving the byte[]. Please reply soon – bharat singh Feb 22 '18 at 09:28

1 Answers1

1

A cursor allows you to access the query result set. The query result set does not change if you insert new data after querying. Hence the cursor count stays at 0.

You need to query your database again to see the new data.

The NPE seen as warning in your System.err log is not produced by the version of your code you posted.

laalto
  • 150,114
  • 66
  • 286
  • 303