0

quick summary of what I'm trying to do.

Using shared preferences so that when a user watches a video a green tick will appear beside it to mark it as complete. Currently getting a null pointer error.

Basically there are two pages - main page and video page. When user lands on the main page the if statement should check the sharepreferences to see if they have already visited the video page. If they have it should display the completedTick imageView.

Please see the code and context below.

MAIN PAGE CODE SNIPPET:

 /**
 * Shared preferences variable which is used to store the userId which is captured at the login screen.
 */
private SharedPreferences loginPref, activityPref;   
private ImageView completedTick;
private Boolean activityCompleted;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
    activityPref = getContext().getSharedPreferences("activityPref", Context.MODE_PRIVATE);
    activityCompleted = activityPref.getBoolean("activityCompleted", true);

    View view = inflater.inflate(R.layout.fragment_activities, container, false);

    initialiseRecyclerView(view);
    retrieveActivities();

    if(activityCompleted == false || activityCompleted == null ) {
        completedTick.setVisibility(view.INVISIBLE);
    }else{
        completedTick.setVisibility(View.VISIBLE);
    }

    return view;

}

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

/**
 * Method which is used to initialise the recycler view
 *
 * @param view
 */
private void initialiseRecyclerView(View view) {
    mRecyclerView = view.findViewById(R.id.recyclerViewActivities);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    EmptyAdapter emptyAdapter = new EmptyAdapter();
    mRecyclerView.setAdapter(emptyAdapter);
    completedTick = view.findViewById(R.id.completedTick);
}

VIDEO PAGE CODE SNIPPET:

private SharedPreferences activityPref;
private Boolean activityCompleted;


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

    Log.d(TAG, "onCreateView: Starting");
    activityPref = getSharedPreferences("activityPref", MODE_PRIVATE);
    activityCompleted = activityPref.getBoolean("activityCompleted", true);

        getExtrasMethod();
        initialiseViews();
        setTextInViews();
        checkCompleteD();


 private void checkCompleteD() {

    if (activityCompleted) {
        SharedPreferences.Editor editor = activityPref.edit();
        activityCompleted = false;
        editor.putBoolean("activityCompleted", activityCompleted);
        editor.apply();

    }
}

The error is :

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
        at com.example.fit.ActivitiesFragment.onCreateView(ActivitiesFragment.java:78)

UPDATE:

Made the suggedted changes

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
    activityPref = getContext().getSharedPreferences("activityPref", Context.MODE_PRIVATE);
    activityCompleted = activityPref.getBoolean("activityCompleted", true);

    completedTick = view.findViewById(R.id.completedTickImage);
    initialiseRecyclerView(view);
    retrieveActivities();

    if (activityCompleted == false || activityCompleted == null) {
        completedTick.setVisibility(View.INVISIBLE);
    } else {
        completedTick.setVisibility(View.VISIBLE);
    }
}

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference at com.example.feelingfit.ActivitiesFragment.onViewCreated(ActivitiesFragment.java:85)

Which is this line : completedTick.setVisibility(View.INVISIBLE);

2 Answers2

5

First of all, your completedTick is null. Try putting the completedTick = view.findViewById(R.id.completedTick); in your OnViewCreated above this code:

if(activityCompleted == false || activityCompleted == null ) {
        completedTick.setVisibility(View.INVISIBLE);
    }else{
        completedTick.setVisibility(View.VISIBLE);
    }

And also, replace the view.INVISIBLE with View.INVISIBLE

Edit + Aditional Information:

If you occur again the NullPointerException please refer to this link.

Edit 2: Rewrite your code inside the onViewCreated method like this:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_activities, container, false);

}

@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
 loginPref = getContext().getSharedPreferences("loginPref", Context.MODE_PRIVATE);
        activityPref = getContext().getSharedPreferences("activityPref", Context.MODE_PRIVATE);
        activityCompleted = activityPref.getBoolean("activityCompleted", true);

        initialiseRecyclerView(view);
        retrieveActivities();

        if(activityCompleted == false || activityCompleted == null ) {
            completedTick.setVisibility(View.INVISIBLE);
        }else{
            completedTick.setVisibility(View.VISIBLE);
        }
}

Edit number 3: Since it is crashing again on the same use case I assume your R.id.completedTickImage is not inside your fragment_activities

coroutineDispatcher
  • 5,134
  • 2
  • 16
  • 50
-1

Do the operation of CompleteTick.setVisibility inside onActivityCreated() method and also replace the view.INVISIBLE to View.INVISIBLE

coroutineDispatcher
  • 5,134
  • 2
  • 16
  • 50