android的数据库帮助类,空指针错误。
问题:去sqlite数据库里查了下,表格建好了,数据也打印了出来。为什么会报空指针错误呢?
错误代码:return db.insert(TB_SONG, null, cv); 第81行。
log:
08-09 09:42:55.574: I/System.out(2370): cv------>SongType=guonei SongUrl=asd _id=11 SongName=zhangsan08-09 09:42:55.585: D/AndroidRuntime(2370): Shutting down VM08-09 09:42:55.595: W/dalvikvm(2370): threadid=3: thread exiting with uncaught exception (group=0x4001b188)08-09 09:42:55.595: E/AndroidRuntime(2370): Uncaught handler: thread main exiting due to uncaught exception08-09 09:42:55.614: E/AndroidRuntime(2370): java.lang.NullPointerException08-09 09:42:55.614: E/AndroidRuntime(2370): at com.xiuman.XMPlayer_DB.DatabaseHelper.insertSong(DatabaseHelper.java:81)08-09 09:42:55.614: E/AndroidRuntime(2370): at com.xiuman.XMPlayer_DB.MainActivity$InsertListener.onClick(MainActivity.java:54)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.View.performClick(View.java:2364)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.View.onTouchEvent(View.java:4179)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.widget.TextView.onTouchEvent(TextView.java:6541)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.View.dispatchTouchEvent(View.java:3709)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)08-09 09:42:55.614: E/AndroidRuntime(2370): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)08-09 09:42:55.614: E/AndroidRuntime(2370): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)08-09 09:42:55.614: E/AndroidRuntime(2370): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.os.Handler.dispatchMessage(Handler.java:99)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.os.Looper.loop(Looper.java:123)08-09 09:42:55.614: E/AndroidRuntime(2370): at android.app.ActivityThread.main(ActivityThread.java:4363)08-09 09:42:55.614: E/AndroidRuntime(2370): at java.lang.reflect.Method.invokeNative(Native Method)08-09 09:42:55.614: E/AndroidRuntime(2370): at java.lang.reflect.Method.invoke(Method.java:521)08-09 09:42:55.614: E/AndroidRuntime(2370): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)08-09 09:42:55.614: E/AndroidRuntime(2370): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)08-09 09:42:55.614: E/AndroidRuntime(2370): at dalvik.system.NativeStart.main(Native Method)08-09 09:42:55.635: I/dalvikvm(2370): threadid=7: reacting to signal 308-09 09:42:55.635: E/dalvikvm(2370): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
package com.xiuman.XMPlayer_DB;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.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class DatabaseHelper extends SQLiteOpenHelper{ private static final String DB_NAME = "XWPlayer_DB"; //数据库名 private static final int VERSION = 1;//数据库版本 private static final String TB_SONG = "Song_TB"; private static final String TB_PLAYERLIST = "PlayerList_TB"; public static final String Song_id = "_id"; public static final String Song_Name = "SongName"; public static final String Song_Url = "SongUrl"; public static final String Song_Type = "SongType"; public static final String PlayerList_id = "_id"; public static final String PlayerList_Name = "SongName"; public static final String PlayerList_SingerName = "SingerName"; private static SQLiteDatabase db; private static DatabaseHelper DBHelper; private final String Create_TB_SQL ="create Table "+TB_SONG+" (_id integer primary key autoincrement, SongName text not null, SongUrl text, SongType text)"; private final String Create_TB_SQL2 ="create table "+TB_PLAYERLIST+" (_id integer primary key autoincrement, SongName text not null, SingerName text not null)"; public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public DatabaseHelper(Context context, String name) { this(context, DB_NAME, VERSION); } public DatabaseHelper(Context context, String name, int version) { this(context, DB_NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("execute create table sql"); db.execSQL(Create_TB_SQL); db.execSQL(Create_TB_SQL2); } public DatabaseHelper open() throws SQLException { db = DBHelper.getWritableDatabase(); return this; } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL(Create_TB_SQL); db.execSQL(Create_TB_SQL2); onCreate(db); } /** * 增加记录 */ public long insertSong(int _id, String SongName, String SongUrl, String SongType) { ContentValues cv = new ContentValues(); cv.put(Song_id, _id); cv.put(Song_Name, SongName); cv.put(Song_Url, SongUrl); cv.put(Song_Type, SongType); System.out.println("cv------>" + cv); return db.insert(TB_SONG, null, cv); } public long insertPlayerList(String SongName, String SingerName) { ContentValues cv = new ContentValues(); cv.put(PlayerList_Name, SongName); cv.put(PlayerList_SingerName, SingerName); return db.insert(TB_PLAYERLIST, null, cv); } /** * 删除记录 */ public int deleteSong(int _id) { String[] whereValue={Integer.toString(_id)}; return db.delete(TB_SONG, "_id=?", whereValue); } public int deletePlayerList(int _id) { String[] whereValue={Integer.toString(_id)}; return db.delete(TB_PLAYERLIST, "_id=?", whereValue); } /** * 更新记录 */ public int updateSong(int _id, String songName, String songUrl, String songType) { ContentValues cv = new ContentValues(); String[] whereValue={Integer.toString(_id)}; cv.put(Song_Name, songName); cv.put(Song_Url, songUrl); cv.put(Song_Type, songType); return db.update(TB_SONG, cv, "_id=?", whereValue); } public int updatePlayerList(int _id, String songname, String singername) { ContentValues initialValues = new ContentValues(); String[] whereValue={Integer.toString(_id)}; initialValues.put(PlayerList_Name, songname); initialValues.put(PlayerList_SingerName, singername); return db.update(TB_PLAYERLIST, initialValues, "_id=?", whereValue); } /** * 查询记录 */ public Cursor selectSong() { SQLiteDatabase db = getReadableDatabase(); return db.query(TB_SONG, new String[]{"_id","SongName","SongUrl", "SongType"}, "id=?", new String[]{"1"}, null, null, " _id desc"); } public Cursor selectPlayerList() { SQLiteDatabase db = getReadableDatabase(); return db.query(TB_PLAYERLIST, new String[]{"_id","SongName","SingerName"}, "id=?", new String[]{"1"}, null, null, " _id desc"); } public List<Song> getAllSongObj() { List<Song> SongSet = new ArrayList<Song>(); Cursor c = db.query(TB_SONG, new String[] {Song_id, Song_Name, Song_Url, Song_Type},null,null,null,null,null); for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()){ Song song = new Song(); song.setSongId(c.getInt(0)); song.setSongName(c.getString(1)); song.setSongUrl(c.getString(2)); song.setSongType(c.getString(3)); SongSet.add(song); } c.close(); return SongSet; } public List<PlayerList> getAllPlayerListObj() { List<PlayerList> PlayerListSet = new ArrayList<PlayerList>(); Cursor c = db.query(TB_PLAYERLIST, new String[] {PlayerList_id, PlayerList_Name, PlayerList_SingerName},null,null,null,null,null); for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext()){ PlayerList pl = new PlayerList(); pl.setId(c.getInt(0)); pl.setSongName(c.getString(1)); pl.setSingerName(c.getString(2)); PlayerListSet.add(pl); } c.close(); return PlayerListSet; }}
ContentValues cv = new ContentValues(); cv.put(Song_Name, SongName); cv.put(Song_Url, SongUrl); cv.put(Song_Type, SongType); System.out.println("cv------>" + cv); open(); return db.insert(TB_SONG, "SongName, SongUrl, SongType", cv);
[解决办法]
public DatabaseHelper(Context context, String name) {
this(context, DB_NAME, VERSION);
}
public DatabaseHelper(Context context, String name, int version)
{
this(context, DB_NAME, null, VERSION);
}
改为:
public DatabaseHelper(Context context, String name) {
this(context, DB_NAME, VERSION);
DBHelper = new DatabaseHelper(context, DB_NAME, null, VERSION);
}
public DatabaseHelper(Context context, String name, int version)
{
this(context, DB_NAME, null, VERSION);
DBHelper = new DatabaseHelper(context, DB_NAME, null, VERSION);
}
看看