public class Yuanzhu { public static void main(String args[]){ Circle cir=new Circle(2); Volume vol=new Volume(cir,5); } } class Circle{ double radius; public Circle(double r){ radius=r; } double getArea(){ return 3.14*radius*radius; } } class Volume{ double heigh; Circle circle; public Volume(Circle c,double h){ circle=c; heigh=h; } double s=circle.getArea();//s为底面积 public void getVolume(){ double volume=s*heigh; System.out.println("体积是"+volume); } }
三个类计算圆柱体积,Circle类创建底面圆对象,有个方法返回圆面积,Volume类计算圆柱体积,并打印输出,主函数在Yuanzhu类中。但是运行后提示空指针异常是怎么回事?double s=circle.getArea();这个报错了
慕圣0830664