7 Replies - 1371 Views - Last Post: 13 February 2012 - 03:22 PM Rate Topic: -----

#1 trogan234   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-February 12

NullPointerException Problem

Posted 13 February 2012 - 10:43 AM

Im stuck on a nullpointerexception. It says "Exception in thread "main" java.land.NullPointerException
at CD.compareTo<CD.java:44>
at Sorts.insertionSort<Sorts.java:66>
at Tunes.main<Tunes.java:66>

I'm pretty sure I just need to change something small and it will work.
Thanks
public class Tunes
{
   //-----------------------------------------------------------------
   //  Creates a CDCollection object and adds some CDs to it. Prints
   //  reports on the status of the collection.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {

      CDCollection music = new CDCollection ();

      music.addCD ("Storm Front", "Billy Joel", 14.95, 10);
      music.addCD ("Come On Over", "Shania Twain", 14.95, 16);
      music.addCD ("Soundtrack", "Les Miserables", 17.95, 33);
      music.addCD ("Graceland", "Paul Simon", 13.90, 11);

      System.out.println (music);

      music.addCD ("Double Live", "Garth Brooks", 19.99, 26);
      music.addCD ("Greatest Hits", "Jimmy Buffet", 15.95, 13);

      Sorts.insertionSort (music.getCDS());

      System.out.println (music);
   }
}


public class Sorts
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (int[] numbers)
   {
      for (int index = 1; index < numbers.length; index++)
      {
         int key = numbers[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && numbers[position-1] > key)
         {
            numbers[position] = numbers[position-1];
            position--;
         }
            
         numbers[position] = key;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of objects using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (Comparable[] objects)
   {
      for (int index = 1; index < objects.length; index++)
      {
         Comparable key = objects[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && objects[position-1].compareTo(key) > 0)
         {
            objects[position] = objects[position-1];
            position--;
         }
            
         objects[position] = key;
      }
   }
}


import java.text.NumberFormat;

public class CD implements Comparable
{
   private String title, artist;
   private double cost;
   private int tracks;

   //-----------------------------------------------------------------
   //  Creates a new CD with the specified information.
   //-----------------------------------------------------------------
   public CD (String name, String singer, double price, int numTracks)
   {
      title = name;
      artist = singer;
      cost = price;
      tracks = numTracks;
   }

   //-----------------------------------------------------------------
   //  Returns a description of this CD.
   //-----------------------------------------------------------------
   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String description;

      description = fmt.format(cost) + "\t" + tracks + "\t";
      description += title + "\t" + artist;

      return description;
  }
  //------------------------------------------------------------------
  public int compareTo (Object other)
  {
	  int result;
	  if (title.equals(((CD)other).title))
	  result = artist.compareTo(((CD)other).artist);
	  else
	  result = title.compareTo(((CD)other).title);
	  return result;
   }
}


import java.text.NumberFormat;

public class CDCollection
{
   private CD[] collection;
   private int count;
   private double totalCost;

   //-----------------------------------------------------------------
   //  Creates an initially empty collection.
   //-----------------------------------------------------------------
   public CDCollection ()
   {
      collection = new CD[1];
      count = 0;
      totalCost = 0.0;
   }

   //-----------------------------------------------------------------
   //  Adds a CD to the collection, increasing the size of the
   //  collection if necessary.
   //-----------------------------------------------------------------
   public void addCD (String title, String artist, double cost,
                      int tracks)
   {
      if (count == collection.length)
         increaseSize();

      collection[count] = new CD (title, artist, cost, tracks);
      totalCost += cost;
      count++;
   }

   //-----------------------------------------------------------------
   //  Returns a report describing the CD collection.
   //-----------------------------------------------------------------
   public String toString()
   {
      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String report = "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n";
      report += "My CD Collection\n\n";

      report += "Number of CDs: " + count + "\n";
      report += "Total cost: " + fmt.format(totalCost) + "\n";
      report += "Average cost: " + fmt.format(totalCost/count);

      report += "\n\nCD List:\n\n";


      for (int cd = 0; cd < count; cd++)
         report += collection[cd].toString() + "\n";

      return report;
   }

   //-----------------------------------------------------------------
   //  Doubles the size of the collection by creating a larger array
   //  and copying the existing collection into it.
   //-----------------------------------------------------------------
   
   private void increaseSize ()
   {
      CD[] temp = new CD[collection.length * 2];

      for (int cd = 0; cd < collection.length; cd++)
         temp[cd] = collection[cd];

      collection = temp;
   }
   //------------------------------------------------------------------
   public CD[] getCDS()
   {
   return collection;
	}
}


Forgot: The code is supposed to alphabetize the cd's by title.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException Problem

#2 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: NullPointerException Problem

Posted 13 February 2012 - 10:50 AM

 if (title.equals(((CD)other).title))



The problem will be in this line. One of these objects is null. You can figure out which one by something simple like:

System.out.println("title is null? "+title==null + "other is null? " + other==null + 
     "other.title is null?" + other.title==null);



Put that line at the start of the compareTo method, and you'll see what object is causing the problem. Then you can trace back to figure out where you failed to initialize it, and you're sorted.
Was This Post Helpful? 0
  • +
  • -

#3 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1825
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: NullPointerException Problem

Posted 13 February 2012 - 10:51 AM

Null pointer exception means that you are referring to a null object.

The getCDS() methods returns collection which is not full, so it will have some null index which when passed to compareTo() throws exception because compareTo() does not allow null parameter
Was This Post Helpful? 0
  • +
  • -

#4 trogan234   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-February 12

Re: NullPointerException Problem

Posted 13 February 2012 - 01:01 PM

View Postjon.kiparsky, on 13 February 2012 - 10:50 AM, said:

 if (title.equals(((CD)other).title))



The problem will be in this line. One of these objects is null. You can figure out which one by something simple like:

System.out.println("title is null? "+title==null + "other is null? " + other==null + 
     "other.title is null?" + other.title==null);



Put that line at the start of the compareTo method, and you'll see what object is causing the problem. Then you can trace back to figure out where you failed to initialize it, and you're sorted.


Okay, I tried it and this is what I got:

CD.java:46: cannot find symbol
symbol : variable title
location: class java.lang.Object
"other.title is null?" + other.title==null);
^
C:\Users\TBeckham14\Downloads\Desktop\Sophomore\apcomputers\Chapter 6 Project\7\CD.java:46: incomparable types: boolean and java.lang.String
"other.title is null?" + other.title==null);
^
C:\Users\TBeckham14\Downloads\Desktop\Sophomore\apcomputers\Chapter 6 Project\7\CD.java:46: incomparable types: boolean and <nulltype>
"other.title is null?" + other.title==null);
^
So title is null, correct?
What can I do to fix it?
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: NullPointerException Problem

Posted 13 February 2012 - 01:18 PM

Because it is adding it to a String it is concatnating the string to the title and then comparing it to null. You need to put ( ) around them.

System.out.println("Is this title null? " + (title == null) + "\nIs the other title null? " + (other.title == null));
Was This Post Helpful? 1
  • +
  • -

#6 trogan234   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-February 12

Re: NullPointerException Problem

Posted 13 February 2012 - 01:50 PM

View PostFuzzyness, on 13 February 2012 - 01:18 PM, said:

Because it is adding it to a String it is concatnating the string to the title and then comparing it to null. You need to put ( ) around them.

System.out.println("Is this title null? " + (title == null) + "\nIs the other title null? " + (other.title == null));


Wait, so is the problem in the compareTo?
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 12350
  • View blog
  • Posts: 20,984
  • Joined: 19-March 11

Re: NullPointerException Problem

Posted 13 February 2012 - 01:55 PM

The problem fuzzy solved is that I left out some parens in the diagnostic println statement I sketched for you. My bad. (Thank you, fuzzy!)

The problem in your code is that you're trying to dereference a null object in your compareTo method. This statement will tell you which of your objects are null. From there, you can figure out how they got that way, and then you'll be able to solve the problem.
Was This Post Helpful? 0
  • +
  • -

#8 trogan234   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-February 12

Re: NullPointerException Problem

Posted 13 February 2012 - 03:22 PM

hmm, okay so ive been looking for where it goes null but I cant find it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1