October 14, 201510 yr 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, 201510 yr by Paradox68
October 14, 201510 yr 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
October 15, 201510 yr Author 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.
October 15, 201510 yr Post where the lines you update the allTimes reference. You are assigning it null somewhere
October 15, 201510 yr public ArrayList<Integer> allTimes = new ArrayList<Integer>(); otherwise just make sure you actually set it (if you are setting it to null)
October 15, 201510 yr 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
October 15, 201510 yr Author 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.
Create an account or sign in to comment