您好我有一个名为Rectangle的对象,这是代码:
class Rectangle{
private double length, breath, area, perimeter;
private static double largestArea;
public void setSideDetails(double length, double breath){
this.length = length;
this.breath = breath;
}
public double getArea(){
area = length*breath;
return area;
}
public double getPerimeter(){
perimeter = 2*(length+breath);
return perimeter;
}
public static void setLargestArea(double areas[]){
for(int i = 0; i < areas.length; i++){
if(areas[i] > largestArea){
largestArea = areas[i];
}
}
}
public static double getLargestArea(){
return largestArea;
}
public void drawRectangle(){
System.out.print(" ");
for(int i = 0; i < length; i++){
System.out.print("_");
}
System.out.println();
for(int i = 0; i < breath; i++){
System.out.print("|");
for(int j = 0; j < length; j++){
System.out.print(" ");
}
System.out.println("|");
}
System.out.print(" ");
for(int i = 0; i < length; i++){
System.out.print("¯");
}
System.out.println();
}
}
以及正在使用它的类:
import java.util.*;
class UsingRectangle{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
in.useDelimiter("\n");
int number = 0;
boolean isError = false;
String units;
System.out.print("How many rectangles do you want to create? ");
do{
try{
number = in.nextInt();
number = Math.abs(number);
isError = false;
}catch(InputMismatchException e){
System.out.println("Your input was not accepted. Please try again.");
in.next();
isError = true;
}
}while(isError);
do{
isError = false;
System.out.print("Please enter the units in mm, cm or m: ");
units = in.next();
if(!(units.equalsIgnoreCase("mm"))&&!(units.equalsIgnoreCase("cm"))&&!(units.equalsIgnoreCase("m"))){
System.out.println("Your input was not accepted. Please try again.");
isError = true;
}
}while(isError);
Rectangle[] r = new Rectangle[number]; //The actual rectangles are instanceiated here.
double areas[] = new double[number]; //This will be used to collect the areas to be sent to a static method.
for(int i = 0; i < r.length; i++){
double length = 0;
double breath = 0;
do{
isError = false;
try{
System.out.print("Please enter the length of Rectangle "+(i+1)+": ");
length = Math.abs(in.nextInt());
}catch(InputMismatchException e){
System.out.println("Your input was not accepted. Please try again.");
in.next();
isError = true;
}
}while(isError);
do{
isError = false;
try{
System.out.print("Please enter the breath of Rectangle "+(i+1)+" ");
breath = Math.abs(in.nextInt());
}catch(InputMismatchException e){
System.out.println("Your input was not accepted. Please try again.");
in.next();
isError = true;
}
}while(isError);
r[i].setSideDetails(length, breath);
}
for(int i = 0; i < r.length; i++){
r[i].drawRectangle();
areas[i] = r[i].getArea();
System.out.println("Area: "+areas[i]+units+"2 Perimeter: "+r[i].getPerimeter()+units);
}
System.out.print("The largest area is: ");
Rectangle.setLargestArea(areas);
System.out.print(Rectangle.getLargestArea());
}
}
我的问题是:
表示r[i].setSideDetails(length,breath);
运行时错误 - java.lang.nullPointerException null
。我不知道是什么造成了这个。任何搜索都没有结果,除了如何解决之外,我想知道原因,影响和预防。
由于
顺便说一下,我是一名A级学生,在两年内参加考试,所以如果我是专业人士,请不要表现得像我应该知道的那样。
答案 0 :(得分:4)
那是因为您刚刚声明了Rectangle[] r = new Rectangle[number];
并且没有初始化此数组中的元素。默认情况下,数组的每个元素都是null
,在尝试调用方法时会给你NPE。在调用方法之前初始化数组中的每个元素。
示例:的
Rectangle[] r = new Rectangle[number];
for(int i = 0; i < r.length; i++){
r[i] = new Rectangle(); // You can use either this or some other constructor you have.
}
答案 1 :(得分:0)
您收到NullPointerException,因为引用r[i]
指向任何/ null
。因此,您必须实例化Rectangle
个对象并将其放入数组中。
for(int i = 0; i < r.length; i++)
r[i] = new Rectangle(); // creating a new Rectangle object here