title: Error when trying to save data
body: <p>The app runs fine at the first run after deleting previous data. It crashes when it runs second time and has to take saved info from sharedprefs (I saved it with HashSets). </p>
  
  <pre><code>SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.adam.vaistai", Context.MODE_PRIVATE);
  
  HashSet&lt;String&gt; set = (HashSet&lt;String&gt;) sharedPreferences.getStringSet("names", null);
  
  HashSet&lt;String&gt; numberSet = (HashSet&lt;String&gt;) sharedPreferences.getStringSet("numbers", null);
  
  HashSet&lt;String&gt; descSet = (HashSet&lt;String&gt;) sharedPreferences.getStringSet("desc", null);
  
  if (set == null){
      vaistai.add("Pavyzdys 2017-08-18");
      numbers.add(String.valueOf(0));
      description.add("Nuo persalimo, vartoti 3 kartus dienoje");
  
  } else {
  
      vaistai = new ArrayList&lt;&gt;(set);
      //error
      numbers = new ArrayList&lt;&gt;(numberSet);
      description = new ArrayList&lt;&gt;(descSet);
  
  }
  </code></pre>
  
  <p>Using logs I found out that the app crashes after line (<strong>vaistai = new ArrayList&lt;>(set);</strong>)</p>
  
  <pre><code>08-26 17:11:00.745 7730-7730/com.example.adam.vaistai E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.adam.vaistai, PID: 7730
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adam.vaistai/com.example.adam.vaistai.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
  </code></pre>
  
  <p>logcat shows this</p>
  
  <p>Thats the code of MainActivity.java</p>
  
  <pre><code>package com.example.adam.vaistai;
  
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteStatement;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.HashSet;
  import java.util.Set;
  
  public class MainActivity extends AppCompatActivity {
  
  ListView listView;
  
  static ArrayList&lt;String&gt; vaistai = new ArrayList&lt;&gt;();
  static ArrayList&lt;String&gt; numbers = new ArrayList&lt;&gt;();
  static ArrayList&lt;String&gt; description = new ArrayList&lt;&gt;();
  
  static ArrayAdapter arrayAdapter;
  
  
  public void nextActivity (int pos){
  
      Intent intent = new Intent(this, SecondActivity.class);
  
      intent.putExtra("index", pos);
  
      startActivity(intent);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      MenuInflater menuInflater = getMenuInflater();
  
      menuInflater.inflate(R.menu.main_menu, menu);
  
      return super.onCreateOptionsMenu(menu);
  
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      super.onOptionsItemSelected(item);
  
      if (item.getItemId() == R.id.newNote){
  
          vaistai.add("");
          numbers.add(String.valueOf(0));
          description.add("");
  
          arrayAdapter.notifyDataSetChanged();
  
          nextActivity(vaistai.size()-1);
      }
      return false;
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      listView = (ListView) findViewById(R.id.listView);
  
      SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.adam.vaistai", Context.MODE_PRIVATE);
  
      HashSet&lt;String&gt; set = (HashSet&lt;String&gt;) sharedPreferences.getStringSet("names", null);
  
      HashSet&lt;String&gt; numberSet = (HashSet&lt;String&gt;) sharedPreferences.getStringSet("numbers", null);
  
      HashSet&lt;String&gt; descSet = (HashSet&lt;String&gt;) sharedPreferences.getStringSet("desc", null);
  
      if (set == null){
          vaistai.add("Pavyzdys 2017-08-18");
          numbers.add(String.valueOf(0));
          description.add("Nuo persalimo, vartoti 3 kartus dienoje");
  
      } else {
  
          vaistai = new ArrayList&lt;&gt;(set);
          numbers = new ArrayList&lt;&gt;(numberSet);
          description = new ArrayList&lt;&gt;(descSet);
  
      }
  
      arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, vaistai);
  
      listView.setAdapter(arrayAdapter);
  
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  
              nextActivity(i);
  
          }
      });
  
      listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  
              final int item = i;
  
              new AlertDialog.Builder(MainActivity.this)
                      .setIcon(android.R.drawable.alert_dark_frame)
                      .setTitle("Ar norite istrinti?")
                      .setMessage("Irasas bus pasalintas")
                      .setPositiveButton("Taip", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
  
                              vaistai.remove(item);
                              numbers.remove(item);
                              description.remove(item);
                              arrayAdapter.notifyDataSetChanged();
  
                              SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("com.example.adam.vaistai", Context.MODE_PRIVATE);
  
                              HashSet&lt;String&gt; numberSet = new HashSet&lt;String&gt;(MainActivity.numbers);
  
                              sharedPreferences.edit().putStringSet("numbers", numberSet).apply();
  
                              HashSet&lt;String&gt; set = new HashSet&lt;String&gt;(MainActivity.vaistai);
  
                              sharedPreferences.edit().putStringSet("names", set).apply();
  
                              HashSet&lt;String&gt; descSet = new HashSet&lt;String&gt;(MainActivity.description);
  
                              sharedPreferences.edit().putStringSet("desc", descSet).apply();
                          }
                      }
                      )
                      .setNegativeButton("Ne", null)
                      .show();
  
              return true;
          }
      });
  
  }
  </code></pre>
  
  <p>}</p>