所以,我正在开发一个应该播放“\sdcard\movies”中的视频的应用程序,为此它必须查阅我数据库中的一个表。但是,选择查询在该行返回一个空指针异常:
Cursor query = myDataBase.rawQuery("SELECT Arquivo FROM Comercial ORDER BY UltimaExecucao ASC , Random DESC LIMIT 1 ", null);
这是我的代码:
DBHandler.java
public class DBHandler extends SQLiteOpenHelper {
private static String DB_PATH = "/sdcard/movies/";
private static String DB_NAME = "audiostore.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHandler(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public String getData() {
String str = new String();
Cursor query = myDataBase.rawQuery("SELECT Arquivo FROM Comercial ORDER BY UltimaExecucao ASC , Random DESC LIMIT 1 ", null);
str = query.getString(query.getColumnIndex(str));
return str;
}
}
有人可以告诉我为什么它得到一个空值?
=====编辑=====
这是日志猫:
10-02 21:19:50.004: E/AndroidRuntime(507): FATAL EXCEPTION: main
10-02 21:19:50.004: E/AndroidRuntime(507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.VideoViewExample/com.sample.VideoViewExample.VideoViewExample}: java.lang.NullPointerException
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.os.Looper.loop(Looper.java:126)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.ActivityThread.main(ActivityThread.java:3997)
10-02 21:19:50.004: E/AndroidRuntime(507): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 21:19:50.004: E/AndroidRuntime(507): at java.lang.reflect.Method.invoke(Method.java:491)
10-02 21:19:50.004: E/AndroidRuntime(507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-02 21:19:50.004: E/AndroidRuntime(507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-02 21:19:50.004: E/AndroidRuntime(507): at dalvik.system.NativeStart.main(Native Method)
10-02 21:19:50.004: E/AndroidRuntime(507): Caused by: java.lang.NullPointerException
10-02 21:19:50.004: E/AndroidRuntime(507): at com.sample.VideoViewExample.DBHandler.getData(DBHandler.java:158)
10-02 21:19:50.004: E/AndroidRuntime(507): at com.sample.VideoViewExample.VideoViewExample.onCreate(VideoViewExample.java:21)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-02 21:19:50.004: E/AndroidRuntime(507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
10-02 21:19:50.004: E/AndroidRuntime(507): ... 11 more
在哪里:
10-02 21:19:50.004: E/AndroidRuntime(507): 在 com.sample.VideoViewExample.VideoViewExample.onCreate(VideoViewExample.java:21)
是另一个类,其中方法应该返回游标的值。