So my title was meant to be array not loop
I need to create an application that will generate random spots so far my work is this

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;


public class Shape{
  private int x;
  private int y;
  private int width;
  private int height;
  private Color color;
  
// random values with particular limits
  public int randomRange(int low, int high){
   Random generator = new Random();
   return generator.nextInt(high - low +1) + low;
  }
  
  public Shape(int width, int height, int x, int y, Color color){
    Random r = new Random();
    this.width = r.nextInt(30) + 10;
    this.height = this.width;
    this.x = r.nextInt(400 - this.width) + 0;
    this.y = r.nextInt(400 - this.height) + 0;
    this.color = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
  }
  public void display(Graphics page) {
    page.setColor(color);
    page.drawOval(x, y, width, height); 
    page.fillOval(x, y, width, height);
  }
}
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;

public class ShapePanel extends JPanel{
  private Shape [] drawObjects = new Shape[20];
  private JButton addShape;
  private JTextField one;
  private JLabel label;
  private int count;
  
  DrawingPanel drawPanel = new DrawingPanel();
  
  public static void main (String[] args){
    JFrame frame = new JFrame();
    ShapePanel shape = new ShapePanel();
    frame.getContentPane().add (new ShapePanel());
    
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
  
  public ShapePanel(){
    JPanel controlPanel = new JPanel();
    addShape = new JButton("Add Shape");
    controlPanel.setPreferredSize (new Dimension(100, 400));
    one = new JTextField();
    controlPanel.add (addShape);
    label = new JLabel ("Count:");
    controlPanel.add (label);
    controlPanel.add (one);
    add(controlPanel);
    add(drawPanel);
    
    ButtonListener listener = new ButtonListener();
    addShape.addActionListener (listener);
  }
  
  private class ButtonListener implements ActionListener
  {
    public void actionPerformed (ActionEvent event)
    {
      if (event.getSource() == addShape){
        if(count < drawObjects.length){
          count++;
          one.setText(" " + count);
          drawPanel.repaint();
          
        }
      }
      
    }
  }
  
  private class DrawingPanel extends JPanel{
    public DrawingPanel(){
      setPreferredSize (new Dimension(400, 400));
      setBackground (Color.pink);
      
    }
  }
  public void paintComponent (Graphics g)
  {
    super.paintComponent(g); 
    for(int i = 0; i < drawObjects.length; i++){
      drawObjects[i].display(g);
      
    }
  }
}

I get an error of null at this line drawObjects.display(g) i also tried

for(Shape s : drawObjects)
s.display(g);

Check are u intializing each drawObjects before you use it

drawObjects[i] = new Shapes(); // Are u doing like this.........................

Your array of Shapes starts out containing 20 nulls which get replaced by Shapes when you add a new Shape. It looks like the var count contains the number of Shapes currently in the array, so that it what you need for the upper limit of your for loop - ie stop the loop before it goes into the null part of the array.
I can't see the code where you add a new Shape - shouldn't that be in the ButtonListener ?
What's the point of all the parameters on the constructor on line 21? You don't use any of them!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.