Results 1 to 5 of 5
- 09-21-2011, 02:37 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
How do I get past this NullPointerException?
Below is a class I have written that will take Weather objects and put them into a list. I am trying to initiate a sort that will sort the temperatures of the array list in descending order. Everything compiled, but I keep getting the NullPointerExeption error at the line marked below and I can't figure out why. Also, I'm not sure if the sort is going to do what I want, so if you see that it won't do temperatures in descending order I would also appreciate if you'd let me know. Thanks!
Java Code:import java.text.NumberFormat; public class WeatherList { private Weather [] list = new Weather[5]; private int capacity; private int listTotal; private double tempTotal; public WeatherList() { capacity = 5; listTotal = 0; tempTotal = 0.0; } public void addToList(String cond, int t, String d) { if (listTotal < capacity) { Weather temp = new Weather(cond, t, d); list[listTotal] = temp; listTotal++; tempTotal+= t; if (listTotal==capacity) increaseSize(); } } public double getTempAvg() { double average = tempTotal/listTotal; return average; } private void increaseSize() { Weather [] temp = new Weather[list.length+3]; for ( int i = 0; i < list.length; i++ ) { temp[i] = list[i]; } list = temp; capacity += 3; } public String toString() { String contents = "\nConditions\tTemperature\tDate\n"; for (int i = 0; i < listTotal; i++) contents += list[i]+ "\n"; return contents; } //Sort alphabetically with selection sort public void conditionSort() { Weather temp; int j; for ( int i = 1; i < listTotal; i++ ) { j = i; temp = list[i]; while ( j != 0 && ( temp.getCondition( ) ).compareTo(list[j - 1].getCondition( ) ) < 0 ) { list[j] = list[j - 1]; j--; } // end while loop list[j] = temp; } // end for loop } public int conditionSearch( String cond ) { for ( int i = 0; i < listTotal; i++ ) { if ( list[i].getCondition() .equals(cond) ) return i; } return -1; // end of array reached without finding } public Weather getObj(int index) { return(list[index]); } public int indexOfLargestElement( Weather [] list, int listTotal) { int largeElement = list[0].getTemp(); int indexOfLargestElement = 0; for(int i = 0; i < listTotal; i++) { if (list[i].getTemp() > largeElement) //***This is the line that gets the error*** { largeElement = list[i].getTemp(); indexOfLargestElement = i; } } return indexOfLargestElement; } public void tempSort() { Weather temp;//temporary location for swap int max; //index of max value in subarray for ( int i = 0; i < listTotal - 1; i++ ) { max = indexOfLargestElement( list, listTotal - i ); // swap array[max] and array[array.length - i - 1] temp = list[max]; list[max] = list[list.length - i - 1]; list[list.length - i - 1] = temp; } } }
- 09-21-2011, 02:44 AM #2
Re: How do I get past this NullPointerException?
Is the i element in the list array null?if (list[i].getTemp() > largeElement)
If your array can have null elements in it, you need to test if the element is null before using its contents.
Perhaps:
if (list[i] != null && list[i].getTemp() > largeElement)
- 09-21-2011, 02:55 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: How do I get past this NullPointerException?
Ok I was able to get past the error, but now my output shows that some of my elements are now null... and my list was not sorted properly... AAGH. If anyone thinks they can help I'll take the advice.
Conditions Temperature Date
Miserable 35 01/20/09
null
null
Foggy 50 11/20/09
null
Sunny 78 03/29/10
Rainy 68 04/15/10
Stormy 50 02/01/10
Damp 60 12/4/09
Rainy 65 04/21/07
Balmy 70 05/15/08
- 09-21-2011, 03:06 AM #4
Re: How do I get past this NullPointerException?
You need to do some debugging.
If you don't have an interactive debugger, then you need to add println statements to the code to show the execution flow and the values of variables as they are used and changed.
For anyone to test your code they need a definition for the Weather class and driver code that loads the data.Last edited by Norm; 09-21-2011 at 03:08 AM.
- 09-21-2011, 03:09 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Accessing past Histories
By shakeel in forum NetworkingReplies: 1Last Post: 01-09-2011, 04:59 PM -
Cant get past 1st if statement in driver
By erin.ctm in forum New To JavaReplies: 5Last Post: 12-04-2010, 01:39 AM -
New To Java Ive Done Some Coding In The Past!
By Noob Koer in forum New To JavaReplies: 1Last Post: 07-08-2010, 07:46 AM -
[SOLVED] Cut,Past,copy in edit menu
By smartsubroto in forum New To JavaReplies: 9Last Post: 07-03-2008, 10:15 AM -
TextArea additable and uneditable (copy/past problem)
By qwerty55 in forum Advanced JavaReplies: 0Last Post: 01-19-2008, 11:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks