-1

I'm trying to fetch the data from SQLite database in the form of String array.

In my Activity:

public class CreateTagActivity extends Activity  {

    ListView tagList;
    DBFunctions db;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_tag);

        tagList=(ListView)findViewById(R.id.list_of_tags);
        db=new DBFunctions(CreateTagActivity.this);

        String[] tagsLists=db.getAllTag();

        ArrayAdapter adapter=new ArrayAdapter(CreateTagActivity.this,android.R.layout.simple_list_item_1,tagsLists);
        tagList.setAdapter(adapter);
}
}

In my Database File :

public class DBFunctions extends SQLiteOpenHelper {
private static final String DATABASE_NAME="maindb";
private static final int VERSION=1;
private static final String event_tags="CREATE TABLE "+TABLE_TAGS+"(_id INTEGER PRIMARY KEY AUTOINCREMENT,tag TEXT)";
 private final Context context;

public DBFunctions(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(event_tags);
}

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

    db.execSQL("DROP TABLE IF EXISTS "+event_tags);
    onCreate(db);
}
 public long insertTag(String tag)
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put("tag",tag);
    long id=db.insert(TABLE_TAGS,null,values);
    return id;
}
public String[] getAllTag()
{
    SQLiteDatabase db=this.getWritableDatabase();
    String[] columns={"tag"};
    Cursor cursor=db.query(TABLE_TAGS,columns,null,null,null,null,null);
    int length=cursor.getCount();
    String[] value=new String[length];
    while(cursor.moveToNext())
    {
        int i=0;
        int index=cursor.getColumnIndex("tag");
        value[i]=cursor.getString(index);
        i++;
    }
    return value;
}
}

then i got an error as NullPointerException in tagsList.

here is the Error log:

09-17 11:17:03.695    3084-3084/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pack.proj PID: 3084
java.lang.NullPointerException
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsListView.obtainView(AbsListView.java:2255)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
        at android.widget.ListView.onMeasure(ListView.java:1175)
        at android.view.View.measure(View.java:16540)

Please help me on this. Thanks.

PaolaG
  • 814
  • 1
  • 11
  • 27
Madhan
  • 495
  • 2
  • 10
  • 22

1 Answers1

2
int i=0;

On each loop iteration you reset the index to zero. All other indices in the array remain as null.

The NPE is caused by a null item in the array supplied to ArrayAdapter.

To fix it, move the int i=0 outside the while loop.

laalto
  • 150,114
  • 66
  • 286
  • 303