Android veritabanından veri null dönüyor
Merhaba
Veritabanımdaki tüm kayıtları çekip arrayliste yazdırmak istiyorum ama çektiğim değer null dönüyor bu yüzden program hata veriyor.Aldığım hata:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geniusmathgame/com.geniusmathgame.Bolumler}: java.lang.NullPointerException
Kullandığım sınıflar aşağıda.Bu arada MySqliteHelperda soru için gereksiz olan bazı metodları yazmadım soru çok uzamasın diye.Nerede hata yaptığımı çözemedim.Yardımcı olursanız sevinirim.
package com.geniusmathgame;
import java.util.ArrayList;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.example.geniusmathgame.R;
public class Bolumler extends Activity{
ListView lv1;
ListAdapter adapter;
public static String[] dizi;
ArrayList<Math> liste;
MySQLiteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bolumler);
helper=new MySQLiteHelper(this);
//helper.open();
for(int i=1;i<40;i++){
//HATAYA SEBEP OLAN YER BURASI
liste.add(helper.getMath(i));
}
lv1=(ListView)findViewById(R.id.listView1);
}
}
package com.geniusmathgame;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_MATH = "Math";
public static final String COLUMN_ID = "_id";
public final static String COLUMN_LEVELS = "levels";
public static final String COLUMN_STARS = "stars";
private static final String DATABASE_NAME = "Math.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_MATH + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_LEVELS + ", " + COLUMN_STARS+");";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MATH);
onCreate(db);
}
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = {COLUMN_ID,
COLUMN_LEVELS, COLUMN_STARS};
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Math getMath(int id){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_MATH, // a. table
allColumns, // b. column names
" _id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)//if (!cursor.isAfterLast())
cursor.moveToFirst();
// 4. build book object
Math harcama = new Math();
harcama.setLevel(cursor.getInt(1));
harcama.setStar(cursor.getInt(2));
cursor.close();
//db.close();
return harcama;
}
public List<Math> getAllMaths() {
List<Math> maths = new ArrayList<Math>();
Cursor cursor = database.query(TABLE_MATH,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Math math = cursorToMath(cursor);
maths.add(math);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return maths;
}
private Math cursorToMath(Cursor cursor) {
Math math = new Math();
math.setId(cursor.getLong(0));
math.setLevel(cursor.getInt(1));
math.setStar(cursor.getInt(2));
return math;
}
}