0

This is CD.java

public class CD
{
   // Fields (or instance data members)
   private String title;         // CD's title
   private String artist;        // CD's artist
   private double cost;          // CD's cost
   private int numberOfTracks;   // Number of Tracks

   /**
    * Constructors: Please describe each of them.
    * 
    */
   public CD ()
   {

   }

   public CD (String title, String artist, double cost, int numberOfTracks)
   {

    // four statements




   }    

   /**
    * The setTitle () method has a parameter.  It assigns the value of the parameter to instance field name.
    * The method does not return a value.
    */

   public void setTitle (String cdTitle)
   {
      title = cdTitle;
   }

   /**
    * The setArtist () method has a parameter.  It assigns the value of the parameter to instance field name.
    * The method does not return a value.
    */

   public void setArtist (String cdArtist)
   {
      artist = cdArtist;
   }  

   /**
    * The setCost () method has a parameter.  It assigns the value of the parameter to instance field name.
    * The method does not return a value.
    */

   public void setCost (double cdCost)
   {
      cost = cdCost;
   }

   /**
    * The setNumberOfTracks () method has a parameter.  It assigns the value of the parameter to instance field name.
    * The method does not return a value.
    */

   public void setNumberOfTracks (int cdNumberOfTracks)
   {
      numberOfTracks = cdNumberOfTracks;
   }

   /**
    * The getTitle method does not accept arguments.
    * It simply returns the value of the title field.
    */

   public String getTitle ()
   {
      return title;
   }

   /**
    * The getArtist method does not accept arguments.
    * It simply returns the value of the artist field.
    */

   public String getArtist ()
   {
      return artist;
   }

   /**
    * The getCost method does not accept arguments.
    * It simply returns the value of the cost field.
    */

   public Double getCost ()
   {
      return cost;
   }

   /**
    * The getNumberofTracks method does not accept arguments.
    * It simply returns the value of the numberofTracks field.
    */

   public int getNumberOfTracks ()
   {
      return numberOfTracks;
   }

} // End of class CD

This is CDDemo.java

// an import statement needed here for keyboard input
import java.util.Scanner;

public class CDDemo
{
  private static CD cd1;
  private static CD cd2;
  private static CD cd3;

   public static void main (String [] args)
   {
      // Declare ALL necessary variables here, 
       String title;         // CD's title
       String artist;        // CD's artist
       double cost;          // CD's cost
       int numberOfTracks;   // Number of Tracks

      // Create a Scanner object
      Scanner keyboard = new Scanner(System.in);

      // Create the first CD object. Use the no-arg constructor (assuming
      // that you added one to the CD class definition).
      CD cd = new CD();

      // Read the data for First CD from the keyboard
      System.out.print("Please enter CD1 Title: "); 
      title=keyboard.nextLine();

      System.out.print("Please enter CD1 Artist: ");
      artist=keyboard.nextLine();

      System.out.print("Please enter CD1 Cost: ");
      cost=keyboard.nextDouble();

      // Clear out Double
      keyboard.nextLine();

      System.out.print("Please enter CD1 number of tracks: ");
      numberOfTracks=keyboard.nextInt();

      // Clear out int
      keyboard.nextLine();

      // Call the set methods of the first CD object  
      // to set its fields with values entered from the keyboard.
      // This is how you test the set methods.
      cd1.setTitle (title);
      cd1.setArtist (artist);
      cd1.setCost (cost);
      cd1.setNumberOfTracks (numberOfTracks);

      // Create the second and third CD objects using the 
      // constructor that accepts arguments for all  of the fields.
      cd2 = new CD ("CD Title2", "CD Artist2", 15.50, 5);
      cd3 = new CD ("CD Title3", "CD Artist3", 20.00, 8);

      // The rest of this class tests get methods (and the successful execution of
      // constructors and set methods as well).

      // Display the data for CD 1.
      System.out.println ("CD #1");
      System.out.println ("Title: " + cd1.getTitle());
      System.out.println ("Artist: " + cd1.getArtist());
      System.out.println ("Cost: " + cd1.getCost());
      System.out.println ("Number of Tracks: " + cd1.getNumberOfTracks());
      System.out.println ();

      // Display the data for CD 2.
      System.out.println ("CD #2");
      System.out.println ("Title: " + cd2.getTitle());
      System.out.println ("Artist: " + cd2.getArtist());
      System.out.println ("Cost: " + cd2.getCost());
      System.out.println ("Number of Tracks: " + cd2.getNumberOfTracks());
      System.out.println ();

      // Display the data for CD 3.
      System.out.println ("CD #3");
      System.out.println ("Title: " + cd3.getTitle());
      System.out.println ("Artist: " + cd3.getArtist());
      System.out.println ("Cost: " + cd3.getCost());
      System.out.println ("Number of Tracks: " + cd3.getNumberOfTracks());
      System.out.println ();

   } // end of method main ()

} // end of class CDDemo

CMD Entries

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\fLyBa>cd C:\Users\fLyBa\Desktop\java

C:\Users\fLyBa\Desktop\java>javac CD.java

C:\Users\fLyBa\Desktop\java>javac CDDemo.java

C:\Users\fLyBa\Desktop\java>java CDDemo
Please enter CD1 Title: Title
Please enter CD1 Artist: Artist
Please enter CD1 Cost: 12
Please enter CD1 number of tracks: 12
Exception in thread "main" java.lang.NullPointerException
        at CDDemo.main(CDDemo.java:58)

C:\Users\fLyBa\Desktop\java>

When I run this java code I get This error:Exception in thread "main" java.lang.NullPointerException at CDDemo.main(CDDemo.java:58)

Anyone knows how can I fix this? I just want to apply simplest correction without changing the code too much...

Baris Sarac
  • 115
  • 1
  • 7

1 Answers1

0

You never initialize the cd1 variable, so it remains null.

You should probably replace

  CD cd = new CD();

with

  cd1 = new CD();
Eran
  • 387,369
  • 54
  • 702
  • 768