This question already has an answer here:
I am a beginner in object oriented programming what i wanted to do here is create a array of test and set length and breadth in each objects of array "a[]" and print the area. I tried with only one object but the error pop out as null pointer. The setLength() and setBreadth() method is not working or what.
package test;
public class test {
private double length;
private double breadth;
private double area;
public test()
{
this.length=0;
this.breadth=0;
}
public test(double length, double breadth)
{
this.length=length;
this.breadth=breadth;
}
public void setLength(double length)
{
System.out.println("length setted");
this.length=length;
}
public void setBreadth (double breadth)
{
this.breadth=breadth;
System.out.println("breadth setted");
}
public double getArea()
{
return 2*(this.length+this.breadth);
}
}
package test;
import java.util.*;
public class hellop {
public static void main(String[] args) {
// TODO Auto-generated method stub
test a[]= new test[5];
a[1].setLength(5.9);
a[1].setBreadth(9.5);
System.out.println(a[1].getArea());
}
}