Jump to content

Null pointer with ArrayList?


Paradox68

Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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