java - 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.
解决方案
那是因为,RoomDimension dim
没有被初始化。该字段必须使用构造函数或通过 setter 进行初始化。
在 RoomCarpet 类中:
public RoomCarpet(double carpetCost, RoomDimension dim)
{
this.carpetCost = carpetCost;
this.dim = dim;
}
主要:
RoomCarpet myRoomCarpet =
new RoomCarpet(cost, myRoomDim);
推荐阅读
- themes - PrestaShop 1.6 - 无法覆盖主题中的模块 tpl 文件
- wordpress - WordPress,我们如何获取浏览器网络监控中显示的所有js和css文件
- mattermost - 为用户添加短文本/徽章/图标
- aws-sam - 原因:无效的模板属性或属性 [Api]
- c# - 授权属性在我的项目中不起作用
- azure - 当不同容器注册表上的新映像可用时,如何触发 azure devops 构建管道?
- azure - 由于缺少“授权”标头,身份验证失败
- postgresql - Postgresql 从 JSONB 字段中的对象数组中获取键
- angular - 如何在 React 中使用现有的 Angular 服务?
- jquery - 下一个和上一个按钮来导航我的数据库