Results 1 to 7 of 7
- 09-19-2011, 04:15 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
I keep getting NullPointerException
I am trying to make a program that will read in Weather objects and create a WeatherList. However, when I try to sort the list, I get a NullPointerException for line 67 in the WeatherClass class. Any help would be appreciated. I am sorry about the formatting, I don't know how to change it. My data is at the bottom if anyone wants to run it.
public class Weather
{
private String condition;
private int temp;
private String date;
public Weather()
{
condition = "No condition entered";
temp = 0;
date = "No date entered";
}
public Weather(String cond, int t, String d)
{
condition = cond;
temp = t;
date = d;
}
//getters
public String getCondition()
{return condition;}
public int getTemp()
{return temp;}
public String getDate()
{return date;}
//setters
public void setCondition(String cond)
{condition = cond;}
public void setTemp(int t)
{temp = t;}
public void setDate(String d)
{date = d;}
public String toString()
{
return (condition+"\t\t"+temp+"\t\t"+date);
}
}
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 < list.length; 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
}
}
import java.util.Scanner;
import java.io.*;
public class WeatherDriver
{
public static void main (String[] args)throws IOException
{
WeatherList wl = new WeatherList();
String condition;
int temp;
String date;
String c = "";
Scanner scan = new Scanner(new File("weatherinput.txt"));
while (scan.hasNext())
{
c = scan.nextLine();
String [] data = c.split(" ");
condition = data[0];
temp = Integer.parseInt(data[1]);
date = data[2];
wl.addToList(condition, temp, date);
}
System.out.println(wl);
wl.getTempAvg();
System.out.println("The average temperature is "+wl.getTempAvg());
wl.conditionSort();
System.out.println(wl);
}
}
Damp 60 12/4/09
Balmy 70 05/15/08
Rainy 65 04/21/07
Pleasant 75 06/17/10
Stormy 50 02/01/10
Sunny 78 03/29/10
Rainy 68 04/15/10
Miserable 35 01/20/09
Clear 78 09/16/10
Sunny 95 08/19/10
Foggy 50 11/20/09
-
Re: I keep getting NullPointerException
- 09-19-2011, 04:05 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: I keep getting NullPointerException
Sorry, the line that is throwing is
while ( j != 0 && ( temp.getCondition( )
).compareTo(list[j - 1].getCondition( ) ) < 0 )
And if anyone could tell me how to properly format my code for any future posts that would be great also, thanks.
- 09-19-2011, 04:31 PM #4
- 09-19-2011, 04:40 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: I keep getting NullPointerException
- 09-19-2011, 05:21 PM #6
Member
- Join Date
- Sep 2011
- Location
- Athens Greece
- Posts
- 29
- Rep Power
- 0
Re: I keep getting NullPointerException
I think the proplem is in the for loop of the sorting algorithm
You should limit the iteration to listTotal.
Because the list.length will give you the length of the list but that doesn't mean that your list
actually has as much objects in it!
Suround you code with code tags!I am sorry about the formatting, I don't know how to change itLast edited by DeNiS_M; 09-19-2011 at 05:32 PM.
- 09-19-2011, 05:53 PM #7
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
NullPointerException
By jayragz in forum NetBeansReplies: 5Last Post: 05-12-2011, 05:19 PM -
Nullpointerexception?
By Pulgo in forum New To JavaReplies: 5Last Post: 01-05-2011, 04:05 PM -
NullPointerException
By s0meb0dy in forum New To JavaReplies: 3Last Post: 10-09-2010, 08:12 PM -
NullPointerException
By Juuno in forum New To JavaReplies: 1Last Post: 02-11-2010, 05:43 PM -
NullPointerException
By JohnST in forum New To JavaReplies: 13Last Post: 01-06-2010, 12:37 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks