无法使用* .get *()将数据从一个类获取到另一个类

时间:2016-11-06 03:07:16

标签: java class object

我有两个课 RoomDimension & RoomCarpet 。然后我有一个调用这两个类的程序,但是当我尝试获得地毯的TotalCost时,我遇到了RoomCarpet类的问题。当我运行 CarpetCalculator

时,它会给我这个错误
(Exception in thread "main" java.lang.NullPointerException
    at RoomCarpet.getTotalCost(RoomCarpet.java:49)
    at CarpetCalculator.main(CarpetCalculator.java:44)

java:44,此位置是调用getTotalCost的结尾的system.out.print

当我尝试在RoomCarpet类下调用getTotalCost时。我认为它与size.getArea()有关(当我调用它时)。以下是所有课程的代码。感谢大家的帮助。

RoomDimension:

public class RoomDimension {
       private double length;
       private double width;

       public RoomDimension(double len, double w){
          length = len;
          width = w;
       }

       public void setLength(double len){
          length = len;
       }

       public void setWidth(double w){
          width = w;
       }

       public double getLength(){
          return length;
       }

       public double getWidth(){
          return width;
       }

       public double getArea(){
          return length * width;
       }

       public String toString(){
           String str = "The length you entered was " + length 
                        + " and the width you entered was " + width;
           return str;
       }
    }

RoomCarpet:

public class RoomCarpet{
    private RoomDimension size;
    private double carpetCost;

    public RoomCarpet (double cost){
        carpetCost = cost;
    }

       public void setCarpetCost(double cost){
          carpetCost = cost;
       }

       public double getCarpetCost(){
          return carpetCost;
       }

       public double getTotalCost(){
           return size.getArea() * carpetCost; 
       }

      public String toString(){
           String str = "\nYour total area for your room is " + size.getArea()
                        + "\nThe cost of your carpet per square foot is " + carpetCost;

           return str;
       }

    }

CarpetCalculator:

import java.util.Scanner;  // Needed for the Scanner class

/**
 * This program demonstrates the RoomDimension & RoomCarpet classes.
 */

public class CarpetCalculator {

    public static void main(String[] args){

        double length; // hold room length
        double width;  // hold room width
        double cost;   // hold carpet cost

          Scanner keyboard = new Scanner(System.in);

          System.out.print("What is your rooms length? ");
          length = keyboard.nextDouble();

          System.out.print("What is your rooms width? ");
          width = keyboard.nextDouble();

          System.out.print("What is the cost of your carpet per square foot? ");
          cost = keyboard.nextDouble();

          RoomDimension testDimension = new RoomDimension(length, width);

          RoomCarpet testCarpet = new RoomCarpet(cost);

          System.out.println(testDimension);
          System.out.println(testCarpet);
          System.out.println("Which means your total cost to carpet the room is " + testCarpet.getTotalCost());
    }
    }

2 个答案:

答案 0 :(得分:0)

这是因为您从未在 RoomCarpet 中初始化 size 属性。您应该在 RoomCarpet 构造函数中传递 RoomDimension 对象:

public RoomCarpet (RoomDimension size, double carpetCost){
    this.size = size;
    this.carpetCost = carpetCost;
}

答案 1 :(得分:0)

您需要在RoomCarpet类中初始化size变量。您可以使用setter方法或传递构造函数。

试试这个:

公共课RoomCarpet {

private RoomDimension size;
private double carpetCost;

public RoomCarpet (double cost){
    carpetCost = cost;
}

public RoomCarpet (double cost,RoomDimension size){
    carpetCost = cost;
    this.size = size;
}

public void setCarpetCost(double cost){
    carpetCost = cost;
}

public double getCarpetCost(){
    return carpetCost;
}

public RoomDimension getSize() {
    return size;
}

public void setSize(RoomDimension size) {
    this.size = size;
}

public double getTotalCost(){
    if(size != null) {
        return size.getArea() * carpetCost;
    }
    else {
        System.out.println("error size is not define");
        return 0;
    }
}

public String toString(){
    String str = "\nYour total area for your room is " + size.getArea()
            + "\nThe cost of your carpet per square foot is " + carpetCost;

    return str;
}

}