Jump to content

Null pointer with ArrayList?


Recommended Posts

Posted (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 by Paradox68
Posted

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...