所以,我正在开发一个应该播放“\ sdcard \ movies”视频的应用程序,为此,它必须查阅我数据库中的表格。但是,select查询在行上返回nullpointer异常:
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;
}
}
有人可以告诉我为什么它会得到一个空值?
===== EDIT =====
这是logcat:
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):at com.sample.VideoViewExample.VideoViewExample.onCreate(VideoViewExample.java:21)
该方法应该返回光标值的另一个类。
答案 0 :(得分:0)
您永远不会调用openDataBase
方法,因此myDataBase
对象在执行到达行时为null
.-
Cursor query = myDataBase.rawQuery("SELECT Arquivo FROM Comercial ORDER BY UltimaExecucao ASC , Random DESC LIMIT 1 ", null);
抛出NullPointerException
。
答案 1 :(得分:0)
你永远不会真正打开数据库。你的checkDatabase函数打开它并关闭它以检查它是否存在但你从未实际打开它以便你可以使用它。
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
//****This is where you need to open it*****
myDataBase = openDataBase();
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}