1
Exception in thread "main" java.lang.NullPointerException
at PassTest3.putGeometricObject(PassTest3.java:114)
at PassTest3.main(PassTest3.java:135)
这是错误提示
下面的是代码
import java.util.*;
abstract class GeometricObject{
abstract double getPerimeter();
abstract double getArea();
}
class Rectangle extends GeometricObject{
private int x1;
private int x2;
private int y1;
private int y2;
public Rectangle(int x1,int x2,int y1,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public int getHeight(){
int aHeight=0;
aHeight=Math.abs(x1-x2);
return aHeight;
}
public int getWidth(){
int aWidth=0;
aWidth=Math.abs(y1-y2);
return aWidth;
}
public double getPerimeter(){
return getHeight()*2+getWidth()*2;
}
public double getArea(){
return getHeight()*getWidth();
}
}
class Triangle extends GeometricObject{
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public Triangle(int x1,int x2,int x3,int y1,int y2,int y3){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
public double getFirstLength(){
double aFirstLength=0;
aFirstLength=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return aFirstLength;
}
public double getSecondLength(){
double aSecondLength=0;
aSecondLength=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
return aSecondLength;
}
public double getThirdLength(){
double aThirdLength=0;
aThirdLength=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
return aThirdLength;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=getFirstLength()+getSecondLength
()+getThirdLength();
return aPerimeter;
}
public double getArea(){
double aArea=0;
double half=getPerimeter()/2;
aArea=Math.sqrt(half*(half-getFirstLength())*
(half-getSecondLength())*(half-getThirdLength()));
return aArea;
}
}
class Circle extends GeometricObject{
private int x1;
private int y1;
private int r;
private double constant;
public Circle(int x1,int y1,int r){
this.x1=x1;
this.y1=y1;
this.r=r;
constant=3.1415;
}
public int getDiameter(){
int aDiameter=0;
aDiameter=2*r;
return aDiameter;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=constant*getDiameter();
return aPerimeter;
}
public double getArea(){
double aArea=0;
aArea=constant*r*r;
return aArea;
}
}
public class PassTest3{
static void putGeometricObject(GeometricObject[] s){
double sumPerimeter=0;
double sumArea=0;
for(int i=0;i<s.length;i++){
sumPerimeter=s[i].getPerimeter()+sumPerimeter;
sumArea=s[i].getArea()+sumArea;
}
System.out.println(sumPerimeter);
System.out.println(sumArea);
}
public static void main(String[] args){
java.util.Scanner sc=new java.util.Scanner(System.in);
GeometricObject s[]=new GeometricObject[6];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
}
}
putGeometricObject(s);
}
}
6 个解决方案
#1
GeometricObject s[]=new GeometricObject[6];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
上面这块代码不对,你新建数组有6个元素,但是在int j=sc.nextInt();这句话,你只接受了一个读入的数据,switch之后,只实例化了一个数组元素,所以在进入putGeometricObject(s);之后会产生空指针
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
上面这块代码不对,你新建数组有6个元素,但是在int j=sc.nextInt();这句话,你只接受了一个读入的数据,switch之后,只实例化了一个数组元素,所以在进入putGeometricObject(s);之后会产生空指针
#2
看了半天你的程序,觉得有些逻辑好像不对
首先,你新建数组有6个对象,但是switch中只有三个选项,是否数组的长度应该为3?
其次,你应该是想对数组中的每个元素都输出sumPerimeter和sumArea吧,这两个sysout语句应该放到for循环中
个人理解,不知是否正确
首先,你新建数组有6个对象,但是switch中只有三个选项,是否数组的长度应该为3?
其次,你应该是想对数组中的每个元素都输出sumPerimeter和sumArea吧,这两个sysout语句应该放到for循环中
个人理解,不知是否正确
#3
输出的是总和,把6改了之后还是老问题
这是我修改后的程序
import java.util.*;
abstract class GeometricObject{
abstract double getPerimeter();
abstract double getArea();
}
class Rectangle extends GeometricObject{
private int x1;
private int x2;
private int y1;
private int y2;
public Rectangle(int x1,int x2,int y1,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public int getHeight(){
int aHeight=0;
aHeight=Math.abs(x1-x2);
return aHeight;
}
public int getWidth(){
int aWidth=0;
aWidth=Math.abs(y1-y2);
return aWidth;
}
public double getPerimeter(){
return getHeight()*2+getWidth()*2;
}
public double getArea(){
return getHeight()*getWidth();
}
}
class Triangle extends GeometricObject{
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public Triangle(int x1,int x2,int x3,int y1,int y2,int y3){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
public double getFirstLength(){
double aFirstLength=0;
aFirstLength=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return aFirstLength;
}
public double getSecondLength(){
double aSecondLength=0;
aSecondLength=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
return aSecondLength;
}
public double getThirdLength(){
double aThirdLength=0;
aThirdLength=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
return aThirdLength;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=getFirstLength()+getSecondLength
()+getThirdLength();
return aPerimeter;
}
public double getArea(){
double aArea=0;
double half=getPerimeter()/2;
aArea=Math.sqrt(half*(half-getFirstLength())*
(half-getSecondLength())*(half-getThirdLength()));
return aArea;
}
}
class Circle extends GeometricObject{
private int x1;
private int y1;
private int r;
private double constant;
public Circle(int x1,int y1,int r){
this.x1=x1;
this.y1=y1;
this.r=r;
constant=3.1415;
}
public int getDiameter(){
int aDiameter=0;
aDiameter=2*r;
return aDiameter;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=constant*getDiameter();
return aPerimeter;
}
public double getArea(){
double aArea=0;
aArea=constant*r*r;
return aArea;
}
}
public class PassTest3{
/* static void putGeometricObject(GeometricObject[] s){
for(int i=0;i<s.length;i++)
{ s.getPerimeter();
s.getArea();
}
}*/
public static void main(String[] args){
java.util.Scanner sc=new java.util.Scanner(System.in);
double sumPerimeter=0;
double sumArea=0;
GeometricObject s[]=new GeometricObject[3];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
}
sumPerimeter=s[i].getPerimeter()+sumPerimeter;
sumArea=s[i].getArea()+sumArea;
}
/*putGeometricObject(s)*/
System.out.println(sumPerimeter);
System.out.println(sumArea);
}
}
这是我修改后的程序
import java.util.*;
abstract class GeometricObject{
abstract double getPerimeter();
abstract double getArea();
}
class Rectangle extends GeometricObject{
private int x1;
private int x2;
private int y1;
private int y2;
public Rectangle(int x1,int x2,int y1,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public int getHeight(){
int aHeight=0;
aHeight=Math.abs(x1-x2);
return aHeight;
}
public int getWidth(){
int aWidth=0;
aWidth=Math.abs(y1-y2);
return aWidth;
}
public double getPerimeter(){
return getHeight()*2+getWidth()*2;
}
public double getArea(){
return getHeight()*getWidth();
}
}
class Triangle extends GeometricObject{
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public Triangle(int x1,int x2,int x3,int y1,int y2,int y3){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
public double getFirstLength(){
double aFirstLength=0;
aFirstLength=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return aFirstLength;
}
public double getSecondLength(){
double aSecondLength=0;
aSecondLength=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
return aSecondLength;
}
public double getThirdLength(){
double aThirdLength=0;
aThirdLength=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
return aThirdLength;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=getFirstLength()+getSecondLength
()+getThirdLength();
return aPerimeter;
}
public double getArea(){
double aArea=0;
double half=getPerimeter()/2;
aArea=Math.sqrt(half*(half-getFirstLength())*
(half-getSecondLength())*(half-getThirdLength()));
return aArea;
}
}
class Circle extends GeometricObject{
private int x1;
private int y1;
private int r;
private double constant;
public Circle(int x1,int y1,int r){
this.x1=x1;
this.y1=y1;
this.r=r;
constant=3.1415;
}
public int getDiameter(){
int aDiameter=0;
aDiameter=2*r;
return aDiameter;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=constant*getDiameter();
return aPerimeter;
}
public double getArea(){
double aArea=0;
aArea=constant*r*r;
return aArea;
}
}
public class PassTest3{
/* static void putGeometricObject(GeometricObject[] s){
for(int i=0;i<s.length;i++)
{ s.getPerimeter();
s.getArea();
}
}*/
public static void main(String[] args){
java.util.Scanner sc=new java.util.Scanner(System.in);
double sumPerimeter=0;
double sumArea=0;
GeometricObject s[]=new GeometricObject[3];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
}
sumPerimeter=s[i].getPerimeter()+sumPerimeter;
sumArea=s[i].getArea()+sumArea;
}
/*putGeometricObject(s)*/
System.out.println(sumPerimeter);
System.out.println(sumArea);
}
}
#4
还是我在一楼说的问题, int j=sc.nextInt();这句话得放到for循环之中,否则你只接受了一个读入的数据,switch之后,只实例化了一个数组元素,所以在进入putGeometricObject(s);之后会产生空指针
#5
看来你是新手吧,可以用端点调试一下,一步一步的走,容易找到问题的所在
#6
at PassTest3.putGeometricObject(PassTest3.java:114)
报空指针 ,查看一下
报空指针 ,查看一下
#1
GeometricObject s[]=new GeometricObject[6];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
上面这块代码不对,你新建数组有6个元素,但是在int j=sc.nextInt();这句话,你只接受了一个读入的数据,switch之后,只实例化了一个数组元素,所以在进入putGeometricObject(s);之后会产生空指针
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
上面这块代码不对,你新建数组有6个元素,但是在int j=sc.nextInt();这句话,你只接受了一个读入的数据,switch之后,只实例化了一个数组元素,所以在进入putGeometricObject(s);之后会产生空指针
#2
看了半天你的程序,觉得有些逻辑好像不对
首先,你新建数组有6个对象,但是switch中只有三个选项,是否数组的长度应该为3?
其次,你应该是想对数组中的每个元素都输出sumPerimeter和sumArea吧,这两个sysout语句应该放到for循环中
个人理解,不知是否正确
首先,你新建数组有6个对象,但是switch中只有三个选项,是否数组的长度应该为3?
其次,你应该是想对数组中的每个元素都输出sumPerimeter和sumArea吧,这两个sysout语句应该放到for循环中
个人理解,不知是否正确
#3
输出的是总和,把6改了之后还是老问题
这是我修改后的程序
import java.util.*;
abstract class GeometricObject{
abstract double getPerimeter();
abstract double getArea();
}
class Rectangle extends GeometricObject{
private int x1;
private int x2;
private int y1;
private int y2;
public Rectangle(int x1,int x2,int y1,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public int getHeight(){
int aHeight=0;
aHeight=Math.abs(x1-x2);
return aHeight;
}
public int getWidth(){
int aWidth=0;
aWidth=Math.abs(y1-y2);
return aWidth;
}
public double getPerimeter(){
return getHeight()*2+getWidth()*2;
}
public double getArea(){
return getHeight()*getWidth();
}
}
class Triangle extends GeometricObject{
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public Triangle(int x1,int x2,int x3,int y1,int y2,int y3){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
public double getFirstLength(){
double aFirstLength=0;
aFirstLength=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return aFirstLength;
}
public double getSecondLength(){
double aSecondLength=0;
aSecondLength=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
return aSecondLength;
}
public double getThirdLength(){
double aThirdLength=0;
aThirdLength=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
return aThirdLength;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=getFirstLength()+getSecondLength
()+getThirdLength();
return aPerimeter;
}
public double getArea(){
double aArea=0;
double half=getPerimeter()/2;
aArea=Math.sqrt(half*(half-getFirstLength())*
(half-getSecondLength())*(half-getThirdLength()));
return aArea;
}
}
class Circle extends GeometricObject{
private int x1;
private int y1;
private int r;
private double constant;
public Circle(int x1,int y1,int r){
this.x1=x1;
this.y1=y1;
this.r=r;
constant=3.1415;
}
public int getDiameter(){
int aDiameter=0;
aDiameter=2*r;
return aDiameter;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=constant*getDiameter();
return aPerimeter;
}
public double getArea(){
double aArea=0;
aArea=constant*r*r;
return aArea;
}
}
public class PassTest3{
/* static void putGeometricObject(GeometricObject[] s){
for(int i=0;i<s.length;i++)
{ s.getPerimeter();
s.getArea();
}
}*/
public static void main(String[] args){
java.util.Scanner sc=new java.util.Scanner(System.in);
double sumPerimeter=0;
double sumArea=0;
GeometricObject s[]=new GeometricObject[3];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
}
sumPerimeter=s[i].getPerimeter()+sumPerimeter;
sumArea=s[i].getArea()+sumArea;
}
/*putGeometricObject(s)*/
System.out.println(sumPerimeter);
System.out.println(sumArea);
}
}
这是我修改后的程序
import java.util.*;
abstract class GeometricObject{
abstract double getPerimeter();
abstract double getArea();
}
class Rectangle extends GeometricObject{
private int x1;
private int x2;
private int y1;
private int y2;
public Rectangle(int x1,int x2,int y1,int y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public int getHeight(){
int aHeight=0;
aHeight=Math.abs(x1-x2);
return aHeight;
}
public int getWidth(){
int aWidth=0;
aWidth=Math.abs(y1-y2);
return aWidth;
}
public double getPerimeter(){
return getHeight()*2+getWidth()*2;
}
public double getArea(){
return getHeight()*getWidth();
}
}
class Triangle extends GeometricObject{
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public Triangle(int x1,int x2,int x3,int y1,int y2,int y3){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.x3=x3;
this.y3=y3;
}
public double getFirstLength(){
double aFirstLength=0;
aFirstLength=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return aFirstLength;
}
public double getSecondLength(){
double aSecondLength=0;
aSecondLength=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
return aSecondLength;
}
public double getThirdLength(){
double aThirdLength=0;
aThirdLength=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
return aThirdLength;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=getFirstLength()+getSecondLength
()+getThirdLength();
return aPerimeter;
}
public double getArea(){
double aArea=0;
double half=getPerimeter()/2;
aArea=Math.sqrt(half*(half-getFirstLength())*
(half-getSecondLength())*(half-getThirdLength()));
return aArea;
}
}
class Circle extends GeometricObject{
private int x1;
private int y1;
private int r;
private double constant;
public Circle(int x1,int y1,int r){
this.x1=x1;
this.y1=y1;
this.r=r;
constant=3.1415;
}
public int getDiameter(){
int aDiameter=0;
aDiameter=2*r;
return aDiameter;
}
public double getPerimeter(){
double aPerimeter=0;
aPerimeter=constant*getDiameter();
return aPerimeter;
}
public double getArea(){
double aArea=0;
aArea=constant*r*r;
return aArea;
}
}
public class PassTest3{
/* static void putGeometricObject(GeometricObject[] s){
for(int i=0;i<s.length;i++)
{ s.getPerimeter();
s.getArea();
}
}*/
public static void main(String[] args){
java.util.Scanner sc=new java.util.Scanner(System.in);
double sumPerimeter=0;
double sumArea=0;
GeometricObject s[]=new GeometricObject[3];
int j=sc.nextInt();
for(int i=0;i<3;i++){
switch(j){
case 0: s[j]=new Rectangle(0,3,0,4);break;
case 1: s[j]=new Triangle(0,0,4,0,3,0);break;
case 2: s[j]=new Circle(5,5,3);break;
}
sumPerimeter=s[i].getPerimeter()+sumPerimeter;
sumArea=s[i].getArea()+sumArea;
}
/*putGeometricObject(s)*/
System.out.println(sumPerimeter);
System.out.println(sumArea);
}
}
#4
还是我在一楼说的问题, int j=sc.nextInt();这句话得放到for循环之中,否则你只接受了一个读入的数据,switch之后,只实例化了一个数组元素,所以在进入putGeometricObject(s);之后会产生空指针
#5
看来你是新手吧,可以用端点调试一下,一步一步的走,容易找到问题的所在
#6
at PassTest3.putGeometricObject(PassTest3.java:114)
报空指针 ,查看一下
报空指针 ,查看一下