**DISCLAIMER** *I am new to Android and I am following a tutorial*

I am trying to print the database into the string **dbString** in the **MyDbHandler** file under method **databaseToString**

It's not working because the dbString stays empty.

I've also tried removing the while loop and only using this code:

```
dbString += c.getString(c.getColumnIndex("productname"));

```

which gives me the following runtime error:

```
FATAL EXCEPTION: main
                                                                              Process: com.example.jarno.a49_savingdata, PID: 10976
                                                                              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:5637)
                                                                                  at android.view.View$PerformClick.run(View.java:22429)
                                                                                  at android.os.Handler.handleCallback(Handler.java:751)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                               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:5637) 
                                                                                  at android.view.View$PerformClick.run(View.java:22429) 
                                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                               Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
                                                                                  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                                                  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                                  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                                  at com.example.jarno.a49_savingdata.MyDbHandler.databaseToString(MyDbHandler.java:65)
                                                                                  at com.example.jarno.a49_savingdata.MainActivity.printDatabase(MainActivity.java:45)
                                                                                  at com.example.jarno.a49_savingdata.MainActivity.addButtonClicked(MainActivity.java:35)
                                                                                  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:5637) 
                                                                                  at android.view.View$PerformClick.run(View.java:22429) 
                                                                                  at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

```

Looking at the following code I am assuming that either the product isn't added correctly or the print method can't read the database correctly.

```
caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

```

**MyDbHandler.java**

```
package com.example.jarno.a49_savingdata;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

    public class MyDbHandler extends SQLiteOpenHelper {

    private static final String TAG = "debug";

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "products.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    public MyDbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_PRODUCTNAME + " TEXT "  +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP_TABLE_IF_EXISTS" + TABLE_PRODUCTS);
        onCreate(db);
    }

    public void addProduct(Products product) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    public void delProduct(String productName) {
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";" );
    }

    public String databaseToString() {
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
        // Cursor point to a location in the results
        Cursor c = db.rawQuery(query, null);
        // Move to the first row in the results
        c.moveToFirst();

       while(!c.isAfterLast()) {
            if(c.getString(c.getColumnIndex("productname")) != null) {
                dbString += c.getString(c.getColumnIndex("productname"));
                Log.i(TAG, "loop");
                dbString += "\n";
                c.moveToNext();
            }
        }

        Log.i(TAG, dbString);
        db.close();
        return dbString;
    }
}

```

**MainActivity.java**

```
package com.example.jarno.a49_savingdata;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "debug";

    EditText jarnoInput;
    TextView jarnoText;
    MyDbHandler dbHandler;

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

        jarnoInput = (EditText) findViewById(R.id.jarnoInput);
        jarnoText = (TextView) findViewById(R.id.jarnoText);

        dbHandler = new MyDbHandler(this, null, null, 1);
        //printDatabase();

    }

    public void addButtonClicked(View v) {
        Products product = new Products(jarnoInput.getText().toString());
        dbHandler.addProduct(product);
        printDatabase();
    }

    public void delButtonClicked(View v) {
        String inputText = jarnoInput.getText().toString();
        dbHandler.delProduct(inputText);
        printDatabase();
    }

    public void printDatabase() {
        String dbString = dbHandler.databaseToString();
        jarnoText.setText(dbString);
        jarnoInput.setText("");
    }
}

```

**Products.java**

```
package com.example.jarno.a49_savingdata;

public class Products {

    private int _id;
    private String _productname;

    public Products() {
    }

    public Products(String productname) {
        this._productname = productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }
}

```

**Edit1**

Changed the code according to the answer to this:

```
Cursor c = db.query(
                TABLE_PRODUCTS,
                new String[] { "productname" },
                "1",
                new String[] { "id" },
                null,
                null,
                null
        );

        // Move to the first row in the results
        c.moveToFirst();

        if (c.getCount() >= 1) {
            while(c.moveToNext()) {
                if(c.getString(c.getColumnIndex("productname")) != null) {
                    dbString += c.getString(c.getColumnIndex("productname"));
                    Log.i(TAG, "loop");
                    dbString += "\n";
                }
            }
        }

```

Receiving the following error:

```
FATAL EXCEPTION: main
                                                                                Process: com.example.jarno.a49_savingdata, PID: 9807
                                                                                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:5637)
                                                                                    at android.view.View$PerformClick.run(View.java:22429)
                                                                                    at android.os.Handler.handleCallback(Handler.java:751)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:154)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                 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:5637) 
                                                                                    at android.view.View$PerformClick.run(View.java:22429) 
                                                                                    at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                                                                                 Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.
                                                                                    at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
                                                                                    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
                                                                                    at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
                                                                                    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
                                                                                    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                                                    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                                                    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                                                    at com.example.jarno.a49_savingdata.MyDbHandler.databaseToString(MyDbHandler.java:61)
                                                                                    at com.example.jarno.a49_savingdata.MainActivity.printDatabase(MainActivity.java:45)
                                                                                    at com.example.jarno.a49_savingdata.MainActivity.addButtonClicked(MainActivity.java:35)
                                                                                    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:5637) 
                                                                                    at android.view.View$PerformClick.run(View.java:22429) 
                                                                                    at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                    at android.os.Looper.loop(Looper.java:154) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

```