0

Im new to android, and i use Sqlite to store an array of type double into a column called "KEY_VALUE"

public long createEntry(Double rates) { 
    ContentValues cv = new ContentValues();
    cv.put(KEY_VALUE, rates);
    return ourDatabase.insert(TABLE, null, cv);
}

I put the data into the columns here via another class

for(i=0;i<37;i++){
    entry.createEntry((theRSSHandler.rates()[i]));
    }

Now i would like to retrieve the column i saved and get each row as an array element, ive seen and tried other similar solutions but they have not worked. Here is the method i use to try and get the column data But it has failed.

public Double[] getData() {
    String[] col = {KEY_VALUE}; 
    Cursor c = ourDatabase.query(TABLE, col, KEY_VALUE , null, null, null, null);
    Double[] result = null;

    if(c != null){
        c.moveToFirst();
    }

    int iVal = c.getColumnIndex(KEY_VALUE);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result[c.getPosition()] = c.getDouble(iVal);
    }
    return result;
}
2
  • it has failed...how? can you show the LogCat? Commented Feb 3, 2013 at 13:09
  • it shows null pointer exception Commented Feb 3, 2013 at 13:11

1 Answer 1

1

you are trying to put values in an array that has not been initialized.

  Double[] result = null;

needs to be followed up with something like...

  result = new Double[100];

i think you probably want to initialize it with the number of records pointed to by the cursor

  result = new Double[c.count()]; 
Sign up to request clarification or add additional context in comments.

1 Comment

i could be slightly off on the syntax, as i'm doing this from memory (all my "good" code is at work.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.