-1

I tried to change a SharedPreference that is set up in my MainActivity in my Adapter. So I thought with an instance of the MainActivity I could access and change it from there. When an item is clicked a dialog pops up and then when it is confirmed, the variable shall be stored in the SharedPreference. Unfortunately I get an error in the part where this variable should be edited:

android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference

The MainActivity:

public class MainActivity extends AppCompatActivity {

    SharedPreferences favE;
    int x;

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

        favE = MainActivity.this.getSharedPreferences("myFavEName",
                Context.MODE_PRIVATE);
        x = favE.getInt("favKey", 0);

    }

    public int getFavE(){
        return x;
    }
}

My Adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;

    List<GetDataAdapter> getDataAdapter;

    MainActivity ma = new MainActivity();

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

        final int a = getDataAdapter1.getId();

        int x = ma.getFavE();

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 addDialog(a);       
        });
    }


    public void addDialog(final int a) {
        Context context = this.context;
        LayoutInflater inflater = LayoutInflater.from(this.context);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });

        builder.setPositiveButton("Exit Group", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {          
                ma.favE.edit().putInt("favKey", 0)
                        .apply();
            }
        });
        builder.show();
    }
}
Alperen Kantarcı
  • 1,038
  • 9
  • 27
user9155899
  • 19
  • 1
  • 9
  • 1
    the `MainActivity` instance in your adapter is not the same instance that instantiated the SharedPreferences... – Shark Sep 18 '18 at 13:51
  • @Shark Would you explain that to me? – user9155899 Sep 18 '18 at 13:58
  • 1
    Yes. The first snippet you posted, you instantiate (initialize) SharedPreferences member `favE` in it's `onCreate()` method. In the second snippet you posted, you create a new `MainActivity` instance by doing `MainActivity ma = new MainActivity();` - it didn't run it's `onCreate()` method since the activity hasn't been started by the OS. And thus, it's `favE` member is still uninitialized (null). That's why you're getting a NPE. – Shark Sep 18 '18 at 14:00
  • 1
    if you really want to do it like this, just make `favE` static in `MainActivity` - but you should also know that simple `SharedPrefUtil` class can work in both of those and avoid all of these issues you're having – Shark Sep 18 '18 at 14:01
  • 1
    if you do make it `static` a simple `MainActivity.getSharedPref()` does the trick and you don't need to instantiate the activity from the adapter. might be better to just pass the `MainActivity` instance to the adapter instead of instantiating a new one there. – Shark Sep 18 '18 at 14:03
  • Thank you for your help, I was able to solve it with both suggested solutions – user9155899 Sep 18 '18 at 14:27

1 Answers1

3

You don't need an instance of the MainActivity. You can specify a constructor that will take only context from the activity to use Shared preference. So when you create the adapter you will give the MainActivity context to the adapter. I think that in your case since you are creating the new instance, you are using the context before the MainActivity correctly initialized. So it gives you wrong context. Your constructor will be like this

public RecyclerViewAdapter(Context context) {
        this.context = context;
    }
Alperen Kantarcı
  • 1,038
  • 9
  • 27