1

I have looked through all of my code, but I can't find any null references, so I am lost. I am saving certain contacts the the user collects into shared preferences:

   public void saveContacts() {

        contacts = this.getSharedPreferences("contacts", MainActivity.MODE_PRIVATE); //Making a shared preferences

        Set<String> set = new HashSet<String>();
        set.addAll(contactArrayList); 

        contactEditor.clear(); //Clearing current values in shared pref
        contactEditor.putStringSet("contactSetKey", set); //Adding contacts
        contactEditor.commit();

        Toast.makeText(MainActivity.this, "Contacts have been saved!", Toast.LENGTH_SHORT).show();
    }

Here is where I get the contacts:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        contactArrayList= new ArrayList<String>();
        listview = (ListView) findViewById(R.id.listView);

        arrayAdapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, contactArrayList);
        listview.setAdapter(arrayAdapter);

        loadSavedStuff();


    }

    public void loadSavedStuff() {
        //CLEAR CONTENTS OF ARRAYLIST AND GET THEM FROM SHARED PREFERENCE


        if(contacts.getStringSet("contactSetKey", null) != null) {
            contactArrayList.clear();
            contactArrayList = new ArrayList<String>(contacts.getStringSet("contactSetKey", null));
            arrayAdapter.notifyDataSetChanged();
        }
    }

I have been adding stuff to my list like so:

  contactArrayList.add(contactName +" - " +contactNumber);
            arrayAdapter.notifyDataSetChanged();

My null pointer exception is here:

  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Set android.content.SharedPreferences.getStringSet(java.lang.String, java.util.Set)' on a null object reference

Pointing here:

    if(contacts.getStringSet("contactSetKey", null) != null) {

and here:

    loadSavedStuff();
Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83

2 Answers2

4

You're checking only if getStringSet is not null, while contacts still can be null and that's why it throws you NullPointerException.

Change

if(contacts.getStringSet("contactSetKey", null) != null) {

to

if(contacts != null && (contacts.getStringSet("contactSetKey", null) != null)) {
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • Oh, so I am only checking whether the value I am getting is null, wheras I should be checking if the object in which I am calling the getter itself is null? – Ruchir Baronia Jan 30 '16 at 22:00
  • Yes, you should first check the object. If the object is `null` then automatically `if` statement becomes `false` and second condition is even not checked. In your code you assumed that `contacts` object exist (there is reference to it) and you tried to check if `getStringSet` is not returning `null`. – Damian Kozlak Jan 30 '16 at 22:02
  • @RuchirBaronia Can you edit your question and show more code? Especially parts where you **declare** and **initalize** your `contactEditor` and also `onPause()` function? – Damian Kozlak Jan 30 '16 at 22:11
  • Actually, I fixed my `contactEditor` issue, but I'm having a problem with the adapter now. I am getting a NullPointerException here: `listview.setAdapter(arrayAdapter);`. – Ruchir Baronia Jan 30 '16 at 22:16
  • [Let us continue this discussion in chat](http://chat.stackoverflow.com/rooms/102105/nullpointerexceptionissue) – Ruchir Baronia Jan 30 '16 at 22:16
1

Your contacts reference is null. You have not initialized before trying to call a method on it - the initialization is in saveContacts() but it's not called before the onCreate() and loadSavedStuff().

The init code you're missing:

contacts = this.getSharedPreferences("contacts", MainActivity.MODE_PRIVATE);
laalto
  • 150,114
  • 66
  • 286
  • 303
  • 1
    But I did this: if(contacts.getStringSet("contactSetKey", null) != null) { – Ruchir Baronia Jan 30 '16 at 21:59
  • Yes, you're invoking a method on `contacts` which is null. – laalto Jan 30 '16 at 22:00
  • Okay, so I have fixed that using what Damian suggested, but now I am getting null on `contactEditor.clear(); //Clearing current values in shared pref`, and on the call to `saveContacts()` which is in `onPause()`. How can I fix that? – Ruchir Baronia Jan 30 '16 at 22:02
  • http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – laalto Jan 30 '16 at 22:04