Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:56:17+00:00 2026-06-05T06:56:17+00:00

Helper Class: package com.deangrobler.myApp; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context;

  • 0

Screenshot of SQLite database

Helper Class:

package com.deangrobler.myApp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.*;

public class DataBaseHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.deangrobler.chumperoo/databases/"; 
private static String DB_NAME = "ChumperooDB.db"; 
private SQLiteDatabase myDataBase; 

private final Context myContext;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 * @param context
 */
public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}   

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException{

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
           //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database: " + e.getStackTrace());

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){

        //database does't exist yet.

    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

        if(myDataBase != null)
            myDataBase.close();

        super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

//Queries the database
public Cursor queryDataBase(String table, String[] columns, String selection){

    return myDataBase.query(table, columns, selection, null, null, null, null);

}

}

Using in my Main.java like so:

DataBaseHelper myDbHelper = new DataBaseHelper(this);

    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database"); 
    }

    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }

    //Get cursor and print out data
    Cursor returnedCur = myDbHelper.queryDataBase("Expenses", new String[]{"Name","Amount"}, null);
    if (returnedCur.moveToFirst()){
        do{
            String data = returnedCur.getString(returnedCur.getColumnIndex("data"));
            System.out.println("Returned Data: "+data);

        }while(returnedCur.moveToNext());
    }
    returnedCur.close();

And finaly my entire log:

> 06-05 13:28:35.885: E/CursorWindow(733): Bad request for field slot 0,-1. numRows = 1, numColumns = 2
06-05 13:28:35.885: D/AndroidRuntime(733): Shutting down VM
06-05 13:28:35.885: W/dalvikvm(733): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-05 13:28:35.904: E/AndroidRuntime(733): FATAL EXCEPTION: main
06-05 13:28:35.904: E/AndroidRuntime(733): **java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deangrobler.chumperoo/com.deangrobler.chumperoo.Main}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed**
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.os.Looper.loop(Looper.java:123)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.ActivityThread.main(ActivityThread.java:3647)
06-05 13:28:35.904: E/AndroidRuntime(733):  at java.lang.reflect.Method.invokeNative(Native Method)
06-05 13:28:35.904: E/AndroidRuntime(733):  at java.lang.reflect.Method.invoke(Method.java:507)
06-05 13:28:35.904: E/AndroidRuntime(733):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 13:28:35.904: E/AndroidRuntime(733):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 13:28:35.904: E/AndroidRuntime(733):  at dalvik.system.NativeStart.main(Native Method)
06-05 13:28:35.904: E/AndroidRuntime(733): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.database.CursorWindow.getString_native(Native Method)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.database.CursorWindow.getString(CursorWindow.java:329)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
06-05 13:28:35.904: E/AndroidRuntime(733):  at com.deangrobler.chumperoo.Main.onCreate(Main.java:55)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-05 13:28:35.904: E/AndroidRuntime(733):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
06-05 13:28:35.904: E/AndroidRuntime(733):  ... 11 more
06-05 13:28:53.264: I/Process(733): Sending signal. PID: 733 SIG: 9

No idea why this is happening, I’ve been googling for half the day now and nothing is helping me sort this out 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:56:18+00:00Added an answer on June 5, 2026 at 6:56 am
    new String[]{"Name","Amount"}
    

    you are asking for “data” field but I cant found any field name “data” in your db.
    you can try with

    String name = returnedCur.getString(returnedCur.getColumnIndex("Name"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package com.ustr.eMIRnew; import java.util.ArrayList; import java.util.HashMap; import android.R; import android.app.Activity; import android.os.Bundle; import android.widget.ListView;
1st activity package com.example.parserss; import java.net.URL; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.XMLReader;
Manifest: <manfiest package=com.backme ... /> <application android:backupAgent=PrefsBackupAgent .../> <meta-data android:name=com.google.android.backup.api_key android:value=12345.... /> Code: public
I currently have a helper class that I am using to obfuscate a static
I am using a helper class in my app, to access a database and
I have a helper class designed to get data from Core Data and pass
In my razor style helper class (located in the App_Code folder, I've got this
I am looking for a helper class/method/gem that's out there that will help me
I am Having a DataBase Helper class which is not an activity. Here I
I am using a ado.net helper class from Here . I don't know how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.