Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DeNiS_M

Thread: I keep getting NullPointerException

  1. #1
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Post 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I keep getting NullPointerException

    Quote Originally Posted by coasterguy10 View Post
    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.
    ...
    It would be right sporting of you to show us which line is "line 67", the one that is throwing the NPE.

  3. #3
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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.

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: I keep getting NullPointerException

    Quote Originally Posted by coasterguy10 View Post
    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.
    I have a feeling that the null is in list[j - 1].
    try checking if list[j - 1] != null.

  5. #5
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I keep getting NullPointerException

    Quote Originally Posted by PhQ View Post
    I have a feeling that the null is in list[j - 1].
    try checking if list[j - 1] != null.
    Ok I tried that, but I still get ther error. I added an if loop that would stop the sort if it were null, and then went back and made the if statement to stop the sort if it was not null. They both compiled but when I tried to run the driver, I still got the same error.

  6. #6
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default 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!

    I am sorry about the formatting, I don't know how to change it
    Suround you code with code tags!
    Last edited by DeNiS_M; 09-19-2011 at 05:32 PM.
    Fubarable likes this.

  7. #7
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: I keep getting NullPointerException

    That fixed the problem. Thanks so much, now I can actually move on with this project.

Similar Threads

  1. NullPointerException
    By jayragz in forum NetBeans
    Replies: 5
    Last Post: 05-12-2011, 05:19 PM
  2. Nullpointerexception?
    By Pulgo in forum New To Java
    Replies: 5
    Last Post: 01-05-2011, 04:05 PM
  3. NullPointerException
    By s0meb0dy in forum New To Java
    Replies: 3
    Last Post: 10-09-2010, 08:12 PM
  4. NullPointerException
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 02-11-2010, 05:43 PM
  5. NullPointerException
    By JohnST in forum New To Java
    Replies: 13
    Last Post: 01-06-2010, 12:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •