我确信我得到的错误Null Pont Exception错误背后有一些简单的东西,但是,我没有看到它。这是相关的代码:
package com.example.gameexpensetracker;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class Chart_screen_activity extends Activity {
double strategyTotalDB;
DBAdapter db = new DBAdapter(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_screen_activity);
db.open();
pieGraphData();
}
public void pieGraphData() {
Cursor cursor17 = db.rawQuery("SELECT (strategy_total) FROM strategyTotal WHERE _id=(SELECT max(_id) FROM strategyTotal)",
null);
if (cursor17.moveToFirst()){
strategyTotalDB = cursor17.getDouble(0);
cursor17.close();
Toast.makeText(this, "New entry"+strategyTotalDB+ "added!", Toast.LENGTH_LONG).show();
db.close();
}}}
LogCat表示第23行和第32行导致了问题:
12-29 10:46:39.979: D/AndroidRuntime(1754): Shutting down VM
12-29 10:46:39.979: W/dalvikvm(1754): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-29 10:46:39.989: E/AndroidRuntime(1754): FATAL EXCEPTION: main
12-29 10:46:39.989: E/AndroidRuntime(1754): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gameexpensetracker/com.example.gameexpensetracker.Chart_screen_activity}: java.lang.NullPointerException
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.os.Looper.loop(Looper.java:123)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-29 10:46:39.989: E/AndroidRuntime(1754): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 10:46:39.989: E/AndroidRuntime(1754): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 10:46:39.989: E/AndroidRuntime(1754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-29 10:46:39.989: E/AndroidRuntime(1754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-29 10:46:39.989: E/AndroidRuntime(1754): at dalvik.system.NativeStart.main(Native Method)
12-29 10:46:39.989: E/AndroidRuntime(1754): Caused by: java.lang.NullPointerException
12-29 10:46:39.989: E/AndroidRuntime(1754): at com.example.gameexpensetracker.Chart_screen_activity.pieGraphData(Chart_screen_activity.java:32)
12-29 10:46:39.989: E/AndroidRuntime(1754): at com.example.gameexpensetracker.Chart_screen_activity.onCreate(Chart_screen_activity.java:23)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 10:46:39.989: E/AndroidRuntime(1754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-29 10:46:39.989: E/AndroidRuntime(1754): ... 11 more
这是我的DBAdapter:
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;
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_TITLE = "title";
static final String KEY_PLATFORM = "platform";
static final String KEY_GENRE = "genre";
static final String KEY_DATE = "date";
static final String KEY_PRICE = "price";
static final String KEY_TOTAL = "total";
static final String KEY_WHEREBOUGHT = "wherebought";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "gamesDB";
static final String DATABASE_TABLE = "purchases";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS purchases(_id integer primary key autoincrement, "
+ "title text , platform text, genre text , date text , price double , wherebought text );";
static final String DATABASE_CREATE2 = "CREATE TABLE IF NOT EXISTS grandTotal(_id integer primary key autoincrement, "
+ "total double);";
static final String DATABASE_CREATE3 = "CREATE TABLE IF NOT EXISTS strategyTotal(_id integer primary key autoincrement, "
+ "strategy_prices double, strategy_total double);";
static final String DATABASE_CREATE4 = "CREATE TABLE IF NOT EXISTS adventureTotal(_id integer primary key autoincrement, "
+ "adventure_prices double, adventure_total double);";
static final String DATABASE_CREATE5 = "CREATE TABLE IF NOT EXISTS actionTotal(_id integer primary key autoincrement, "
+ "action_prices double, action_total double);";
static final String DATABASE_CREATE6 = "CREATE TABLE IF NOT EXISTS MMOTotal(_id integer primary key autoincrement, "
+ "mmo_prices double, mmo_total double);";
static final String DATABASE_CREATE7 = "CREATE TABLE IF NOT EXISTS racingTotal(_id integer primary key autoincrement, "
+ "racing_prices double, racing_total double);";
static final String DATABASE_CREATE8 = "CREATE TABLE IF NOT EXISTS rpgTotal(_id integer primary key autoincrement, "
+ "rpg_prices double, rpg_total double);";
static final String DATABASE_CREATE9 = "CREATE TABLE IF NOT EXISTS simulationTotal(_id integer primary key autoincrement, "
+ "simulation_prices double, simulation_total double);";
static final String DATABASE_CREATE10 = "CREATE TABLE IF NOT EXISTS sportsTotal(_id integer primary key autoincrement, "
+ "sports_prices double, sports_total double);";
static final String DATABASE_CREATE11 = "CREATE TABLE IF NOT EXISTS otherTotal(_id integer primary key autoincrement, "
+ "other_prices double, other_total double);";
static final String DATABASE_CREATE12 = "CREATE TABLE IF NOT EXISTS puzzleTotal(_id integer primary key autoincrement, "
+ "puzzle_prices double, puzzle_total double);";
double columntotal, strategyPriceInput, actionPriceInput, adventurePriceInput, MMOPriceInput, racingPriceInput, rpgPriceInput, simulationPriceInput,
sportsPriceInput, puzzlePriceInput, otherPriceInput;
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE2);
db.execSQL(DATABASE_CREATE3);
db.execSQL(DATABASE_CREATE4);
db.execSQL(DATABASE_CREATE5);
db.execSQL(DATABASE_CREATE6);
db.execSQL(DATABASE_CREATE7);
db.execSQL(DATABASE_CREATE8);
db.execSQL(DATABASE_CREATE9);
db.execSQL(DATABASE_CREATE10);
db.execSQL(DATABASE_CREATE11);
db.execSQL(DATABASE_CREATE12);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DATABASE_CREATE2);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
}
// ---insert a record into the database---
public long insertRecord(String title, String platform, String genre,
String date, double price, String wherebought) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PLATFORM, platform);
initialValues.put(KEY_GENRE, genre);
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_PRICE, price);
initialValues.put(KEY_WHEREBOUGHT, wherebought);
return db.insert(DATABASE_TABLE, null, initialValues);
}
// delete all records
public void deleteAll() {
context.deleteDatabase(DATABASE_NAME);
}
// ---deletes a particular record---
public boolean deleteRecord(long rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
// ---retrieves all the records---
public Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
KEY_GENRE, KEY_DATE, KEY_PRICE, KEY_WHEREBOUGHT }, null, null,
null, null, null);
}
// ---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException {
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_TITLE, KEY_PLATFORM, KEY_GENRE, KEY_DATE,
KEY_PRICE, KEY_WHEREBOUGHT }, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
// ---updates a record---
public boolean updateRecord(long rowId, String title, String platform,
String genre, String date, double price, String wherebought) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_PLATFORM, platform);
args.put(KEY_GENRE, genre);
args.put(KEY_DATE, date);
args.put(KEY_PRICE, price);
args.put(KEY_WHEREBOUGHT, wherebought);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor rawQuery(String string, Object object) {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:2)
您的DBAdapter.rawQuery()
未实现,它只是一个返回null
的存根。尝试在此moveToFirst()
引用上调用null
会导致NPE。
即改变
public Cursor rawQuery(String string, Object object) {
// TODO Auto-generated method stub
return null;
}
类似
public Cursor rawQuery(String sql, String[] args) {
return db.rawQuery(sql, args);
}
答案 1 :(得分:1)
将数据库初始化移至onCreate
DBAdapter db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_screen_activity);
db = new DBAdapter(this);
答案 2 :(得分:1)
似乎变量cursor17
为空。也许在你的情况下,这改变了if条件有帮助:
if (cursor17 != null && cursor17.moveToFirst()) {
我不知道你的DBAdapter类,但是你的rawQuery()的第二个参数 - 方法没有设置为null,这意味着如果你的表strategyTotal为空,你得到null?我会查看该表是否为空,以及为什么以及您的查询将提供什么结果。