android saving hashmap to SharedPreferences
<p>The <code>RecyclerView</code> in <strong>activity_main</strong> has three check boxes and at the bottom of <strong>activity_main</strong> is a button which when clicked  saves the <code>checkbox</code> status(<strong>true</strong> or <strong>false</strong>) and its adapter position to <code>hashmap</code>. Position is the key and status is the value.Both are converted to <code>String</code>.I want to save it to <code>sharedpreferences</code>.I found that primitive data types can be saved to <code>shared preferences</code>.Is there a  way to save <code>hashmap</code> to it. </p>

<p><strong>RecyclerAdapter.java:</strong></p>

<pre><code>public class RecyclerHolder extends RecyclerView.ViewHolder {
    AnimCheckBox checkBox;
    checkBox = (AnimCheckBox) itemView.findViewById(R.id.checkBox);
    checkBox.setChecked(false);
    public RecyclerHolder(final View itemView) {
        super(itemView);
        checkBox.setOnCheckedChangeListener(new AnimCheckBox.OnCheckedChangeListener() {
            @Override
            public void onChange(boolean checked) {
                checkboxclick = true;
                if (checkBox.isChecked()) {
                    boolean check = true;
                    clicked_position = getAdapterPosition();
                    String position = Integer.toString(clicked_position);
                    String check_status = Boolean.toString(check);
                    hashMap.put(position, check_status);
                } else {
                    String removekey = String.valueOf(getAdapterPosition());
                    if (hashMap.containsKey(removekey)) {
                        hashMap.remove(removekey);
                    }
                }
            }
        });

        SharedPreferences sharedPreferences = context.getSharedPreferences("Main", Context.MODE_PRIVATE);
        bt_click_status = sharedPreferences.getBoolean("BtnCheck", false);

        if (hashMap != null &amp;&amp; bt_click_status == true) {

            if (checkboxclick == true) {
                SharedPreferences sharedPreferences1 = context.getSharedPreferences("ck_itm", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences1.edit();
                editor.put
            }
        }
    }
}
</code></pre>

<p>RecyclerAdapter(Edited).java:</p>

<pre><code>GsonBuilder builder = new GsonBuilder();
Gson gson = builder.enableComplexMapKeySerialization().setPrettyPrinting().create();
Type type = new TypeToken &lt; HashMap &lt; String, String &gt;&gt; () {}.getType();
String json = gson.toJson(hashMap, type);
SharedPreferences sharedPreferences1 = context.getSharedPreferences("Ctk", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences1.edit();
editor.putString("chk", json);
editor.commit();
</code></pre>

<p>MainActivity.java:</p>

<pre><code>SharedPreferences sharedPreferences1 = getApplicationContext().getSharedPreferences("Ctk", Context.MODE_PRIVATE);
String get = sharedPreferences1.getString("chk", null);
places = new Gson().fromJson(get, new TypeToken &lt; HashMap &lt; String, String &gt;&gt; () {}.getType());
s = places.keySet();
Iterator i = s.iterator();
while (i.hasNext()) {
    Map.Entry entry = (Map.Entry) i.next();
    Log.e("key:" + entry.getKey(), "value:" + entry.getValue());
}
</code></pre>

<p>Error:(Error is at "s=places.entrySet() in MainActivity.java)</p>

<pre><code>03 - 09 12: 51: 53.658 29260 - 29260 / com.example.jobinsabu.destination E / AndroidRuntime:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.jobinsabu.destination.MainActivity$1$1.onClick(MainActivity.java: 173)
at android.view.View.performClick(View.java: 4439)
at android.widget.Button.performClick(Button.java: 148)
at android.view.View$PerformClick.run(View.java: 18395)
at android.os.Handler.handleCallback(Handler.java: 725)
at android.os.Handler.dispatchMessage(Handler.java: 92)
at android.os.Looper.loop(Looper.java: 176)
at android.app.ActivityThread.main(ActivityThread.java: 5302)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java: 511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)
at dalvik.system.NativeStart.main(Native Method)
</code></pre>