-1

I am quite new to Java but I encountered the following error: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "bussines.Plot$Rectangle.setRectangle(int, int)" because "java.util.ArrayList.get(int).rectangle" is null at bussines.Owner.buyRectangle(Owner.java:24) at construction.Application.run(Application.java:17) at construction.Application.main(Application.java:36)

I will insert bellow the relevant code snippet:

public class Plot
{
    Rectangle rectangle;
    Square square;
    
    public class Rectangle
    {
        public int length;
        public int width;
    }
        
        public Rectangle() 
        {
            length = 0;
            width = 0;
        }
        
        public void setRectangle(int l, int w) 
        {
            length = l;
            width = w;
        }
        
        public int getArea() 
        {
            return length * width;
        }
    }
    
    public class Square
    {
        public int size;
        
        public Square() 
        {
            size = 0;
        }
        
        public void setSquare(int s) 
        {
            size = s;
        }
        
        public int getArea() 
        {
            return size * size;
        }
    }
}
public class Owner
{
    
    public String name;
    public ArrayList<Plot> landPlots;
    
    public Owner(String n)
    {
        name = n;
        landPlots = new ArrayList<Plot>();
    }
    
    public void buySquare(int lastUpdate, int size) 
    {
        landPlots.add(new Plot());
        landPlots.get(lastUpdate).square.setSquare(size);
    }
    
    public void buyRectangle(int lastUpdate, int l, int w) 
    {
        landPlots.add(new Plot());
        landPlots.get(0).rectangle.setRectangle(l, w);
    }
}
public class Application 
{
    private Owner owner;
    private int lastUpdate;
    
    public Application() 
    {
        owner = new Owner("Matei");
        lastUpdate = -1;
    }
    
    public void run() 
    {
        System.out.println("Buying four plots.");
        
        owner.buyRectangle(0, 4, 2);
        owner.buySquare(1, 5);
        owner.buyRectangle(2, 2, 2);
        owner.buySquare(3, 4);
        
        System.out.println("Compute total area.");
        System.out.print(owner.getTotalArea());
        
        System.out.println("Selling two plots.");
        owner.sellPlot(0);
        owner.sellPlot(1);
        
        System.out.println("Compute total area.");
        System.out.print(owner.getTotalArea());
        
    }
}
  • 1
    Not the object in the arraylist itself is null, but the `rectangle` and `square` fields in your `Plot` object are. The exception stacktrace here holds all the necessary information on what is wrong. – maloomeister Mar 05 '21 at 11:57
  • thanks @maloomeister but I can't seem to find a fix for this... – Matei Anghel Mar 05 '21 at 11:59

1 Answers1

0

Adjust you Plot class. You need to Initialize the Rectangle and the Square object in your constructor.

landPlots.get(0).rectangle.setRectangle(l, w);

Otherwise your line will have a NullException as the rectangle Class is null.

Buying four plots.
Exception in thread "main" java.lang.NullPointerException
at Owner.buyRectangle(Owner.java:24)
at Application.run(Application.java:16)
at NullExceptionApplication.main(NullExceptionApplication.java:6)

Process finished with exit code 1

I did test this and it works now.

public class Plot {
Rectangle rectangle;
Square square;

public Plot() {
    rectangle = new Rectangle();
    square = new Square();
}

public class Rectangle {
    public int length;
    public int width;


    public Rectangle() {
        this.length = 0;
        this.width = 0;
    }

    public void setRectangle(int l, int w) {
        length = l;
        width = w;
    }

    public int getArea() {
        return length * width;
    }
}

public class Square {
    public int size;

    public Square() {
        size = 0;
    }

    public void setSquare(int s) {
        size = s;
    }

    public int getArea() {
        return size * size;
    }
  }
}
I_AM__PAUL
  • 118
  • 7