调用sqlite数据库时出错

时间:2017-12-05 05:05:08

标签: java android sqlite nullpointerexception

我正在学习创建一个应用程序,特别是从数据库(已经创建)获取数据并将其设置为文本。我遵循了几个教程但是我收到了一个错误(代码本身不是错误,但是应用程序崩溃了)。

这是数据库类:

public class ManejadorBaseDeDatos extends SQLiteAssetHelper {

private static String DB_PATH = "data/data/com.xxkuakxxgmail.knowyourchamp/databases/";
private static String DB_NAME = "imcEmbar.db";

private static SQLiteDatabase mDataBase;

private static ManejadorBaseDeDatos sInstance = null;

private static final int DATABASE_VERSION = 1;


public ManejadorBaseDeDatos() {
    super(MainActivity.activity, DB_NAME, null, DATABASE_VERSION);

    try {
        createDataBase();
        openDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public static ManejadorBaseDeDatos instance() {

    if (sInstance == null) {
        sInstance = new ManejadorBaseDeDatos();
    }
    return sInstance;
}


private void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    SQLiteDatabase db_Read = null;

    if (dbExist) {

    } else {

        db_Read = this.getReadableDatabase();
        db_Read.close();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error");
        }
    }
}

public boolean checkDataBase()
{
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

public void copyDataBase() throws IOException {

    InputStream myInput = MainActivity.activity.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();

}

private void openDataBase() throws SQLException {

    String myPath = DB_PATH + DB_NAME;
    mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}


public Cursor select(String query) throws SQLException {
    return mDataBase.rawQuery(query, null);
}

@Override
public synchronized void close() {

    if (mDataBase != null)
        mDataBase.close();

    super.close();

}

这是我想要调用数据库的类:

public class Imc_Ema extends AppCompatActivity {

    Button btnCalcular;
    EditText txtIngresoSemanas;
    EditText txtResultado;
    ManejadorBaseDeDatos manejadorBD;

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

        manejadorBD = ManejadorBaseDeDatos.instance();
        btnCalcular = (Button)findViewById(R.id.btnCalcular);
        txtIngresoSemanas = (EditText) findViewById(R.id.txtIngresoSemanas);

        btnCalcular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                txtResultado.setText(null);
                Double semanas = Double.valueOf(txtIngresoSemanas.getText().toString());
                Cursor cursor = manejadorBD.select("SELECT NormaMin FROM imcEmb WHERE Codigo_producto = '" + semanas + "' ");
                String consulta = cursor.getString(cursor.getColumnIndex("NormaMin"));
                txtResultado.setText(consulta);
            }
        });
    }
}

错误:

12-04 22:07:59.433 13916-13916/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.xxkuakxxgmail.knowyourchamp, PID: 13916
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxkuakxxgmail.knowyourchamp/com.xxkuakxxgmail.knowyourchamp.Imc_Ema}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
                                                   at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init>(SQLiteAssetHelper.java:109)
                                                   at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.<init>(SQLiteAssetHelper.java:129)
                                                   at com.xxkuakxxgmail.knowyourchamp.ManejadorBaseDeDatos.<init>(ManejadorBaseDeDatos.java:0)
                                                   at com.xxkuakxxgmail.knowyourchamp.ManejadorBaseDeDatos.instance(ManejadorBaseDeDatos.java:46)
                                                   at com.xxkuakxxgmail.knowyourchamp.Imc_Ema.onCreate(Imc_Ema.java:25)
                                                   at android.app.Activity.performCreate(Activity.java:6705)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:154) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

0 个答案:

没有答案