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)
You should replace this
Db h = new Db(getApplicationContext());
With
Db h = new Db(youractivity.this);
Pass activity context instead of application context.
避免使用getApplicationContext()
并使用YourPresentActivityClass.this
而不是getApplicationContext()
,并确保在使用之前没有传递null或仔细检查它们。
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.