Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear the hash map<string,string> values?

I want to clear all the hash map values before the values are assigned to hash map from db.For eg.,hashmap<string,string> demo is my hashmap in that i have added the following values from db.The result response from hashmap is {A=1, B=2, C=3, D=4, E=5}.After view loads I want to insert this in above 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 have tried by using demo.clear() it is not working.How can I clear the 1st list of values before adding the 2nd one.Suggest some solutions to solve the problem.

This is the code to retrieve 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;

}

In this demo only the response coming as I explained above.And then am doing this

if(demo.isEmpty())
        {
//code for correct condition
        }
else
    {
     demo.clear();
    }

And passing the values to hash map by 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)
like image 888
Sabari Karthik Avatar asked Oct 14 '25 02:10

Sabari Karthik


1 Answers

hMap.clear() always worked fine for me.

Still

hMap.put("A","test");

will automatically override the existing value for key "A".

If you still wish to clear values,

You can use

hMap = new HashMap<String,String>();

which will initialize a new hashmap in hMap.

like image 101
MysticMagicϡ Avatar answered Oct 17 '25 14:10

MysticMagicϡ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!