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());
        
    }
}
 
    