5

I'm getting this exception while inserting data into Database.I'm android beginner anyone please help me out with this....I'm not able to insert data......I'm posting my code and LOGCAT error....

public class Db extends 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)
6
  • 2
    you should post your code , we can't figure out the error without your cod e Commented Feb 20, 2014 at 4:00
  • You may want to ensure that everything you are trying to insert is valid (i.e. no null values). Commented Feb 20, 2014 at 4:06
  • @mohammedmomn I have created the database in Db class and try to insert data from above method which is in another class but error is repeating I don't know what mistake I have done.... Commented Feb 20, 2014 at 4:09
  • @Somasundaram check with dummy value to insert in your db whether it inserted in your db or not after that you pass your data from services Commented Feb 20, 2014 at 4:11
  • @ErstwhileIII I have checked the values before inserting but it showing error.... 02-20 03:54:17.131: I/System.out(931): id-- 1 02-20 03:54:17.131: I/System.out(931): id-- Fruits 02-20 03:54:17.131: I/System.out(931): inside insert method 02-20 03:54:17.131: D/AndroidRuntime(931): Shutting down VM 02-20 03:54:17.131: W/dalvikvm(931): threadid=1: thread exiting with uncaught exception (group=0x40a71930) java.lang.NullPointerException Commented Feb 20, 2014 at 4:12

3 Answers 3

1

You should replace this

 Db h = new Db(getApplicationContext());

With

 Db h = new Db(youractivity.this);

Pass activity context instead of application context.

Sign up to request clarification or add additional context in comments.

4 Comments

@Somasundaram post u r DB class
@M D ok I do...public class Db extends SQLiteOpenHelper{ Context context; public static String databasename="kotonew.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,databasename, null, SCHEMA_VERSION); this.context=context; } @Override public void onCreate(SQLiteDatabase db) { String query="create table category_table(Id integer primary key ,Name text)"; db.execSQL(query); } }
@Somasundaram post this into u r question
@Somasundaram also add db.execSQL("DROP TABLE IF EXISTS category_table"); onCreate(db); into onUpgrade() in Db
0

When you are initializing your Db object using getApplicationContext() in the following line...its not getting any Context...

    Db h = new Db(getApplicationContext());

To solve your Context problem you can pass the context of your activity through the AsyncTask() constructor. I'm giving you a scenario...

Suppose, from MainActivity.java you are executing your ProgressTask.java then you should pass the Context as follows....

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

In your ProgressTask.java class, you can retrieve that Context from ProgressTask() contructor as follows....

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

    Context mContext;

    ProgressTask (Context context) {

        mContext.this = context

    }

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

    }

    .........

}

Now, you can initialize your Db object as follows...

Db h = new Db(mContext);

This will solve your problem.

1 Comment

Can you please post your whole AsyncTask class...if you ahev no problem?
0

avoid using getApplicationContext() and use YourPresentActivityClass.this instead of getApplicationContext() and make sure that you are not passing null or check them carefully before using.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.