Sharedpreferences receiving Nullpointerexception

Asked

Viewed 36 times

0

I need some help with Sharedpreference. I am developing a phrase APP, where there is a checkbox that marks favorite phrases and plays these phrases in Sharedpreferences where they can be accessed from a button called favorites.

The problem is that if we click on favorites without having any favorite in which it is triggered when checking the checkbox, it closes the app and the log displays the following message:

java.lang.Nullpointerexception: Attempt to invoke interface method 'int java.util.List.size()' on a null Object Reference

I would like to circumvent this situation, even if possible leave a default value in sharedpreference if it is the case or alternative.

Follow my code if you can help me...

// EVENTO DE CLIQUE

           lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {


                   // CHECK BOX

                   View checkBoxView = View.inflate(FraseAutorFrase.this, R.layout.checkbox, null);

                   CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
                   checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                       @Override
                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                           // Save to shared preferences

                           SharedPreferences sharedPreferences = getSharedPreferences("arquivoPreferencia",MODE_PRIVATE);
                           SharedPreferences.Editor editor = sharedPreferences.edit();
                           editor.putString("frase" + position, frasesR[position]);
                           editor.commit();


                       }
                   });
                   checkBox.setText("Marcar como Favorito?");

This information is passed to another Activity

* ACTIVITY FAVORITOS

final SharedPreferences sharedPreferences = getSharedPreferences("arquivoPreferencia", MODE_PRIVATE);

        // CRIANDO A LISTA PARA RECEBER OS FAVORITOS

        final List<String> frasesFavoritoArray = new ArrayList<>();

        // TRANSFORMANDO ARRAY EM STRING

        Object[] objectList = frasesFavoritoArray.toArray();
        final String[] stringArray = Arrays.copyOf(objectList, objectList.length, String[].class);

        // CAPTURANDO TODOS OS REGISTROS DO SHARED PREFERENCE

        Map<String, ?> allEntries = sharedPreferences.getAll();
              for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                frasesFavoritoArray.add(entry.getKey().toString());

                frasesFavoritos = frasesFavoritoArray;

            }
  • The error happens in the code you posted?

  • ramaral she is passed to another Activity, reedited the post now. Sorry.

  • Have you ever debugged to see which line it hangs on? but to avoid npe you should check if it is null before setting in the view, by error it is just that, trying to run a method on a null object.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.