How do I clear the hashmap of <string, string> values?
I want to clear all hashmap values before values are assigned to hashmap from db. For example hashmap<string,string> demo
- this is my hashmap where I added the following values from db. Answer to result from hashmap {A=1, B=2, C=3, D=4, E=5}
. After uploads, I want to insert this into hashmap {A=test, B=demo, C=sample, D=empty, E=null}
. For this, how can I clear the first values before inserting the second set of values. And also I tried to use it demo.clear()
, it doesn't work. How can I clear the 1st list of values before adding the second. Try some solutions to solve the problem.
This is the code to fetch data from db.
public HashMap<String, String> getdata() {
try {
HashMap<String, String> map = new HashMap<String, String>();
Log.e("getdata", "Started");
String selectQuery = "SELECT * FROM tablename";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
map.put(cursor.getString(cursor.getColumnIndex("colmn1")),
cursor.getString(cursor.getColumnIndex("colmn2")));
} while (cursor.moveToNext());
}
cursor.close();
return map;
} catch (Exception e) {
e.printStackTrace();
Log.e("getdata", "Ended");
}
return null;
}
This demo only contains the answer as I explained above. And then I do this
if(demo.isEmpty())
{
//code for correct condition
}
else
{
demo.clear();
}
And passing the values to the hashmap this way.
demo = dbhelper.getdata();
And My Logcat exceptions:
09-06 12:48:53.656: W/dalvikvm(5071): threadid=1: thread exiting with uncaught exception (group=0x40fb0258)
09-06 12:48:53.660: W/System.err(5071): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename/packagename.listdata}: java.lang.NullPointerException
09-06 12:48:53.660: W/System.err(5071): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
09-06 12:48:53.660: W/System.err(5071): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
09-06 12:48:53.660: W/System.err(5071): at android.app.ActivityThread.access$600(ActivityThread.java:134)
09-06 12:48:53.660: W/System.err(5071): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
09-06 12:48:53.660: W/System.err(5071): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 12:48:53.660: W/System.err(5071): at android.os.Looper.loop(Looper.java:154)
09-06 12:48:53.660: W/System.err(5071): at android.app.ActivityThread.main(ActivityThread.java:4624)
09-06 12:48:53.661: W/System.err(5071): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 12:48:53.661: W/System.err(5071): at java.lang.reflect.Method.invoke(Method.java:511)
09-06 12:48:53.661: W/System.err(5071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-06 12:48:53.661: W/System.err(5071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-06 12:48:53.661: W/System.err(5071): at dalvik.system.NativeStart.main(Native Method)
09-06 12:48:53.661: W/System.err(5071): Caused by: java.lang.NullPointerException
09-06 12:48:53.661: W/System.err(5071): at packagename.listdata.onCreate(listdata.java:231)
09-06 12:48:53.661: W/System.err(5071): at android.app.Activity.performCreate(Activity.java:4479)
09-06 12:48:53.661: W/System.err(5071): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
09-06 12:48:53.661: W/System.err(5071): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
source to share