Reputation: 27
I wrote function which puts some values in a list view but the app is crashing with that error -
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
here is the function-
public void SetInList() {
{
database = UserInfo_Activity.this.openOrCreateDatabase("Runs", MODE_PRIVATE, null);
Cursor c = database.rawQuery("SELECT * FROM " +
TABLE_NAME + "", null);
try {
if (c != null) {
if (c.moveToFirst()) {
do {
String Date = c.getString(c.getColumnIndex("Date"));
int steps = c.getInt(c.getColumnIndex("Steps"));
String Time = c.getString(c.getColumnIndex("Time"));
double Distance = c.getDouble(c.getColumnIndex("Distance"));
Runs.add(Date + " " + Distance + " " + Time);
} while (c.moveToNext());
}
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), se.toString());
}
ArrayAdapter <String> adapter = new ArrayAdapter<String>(UserInfo_Activity.this, android.R.layout.simple_expandable_list_item_1, Runs);
list.setAdapter(adapter);
}
The error is related to this line- list.setAdapter(adapter); and I cant understand why
Here the entire class-
package com.exemple.myapplication;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class UserInfo_Activity extends AppCompatActivity {
SQLiteDatabase database;
private String TABLE_NAME = "RunningLog";
private ArrayList<String> Runs;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info_);
ArrayList<String> Runs = new ArrayList<String>();
list = (ListView) findViewById(R.id.listView);
database = this.openOrCreateDatabase("Runs", 0, null);
database.execSQL("CREATE TABLE IF NOT EXISTS " +
TABLE_NAME +
" (Date VARCHAR," +
" Time VARCHAR, steps INT, distance DOUBLE);");
database.execSQL("DELETE FROM " + TABLE_NAME);
SetInList();
}
public void SetInList() {
{
database = UserInfo_Activity.this.openOrCreateDatabase("Runs", MODE_PRIVATE, null);
Cursor c = database.rawQuery("SELECT * FROM " +
TABLE_NAME + "", null);
try {
if (c != null) {
if (c.moveToFirst()) {
do {
String Date = c.getString(c.getColumnIndex("Date"));
int steps = c.getInt(c.getColumnIndex("Steps"));
String Time = c.getString(c.getColumnIndex("Time"));
double Distance = c.getDouble(c.getColumnIndex("Distance"));
Runs.add(Date + " " + Distance + " " + Time);
} while (c.moveToNext());
}
}
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), se.toString());
}
ArrayAdapter <String> adapter = new ArrayAdapter<String>(UserInfo_Activity.this, android.R.layout.simple_expandable_list_item_1, Runs);
list.setAdapter(adapter);
}
}
}
Upvotes: 1
Views: 51
Reputation: 3597
Again you have declared two Runs one global and one local to the method. Use only the global one.
So change your code like this
Old
ArrayList<String> Runs = new ArrayList<String>();
New
Runs = new ArrayList<String>();
Upvotes: 1