-2

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){
th3ramr0d
  • 484
  • 1
  • 7
  • 23
  • 1
    You need to learn some java's basics first... Fx How to compare strings – Selvin Feb 26 '17 at 23:05
  • 1
    @Selvin I'm not sure that's a good target. OP clearly has a null variable somewhere, but we have no [mcve] – OneCricketeer Feb 26 '17 at 23:12
  • subjects is null because it's never initialized because lack of java's basics(string comparison) – Selvin Feb 26 '17 at 23:14
  • 1
    @Selvin Why is it not initialized? `subjects = gson.fromJson(jsonString, type);` – OneCricketeer Feb 26 '17 at 23:15
  • 1
    Because `subjects = new ArrayList<>();` is inside if... Rest is unrelated... And fixing comparison would fix npe... But not the whole code – Selvin Feb 26 '17 at 23:17
  • @Selvin Please read the post again. 1) `private ArrayList subjects = new ArrayList<>()`... So, the field is not null. 2) The `subjects = gson.fromJson` is not in an `if`. But that *could return null* – OneCricketeer Feb 26 '17 at 23:19
  • @th3ramr0d Please `Log.d("JSON", jsonString);` before the `gson.fromJson` and comment out the rest of the code... – OneCricketeer Feb 26 '17 at 23:23

2 Answers2

0

I think your problem is here:

if(jsonString == ""){
    subjects = new ArrayList<>();
    for(int i = 0; 34 > i; i++){
        subjects.add(0);
    }
}

You are initializing the ArrayList again. You don't need this line subjects = new ArrayList<>();, as you already declared the ArrayList with private ArrayList<Integer> subjects = new ArrayList<>();. So you should be able to do:

//You also need to use .equals() method to compare strings
if(jsonString.equals("")){
    for(int i = 0; 34 > i; i++){
        subjects.add(0);
    }
}

However, without seeing the code in its entirety, it's hard to say.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

You are calling the method get in a null object. In your question, you don't specify at which point you run that code, but you either forgot to initialize the ArrayList subjects before using it, or you assigned a null object to that variable.

João Miranda
  • 53
  • 1
  • 6