title: Create new SharedPreferences by onClick
body: <p>I'm new to Android Studio, please anyone can assist me with this problem?</p>
  <p>I want to create new sharedpref when user have done field in the required field.</p>
  <p>When he click the confirm button, automatically will be create new with new Name that with automatically Incremental.</p>
  <p>Edit :
  Here is my code, dunno if it's true by logical programming or not.
  Im using fragment to add the input the required field and back to mainactivity to show the the timer list by using recyclerview.</p>
  <p><strong>AddNewTimerFragment.java</strong></p>
  <pre><code>private SharedPreferences mPreferences;
      private String sharedPrefFile =
              &quot;com.mobprog.ius.dwasu&quot;;
  
  int position = 1;
  
  meditConfirm_button = rootView.findViewById(R.id.editConfirm_button);
          meditConfirm_button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  String.valueOf(startHour);
                  String.valueOf(endHour);
                  String.valueOf(R.array.valueListTimer);
                  position++;
                  newSharedPref(startHour, endHour, R.array.valueListTimer, position);
                  new UploadAlarmDataToServer().execute();
              }
          });
  
  public void newSharedPref(int toStoreStartHour, int toStoreEndHour, int toStoreInterval, int toStorePosition){
  
          SharedPreferences alarmPref = getActivity().getSharedPreferences(&quot;alarmContainer&quot;, MODE_PRIVATE);
          SharedPreferences.Editor preferencesEditor = mPreferences.edit();
          preferencesEditor.putInt(&quot;storedStartHour&quot;, toStoreStartHour);
          preferencesEditor.putInt(&quot;storedEndHour&quot;, toStoreEndHour);
          preferencesEditor.putInt(&quot;storedInterval&quot;, toStoreInterval);
          preferencesEditor.putInt(&quot;storedPosition&quot;, toStorePosition);
          preferencesEditor.apply();
          preferencesEditor.commit();
  
      }
  </code></pre>
  <p><strong>MainActivity.java</strong></p>
  <pre><code>int count = 0;
  
  protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          SharedPreferences countpreferences = getApplicationContext().getSharedPreferences(&quot;Position&quot;, MODE_PRIVATE);
          SharedPreferences.Editor countpreferencesEditor = countpreferences.edit();
  
  // Create recycler view.
          mRecyclerView = findViewById(R.id.recyclerView);
          // Create an adapter and supply the data to be displayed.
          mAdapter = new alarmListAdapter(this, mintervalTimeList);
          // Connect the adapter with the recycler view.
          mRecyclerView.setAdapter(mAdapter);
          // Give the recycler view a default layout manager.
          mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  
          countpreferencesEditor.putInt(&quot;count&quot;, 0);
  
  count = countpreferences.getInt(&quot;count&quot;, 0);
          if(count &gt; 0){
              for(int i = 0; i &lt; count; i++){
                  mintervalTimeList.addLast(&quot;Interval &quot; + getSharedPreferences(&quot;alarmContainer&quot;,MODE_PRIVATE).contains(&quot;storedPosition&quot;));
              }
          }
  }
  </code></pre>
  <p><strong>Logcat</strong></p>
  <pre><code>Process: com.mobprog.ius.dwasu, PID: 4774
      java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
          at com.mobprog.ius.dwasu.AddNewAlarmFragment.newSharedPref(AddNewAlarmFragment.java:266)
          at com.mobprog.ius.dwasu.AddNewAlarmFragment$5.onClick(AddNewAlarmFragment.java:176)
  </code></pre>
  <p><em>The code I showed is only the important, not the full that aren't affected by the problem I faced</em></p>