空指针在与Android的SQL查询 [英] Nullpointer on sql query with android

查看:77
本文介绍了空指针在与Android的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与main.class和main_fragment.class

i have an android app with an main.class and a main_fragment.class

在片段类我有这个code部分:

in the fragment class i have this code part:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String query = "INSERT INTO " + SqlDbHelper.DATABASE_TABLE + " "
            + "(firstname,second name,bday) "
            + "values ('Max','Mustermann','20.05.2016')";

    Log.e ("-->",""+query);

    sqlHandler.executeQuery(query);
}

我将在我的数据库表喜欢设置此值。
但与显示java.lang.NullPointerException此行我的应用程序崩溃:

i would like set this values in my database table. but my app crash on this line with a java.lang.NullPointerException:

sqlHandler.executeQuery(查询);

sqlHandler.executeQuery(query);

我不明白这一点。
我Log.e结果是:

i don't understand this. my Log.e result:

INSERT INTO产品(名字,secondname,BDAY)VALUES('最大'Mustermann,2016年5月20日')

INSERT INTO PRODUCTS (firstname,secondname,bday) values ('Max,'Mustermann','20.05.2016')

谁能帮助我?

更新
这是我的sqlHandler.class

UPDATE This is my sqlHandler.class

public class SqlHandler {

public static final String DATABASE_NAME = "DATABASE";
public static final int DATABASE_VERSION = 1;
Context context;
SQLiteDatabase sqlDatabase;
SqlDbHelper dbHelper;

public SqlHandler(Context context) {
    dbHelper = new SqlDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    sqlDatabase = dbHelper.getWritableDatabase();
}

public void executeQuery(String query) {
    try {

        if (sqlDatabase.isOpen()) {
            sqlDatabase.close();
        }

        sqlDatabase = dbHelper.getWritableDatabase();
        sqlDatabase.execSQL(query);

    } catch (Exception e) {

        System.out.println("DATABASE ERROR " + e);
    }

}

public Cursor selectQuery(String query) {
    Cursor c1 = null;
    try {

        if (sqlDatabase.isOpen()) {
            sqlDatabase.close();

        }
        sqlDatabase = dbHelper.getWritableDatabase();
        c1 = sqlDatabase.rawQuery(query, null);

    } catch (Exception e) {

        System.out.println("DATABASE ERROR " + e);

    }
    return c1;

}

}

推荐答案

第一个code片断您提供不足以回答与任何程度的问题。从异常会出现的sqlHandler变量是空的,但你没有提供任何code,以显示你如何实例化这一点。

The first code snippet you have provided is not sufficient to answer the question with any degree of certainty. From the exception it would appear that the sqlHandler variable is null, but you have not provided any code to show how you are instantiating this.

sqlHandler似乎是一个全局变量,所以你会希望你在使用它,或者只是调用它的方法之前类的构造函数中使用它,比如之前将其设置为SqlHandler类的一个实例。

sqlHandler seems to be a global variable, so you'll want to set it to an instance of the SqlHandler class before you use it, eg in the constructor of the class that uses it or just before calling one of its methods.

sqlHandler = new SqlHandler(*some var of type Context*);

这篇关于空指针在与Android的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆