创建sqlite数据库时出现空指针异常

时间:2014-02-20 03:58:57

标签: android nullpointerexception android-sqlite sql-insert

我在将数据插入Database时遇到此异常。我是android初学者,任何人请帮我解决这个问题....我无法插入数据......我发布的是代码和LOGCAT错误....

公共类Db扩展了SQLiteOpenHelper {

Context context;
public static String databasename="modelnew.db";
private static final int SCHEMA_VERSION = 1;
public static String category_table="category";
public static String Id="categoryid";
public static String Name="caregoryname";



public Db(Context context) {
    //super(context,Environment.getExternalStorageDirectory()+"/"+databasename, null, 1);
    super(context,databasename, null, SCHEMA_VERSION);
    // TODO Auto-generated constructor stub
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String query="create table category_table(Id integer primary key ,Name text)";
    db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

class ProgressTask extends AsyncTask<String, String, JSONObject> 
{



    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

    }

    @Override
    protected JSONObject doInBackground(String... args) 
    {

        System.out.println("Inside do inbackground"+ purl);

        Jsondbparser jParser = new Jsondbparser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(purl);

        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) 
    {

        try 
        {

            System.out.println("returnjson"+json);
            // Getting JSON Array from URL

            JSONObject json1 = json.getJSONObject("response");

            System.out.println(json1.getString("success"));

            jsonary = json1.getJSONArray("categoryid");

            jsonarr=json1.getJSONArray("categoryname");

            for (int i = 0; i < jsonary.length(); i++) 
            {

                categoryId = jsonary.getInt(i);

                System.out.println("id-- "+categoryId);

                categoryName = jsonarr.getString(i);

                System.out.println("id-- "+categoryName);

                idlist.add(String.valueOf(categoryId));

                list.add(categoryName);

                addCategory();

                /*db=new Db(con);

                Model model=new Model();

                model.setcid(categoryId);

                model.setcname(categoryName);

                addCategory(model);

                //close();*/

            }

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

    }
}

void addCategory()
{

   System.out.println("inside insert method");

    Db h = new Db(getApplicationContext());

    SQLiteDatabase db = h.getWritableDatabase();

    for(int j=0;j<=idlist.size();j++)
    {

        ContentValues values = new ContentValues();

        values.put("categoryid", idlist.get(j));//category id

        values.put("categoryname", list.get(j)); // Category Name

        System.out.println("id ---"+idlist.get(j));

        System.out.println("name-----"+list.get(j));

        // Inserting Row
        db.insert("t_category", null, values);

        db.close(); // Closing database connection

        System.out.println("before end");

    }


}

}

02-20 03:54:17.192: E/AndroidRuntime(931): FATAL EXCEPTION: main
02-20 03:54:17.192: E/AndroidRuntime(931): java.lang.NullPointerException
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
02-20 03:54:17.192: E/AndroidRuntime(931):  at com.example.koto.Jsondb.addCategory(Jsondb.java:133)
02-20 03:54:17.192: E/AndroidRuntime(931):  at com.example.koto.Jsondb$ProgressTask.onPostExecute(Jsondb.java:102)
02-20 03:54:17.192: E/AndroidRuntime(931):  at com.example.koto.Jsondb$ProgressTask.onPostExecute(Jsondb.java:1)
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.os.AsyncTask.finish(AsyncTask.java:631)
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.os.Looper.loop(Looper.java:137)
02-20 03:54:17.192: E/AndroidRuntime(931):  at android.app.ActivityThread.main(ActivityThread.java:5041)
02-20 03:54:17.192: E/AndroidRuntime(931):  at java.lang.reflect.Method.invokeNative(Native Method)
02-20 03:54:17.192: E/AndroidRuntime(931):  at java.lang.reflect.Method.invoke(Method.java:511)
02-20 03:54:17.192: E/AndroidRuntime(931):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-20 03:54:17.192: E/AndroidRuntime(931):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-20 03:54:17.192: E/AndroidRuntime(931):  at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

你应该替换这个

 Db h = new Db(getApplicationContext());

使用

 Db h = new Db(youractivity.this);

传递活动上下文而不是应用程序上下文。

答案 1 :(得分:0)

避免使用getApplicationContext()并使用YourPresentActivityClass.this代替getApplicationContext(),并确保在使用之前没有传递null或仔细检查它们。

答案 2 :(得分:0)

当您使用以下行中的Db初始化getApplicationContext()对象时......它没有获得任何Context ...

    Db h = new Db(getApplicationContext());

要解决Context问题,您可以通过AsyncTask()构造函数传递活动的上下文。我给你一个场景......

假设,MainActivity.java您正在执行ProgressTask.java,那么您应该按照以下方式传递Context ....

new ProgressTask(MainActivity.this).execute();

ProgressTask.java课程中,您可以从Context构造函数中检索ProgressTask(),如下所示....

public ProgressTask extends AsyncTask<String, String, JSONObject>  {

    Context mContext;

    ProgressTask (Context context) {

        mContext.this = context

    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

    }

    .........

}

现在,您可以按如下方式初始化Db对象...

Db h = new Db(mContext);

这将解决您的问题。