java.lang.NullPointerException错误

时间:2015-03-07 05:49:52

标签: java

这是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

这是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参赛作品

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>

当我运行这个java代码时,我得到此错误:线程“main”中的异常java.lang.NullPointerException             在CDDemo.main(CDDemo.java:58)

任何人都知道如何解决这个问题?我只是想在不改变代码的情况下应用最简单的修正......

1 个答案:

答案 0 :(得分:0)

您永远不会初始化cd1变量,因此它保持为空。

您应该替换

  CD cd = new CD();

  cd1 = new CD();