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

    Post 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;
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: How do I get past this NullPointerException?

    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)

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

    Default Re: How do I get past this NullPointerException?

    Quote Originally Posted by Norm View Post
    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

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

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

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

    Default Re: How do I get past this NullPointerException?

    Never mind, I fixed it. Thanks for all your help!

Similar Threads

  1. Accessing past Histories
    By shakeel in forum Networking
    Replies: 1
    Last Post: 01-09-2011, 04:59 PM
  2. Cant get past 1st if statement in driver
    By erin.ctm in forum New To Java
    Replies: 5
    Last Post: 12-04-2010, 01:39 AM
  3. New To Java Ive Done Some Coding In The Past!
    By Noob Koer in forum New To Java
    Replies: 1
    Last Post: 07-08-2010, 07:46 AM
  4. [SOLVED] Cut,Past,copy in edit menu
    By smartsubroto in forum New To Java
    Replies: 9
    Last Post: 07-03-2008, 10:15 AM
  5. TextArea additable and uneditable (copy/past problem)
    By qwerty55 in forum Advanced Java
    Replies: 0
    Last Post: 01-19-2008, 11:41 PM

Posting Permissions

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