2015-03-07 2 views
0

Это CD.javajava.lang.NullPointerException Ошибка

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

'cd',' cd1', 'cd2',' cd3' ... –

ответ

0

Вы никогда не инициализируете переменную cd1, поэтому она остается пустой.

Вы, вероятно, следует заменить

CD cd = new CD(); 

с

cd1 = new CD(); 
+0

Это установил его, но теперь cd2 и CD3 является empty –

+0

@Baris, но вы правильно инициализируете cd2 и cd3. Что вы подразумеваете под пустым? – Eran

Смежные вопросы