1

I am using GraphView library code which contains DataPoint[] Array. My code also contains a database named olddb which I use to insert the array length in the code below. The first 2 for loops are for adding the values in the database into an array and the third for loop is to get the values out from the arrays arrayWeight and arrayId and add them to DataPoint[] array.

Though something seems to go wrong, after I run the program it just crashes, can anyone figure out what is causing the application to crash?

private DataPoint[] getDataPoint() {
        if (olddb.check()) {
            List<oldDetails> details = olddb.getDetails();
            double[] arrayWeight = new double[olddb.getDetailsCount()];
            int[] arrayId = new int[olddb.getDetailsCount()];
            for (oldDetails cn : details) { //Adding weights and id of all time to an array.
                double num = cn.getWeight();
                int id = cn.getId();
                for (int i = arrayWeight.length; i > 0; i--) {
                    arrayWeight[i] = num;
                    arrayId[i] = id;
                }
            }
            DataPoint[] dp = new DataPoint[olddb.getDetailsCount()];
            for (int i = 0; i < arrayId.length; i++) {
                for (int j = 0; j < arrayWeight.length; j++)
                    dp[i] = new DataPoint(i, arrayWeight[j]);
            }

            return dp;
        }
        else {
            DataPoint[] dp = new DataPoint[]{
                    new DataPoint(0, 0)
            };
            return dp;
        }
    }

Here is an example for working code of DataPoint array:

private DataPoint[] getDataPoint(){
    DataPoint[] dp = new DataPoint[]{
                new DataPoint(0,1),
                new DataPoint(2,5),
                new DataPoint(5,5),
                new DataPoint(7,4)
    };
    return dp;
}

EDIT: here is the error message:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.none.myapplication, PID: 3881
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.none.myapplication/com.none.myapplication.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT  * FROM oldDetails

Database

Yotam Dahan
  • 689
  • 1
  • 9
  • 33
  • it will tell you in LogCat what the crash is, learn to read it: https://developer.android.com/studio/debug/am-logcat.html – Blundell Dec 30 '16 at 22:25
  • @Blundell I found a message finally https://codeshare.io/2EzBo5 I really don't understand what is the problem – Yotam Dahan Dec 30 '16 at 22:38
  • `java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails` is the error, which happens inside of `DatabaseHandlerOld.getDetailsCount` line 141 – Blundell Dec 30 '16 at 22:41
  • @Blundell do you find anything wrong here? https://codeshare.io/5RkbWa – Yotam Dahan Dec 30 '16 at 22:43
  • yes, you must close your database connection inside of `DatabaseHandlerOld.getDetailsCount` but inside `getDataPoint()` you call this method twice, therefore it crashes – Blundell Dec 30 '16 at 22:44
  • @Blundell So I should add it into variable? – Yotam Dahan Dec 30 '16 at 22:45

2 Answers2

0

You can fix it by replacing all 3 calls to olddb.getDetailsCount()

with

int count = olddb.getDetailsCount();

then:

double[] arrayWeight = new double[count];
int[] arrayId = new int[count];
DataPoint[] dp = new DataPoint[count];

However you need to understand the issue:

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM oldDetails is the error, which happens inside of DatabaseHandlerOld.getDetailsCount line 141

Android SQLite DB When to Close

Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233
0

Well after some researches and some of Blundell help, I figured out the problem was indeed the close(); method in my database. Though the solution was to change the getDetailsCount() function to this:

public long QueryNumEntries()
{
    SQLiteDatabase db = this.getReadableDatabase();
    return DatabaseUtils.queryNumEntries(db, TABLE_OLDDETAILS);
}

and in my main activity file:

private DataPoint[] getDataPoint() {
        if (olddb.check()) {
            List<oldDetails> details = olddb.getDetails();
            long c = olddb.QueryNumEntries();
            int count = (int) c;
            Log.d("Count", "equals: " + count);

        double[] arrayWeight = new double[count];
        int[] arrayId = new int[count];
        for (oldDetails cn : details) { //Adding weights and id of all time to an array.
            double num = cn.getWeight();
            int id = cn.getId();
            Log.d("num", "equals: " + num);
            Log.d("id", "equals: " + id);
            for (int i = count; i < 0; i--) {
                arrayWeight[i] = num;
                arrayId[i] = id;
            }
        }
        DataPoint[] dp = new DataPoint[count];
        for (int i = 0; i < arrayId.length; i++) {
            for (int j = 0; j < arrayWeight.length; j++) {
                dp[i] = new DataPoint(arrayId[i], arrayWeight[j]);
                Log.d("ArrayWeight", "equals: " + arrayWeight[j]);
                Log.d("ArrayId", "equals: " + arrayId[i]);
            }
        }

        return dp;
    }

        DataPoint[] dp = new DataPoint[]{
                new DataPoint(0, 0)
        };
        return dp;
    }
Yotam Dahan
  • 689
  • 1
  • 9
  • 33