首页 > 解决方案 > Passing user variables in a demo class through a method in a different class

问题描述

The method that I am having trouble with is the "getTotalCost" method.

Each method is as follows:

DemoRoomCarpet

package cnmt.sec01.homeworkassign05;

import javax.swing.JOptionPane;

public class DemoRoomCarpet {
    private static double length;
    private static double width;
    private static double cost;
    public static void main(String[] args) {
        length = Double.valueOf(JOptionPane.showInputDialog("What is the length of your carpet?"));
        width = Double.valueOf(JOptionPane.showInputDialog("What is the width of your carpet?"));
        cost = Double.valueOf(JOptionPane.showInputDialog("What is the cost per square foot of carpet?"));

        RoomDimension myRoomDim = 
                new RoomDimension(length,width);

        RoomCarpet myRoomCarpet =
                new RoomCarpet(cost);

        myRoomDim.setLength(length);
        myRoomDim.setWidth(width);
        myRoomCarpet.setCarpetCost(cost);

        myRoomCarpet.getTotalCost();

        myRoomCarpet.toString();
    }
}

RoomDimension

package cnmt.sec01.homeworkassign05;

public class RoomDimension {

    private double length,
                   width;


    public RoomDimension(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

       public RoomDimension(RoomDimension object)
       {
           length = object.length;
           width = object.width;
       }


    /**
     * @return the length
     */
    public double getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public void setLength(double length) {
        this.length = length;
    }

    /**
     * @return the width
     */
    public double getWidth() {
        return width;
    }

    /**
     * @param width the width to set
     */
    public void setWidth(double width) {
        this.width = width;
    }

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

    public String toString() {
        String str = "Length is: " + length + "\nWidth is: " + width + "\nArea is: " + this.getArea();
        return str;
    }
}

and RoomCarpet

package cnmt.sec01.homeworkassign05;

public class RoomCarpet {

    private RoomDimension dim;
    private double carpetCost;

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

       public RoomCarpet(RoomCarpet object)
       {
           carpetCost = object.carpetCost;
       }

    /**
     * @return the carpetCost
     */
    public double getCarpetCost() {
        return carpetCost;
    }

    /**
     * @param carpetCost the carpetCost to set
     */
    public void setCarpetCost(double carpetCost) {
        this.carpetCost = carpetCost;
    }

    public double getTotalCost()
    {
        double total = dim.getArea() * carpetCost;
        return total;
    }

    public String toString()
    {
        String str = "Your total cost is " + this.getTotalCost() + " dollars";
        return str;
    }

}

I feel like the issue is something minor that I am missing, but I could be wrong. Apologies for no comments, but I will add them once I get over this error. Passing the getTotalCost method gives a null pointer exception, but I believe that I passed the user input through it.

标签: javaeclipse

解决方案


那是因为,RoomDimension dim没有被初始化。该字段必须使用构造函数或通过 setter 进行初始化。

在 RoomCarpet 类中:

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

主要:

    RoomCarpet myRoomCarpet =
            new RoomCarpet(cost, myRoomDim);

推荐阅读