gpt4 book ai didi

java - Android 的 rawQuery() 错误

转载 作者:行者123 更新时间:2023-12-01 15:28:56 24 4
gpt4 key购买 nike

当我尝试使用 rawQuery 从数据库中获取一些数据时,出现了 nullPointerException。这里有一个错误:

03-11 18:09:01.522: E/AndroidRuntime(2057): Uncaught handler: thread main exiting due to uncaught exception 03-11 18:09:01.532: E/AndroidRuntime(2057): java.lang.NullPointerException 03-11 18:09:01.532: E/AndroidRuntime(2057): at com.math.scan.FormulaModel.setFormulaList(FormulaModel.java:27)

看看我的代码:

DbHelper

    package com.math.scan;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper extends SQLiteOpenHelper{

public static final String TABLE_NAME = "formulas";

public static final String COLUMN_ID = "_id";
public static final String COLUMN_UNKNOWN = "unknown";
public static final String COLUMN_FORMULA = "formula";
public static final String COLUMN_CTG = "ctg";

private static final String DB_NAME = "formulas.db";
private static int DB_VERSION = 1;

private static final String DB_CREATE = "CREATE TABLE "+TABLE_NAME+
"("+COLUMN_ID+" integer primary key autoincrement,"
+COLUMN_UNKNOWN+" text not null,"
+COLUMN_FORMULA+" text not null,"
+COLUMN_CTG+" text not null );";

public Context mCtx;


public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.mCtx = context;
}

@Override
public void onCreate(SQLiteDatabase database) {

database.execSQL(DB_CREATE);

// reading formulas
AssetManager asset = mCtx.getAssets();
try {
InputStream input = asset.open("formulas");

int size = input.available();

byte[] buffer = new byte[size];
input.read(buffer);
input.close();

String content = new String(buffer);

Scanner scan = new Scanner(content);

String current;
String[] f = new String[2];
String[] formula = new String[2];

while(scan.hasNextLine()) {
current = scan.nextLine();
// get category
f = current.split("!!");
formula = f[1].split(" = ");
database.execSQL("INSERT INTO `formulas` VALUES (NULL, '"+formula[0]+"', '"+formula[1]+"', '"+f[0]+"');");
Log.d("DB", "INSERT INTO `formulas` VALUES (NULL, '"+formula[0]+"', '"+formula[1]+"', '"+f[0]+"');");
}

} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DbHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}

公式模型

package com.math.scan;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class FormulaModel {
private SQLiteDatabase database;
private DbHelper dbHelper;

public FormulaModel(Context context) {
dbHelper = new DbHelper(context);
}

public void open() throws SQLException {
dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public void setFormulaList(String ctg) {
open();
// app stops working here
Cursor cursor = database.rawQuery("SELECT unknown, formula FROM formulas WHERE ctg = '"+ctg+"'", null);
int results = cursor.getCount();
cursor.moveToFirst();

Global.FormuleResult = new String[results];
Global.FormuleTable = new String[results];

for(int i = 0; i < results; i++) {
Global.FormuleTable[i] = cursor.getString(1);
Global.FormuleResult[i] = cursor.getString(0);
}
close();
}
}

这是 Activity ,我在 FormulaModel 类中调用 setFormulaList() 方法。

package com.math.scan;

import net.sourceforge.jeval.EvaluationException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ProblemActivity extends Activity {

private EditText prob;
private Button solve;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.problem);

// text field
prob = (EditText) findViewById(R.id.problem);

// confirm btn
solve = (Button) findViewById(R.id.solve);

// check if confirm button was pressed
solve.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

// get text from the field
String problem = prob.getText().toString();

// check if expression is not empty
if(problem.length() == 0) {
// string is empty!
Toast.makeText(ProblemActivity.this, getString(R.string.empty_field), Toast.LENGTH_SHORT).show();
} else {
FormulaModel f = new FormulaModel(ProblemActivity.this);
f.setFormulaList("mech");
pears.doMagic(problem);
try {
String str = Global.eval.getVariableValue(Global.UNKNOWN);
TextView answ = (TextView) findViewById(R.id.answer);
answ.setText(getString(R.string.prob_answ) + str);
} catch (EvaluationException e) {
Toast.makeText(ProblemActivity.this, getString(R.string.prob_error), Toast.LENGTH_SHORT).show();
}
}
}
});
}

}

我不明白这里出了什么问题。有什么想法吗?

最佳答案

您必须在 open() 中设置数据库变量:

database = dbHelper.getWritableDatabase();

关于java - Android 的 rawQuery() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9811705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com