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!
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;
}
}
}
Re: How do I get past this NullPointerException?
Quote:
if (list[i].getTemp() > largeElement)
Is the i element in the list array null?
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)
Re: How do I get past this NullPointerException?
Quote:
Originally Posted by
Norm
Is the i element in the list array null?
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)
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
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.
Re: How do I get past this NullPointerException?
Never mind, I fixed it. Thanks for all your help!