Fixed: Changed to .equals instead of using ==. Then I changed it to subjects.isEmpty() and it worked as well. For the record my question wasn't how do I compare strings. That just happened to be the problem I had with my code. Thanks to everyone for their replies!
I have an ArrayList that I am loading from sharedprefs as a string and converting it back to an ArrayList. When I load it I check to see if it's empty. If it's empty I populate it with 34 objects. Then I try to use the ArrayList but I'm getting a null object reference.
I declare my variable outside of onCreate
private ArrayList<Integer> subjects = new ArrayList<>();
When I run this code I get the error
private void setupSubjects(){
SharedPreferences sharedPrefs = this.getActivity().getSharedPreferences(viewPressed, Context.MODE_PRIVATE);
Gson gson = new Gson();
String jsonString = new String();
jsonString = sharedPrefs.getString("Subjects", jsonString);
Type type = new TypeToken<ArrayList<Integer>>() {}.getType();
subjects = gson.fromJson(jsonString, type);
if(jsonString == ""){
for(int i = 0; 33 < i; i++){
subjects.add(0);
}
}
if(subjects.get(0) == 1){
questionsString = getResources().getStringArray(R.array.question);
answersString = getResources().getStringArray(R.array.answer);
questionsLoaded = new ArrayList<>(Arrays.asList(questionsString));
answersLoaded = new ArrayList<>(Arrays.asList(answersString));
randomizeSubjects();
}
}
I am getting a java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference on this line
if(subjects.get(0) == 1){