Paradox68 Posted October 14, 2015 Share Posted October 14, 2015 (edited) Declaring the ArrayList public ArrayList<Integer> allTimes; One function for array: if (!allTimes.isEmpty() && allTimes != null) { allTimes.clear(); } Second function: tripE = System.currentTimeMillis() - tripS; if (allTimes != null) { allTimes.add((int)tripE); } Calculating average times. int averageTime = 0; if (allTimes.toArray().length > 3) { int total = 0; for (int i = 0; i < allTimes.toArray().length; i++) { total += allTimes.get(i).intValue(); } averageTime = (total / allTimes.toArray().length); } I don't know if it's the only one but the Nullpointer is thrown on allTimes.clear(); Edited October 14, 2015 by Paradox68 Quote Link to comment Share on other sites More sharing options...
Precise Posted October 14, 2015 Share Posted October 14, 2015 Declaring the ArrayList public ArrayList<Integer> allTimes; One function for array: if (!allTimes.isEmpty() && allTimes != null) { allTimes.clear(); } Second function: tripE = System.currentTimeMillis() - tripS; if (allTimes != null) { allTimes.add((int)tripE); } Calculating average times. int averageTime = 0; if (allTimes.toArray().length > 3) { int total = 0; for (int i = 0; i < allTimes.toArray().length; i++) { total += allTimes.get(i).intValue(); } averageTime = (total / allTimes.toArray().length); } I don't know if it's the only one but the Nullpointer is thrown on allTimes.clear(); should null check before checking if isnt empty Quote Link to comment Share on other sites More sharing options...
Paradox68 Posted October 15, 2015 Author Share Posted October 15, 2015 should null check before checking if isnt empty So the null check works, but why is it throwing a null? Now the code just isn't being called. Quote Link to comment Share on other sites More sharing options...
Ziy Posted October 15, 2015 Share Posted October 15, 2015 Post where the lines you update the allTimes reference. You are assigning it null somewhere Quote Link to comment Share on other sites More sharing options...
Czar Posted October 15, 2015 Share Posted October 15, 2015 public ArrayList<Integer> allTimes = new ArrayList<Integer>(); otherwise just make sure you actually set it (if you are setting it to null) Quote Link to comment Share on other sites More sharing options...
Mysteryy Posted October 15, 2015 Share Posted October 15, 2015 So the null check works, but why is it throwing a null? Now the code just isn't being called. You need to initialize the array list. Right now, you are declaring an arraylist but not initializing it. ArrayList blah = new ArrayList Quote Link to comment Share on other sites More sharing options...
Paradox68 Posted October 15, 2015 Author Share Posted October 15, 2015 Thanks guys, I forgot to post on the thread when I corrected it but Czar was right. I had to assign an initial value of an ArrayList. Quote Link to comment Share on other sites More sharing options...