Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7870695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:41:31+00:00 2026-06-03T01:41:31+00:00

I’d like to begin by saying this is an assignment. I do not want

  • 0

I’d like to begin by saying this is an assignment. I do not want the answer spoon fed to me but I would like to know what is causing my problems.

I am currently implementing Conway’s Game of Life. Clicking the cell should change the color, as to represent that cell being switched to an alive state. if clicked again, it should return to the default color.

When I click anywhere in the window, the program throws a Null Pointer Exception at line 56. Have been stuck on this for the last day or so, so any help is appreciated. Thanks!

Heres the code:

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

public class VisibleGrid extends JPanel implements MouseListener, KeyListener{ 

  CellGrid cellGrid;
  Graphics rect;

  public VisibleGrid(){
    addMouseListener(this);
    cellGrid = new CellGrid();
  }

  //Draw the grid of cells, 7px wide, 75 times to create 75x75 grid
  public void paint(Graphics g){
    for(int i=0; i<525;i=i+7){
      for(int j = 0; j<525; j=j+7){
        g.drawRect(i ,j,7,7);       
      }
    }   
  }

  //auxillary method called to fill in rectangles
  public void paint(Graphics g, int x, int y){
    g.fillRect(x, y, 7, 7);
    repaint();

  }

  //main method, adds this JPanel to a JFrame and sets up the GUI  
  public static void main(String[] args){
    JFrame j = new JFrame("Conway's Game of Life");
    j.setLayout(new BorderLayout());
    j.add(new VisibleGrid(), BorderLayout.CENTER);
    JTextArea info = new JTextArea("Press S to Start, E to End");
    info.setEditable(false);
    j.add(info, BorderLayout.SOUTH);
    j.setSize(530,565);
    j.setVisible(true);
  }

  //these methods are to satisfy the compiler/interface
  //Begin Mouse Events
  public void mouseExited(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseReleased(MouseEvent e){}
  public void mousePressed(MouseEvent e){}
  public void mouseClicked(MouseEvent e){
    //fill the selected rectangle
    rect.fillRect(e.getX(), e.getY(), 7,7); 
    repaint();

    //set the corresponding cell in the grid to alive
    int row = e.getY() /7;
    int column = e.getX() /7;
     cellGrid.getCell(row, column).setAlive(true);
  } 
  //End Mouse Events

//These methods are to satisfy the compiler/interface
//Begin KeyEvents
  public void keyReleased(KeyEvent e){}
  public void keyPressed(KeyEvent e){}
  public void keyTyped(KeyEvent e){}



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:41:32+00:00Added an answer on June 3, 2026 at 1:41 am

    The problem here is that your rect field is never set to anything so it stays as null. Calling rect.drawRect will cause the NullPointerException you’re seeing.

    If I remember correctly, Swing Graphics objects don’t really like you painting on them when they’re not expecting you to be doing any painting. I would therefore recommend against stashing the Graphics object you get during a call to paint() in a field such as rect. If you want to repaint part of the window, it’s better to tell Swing what part of the window needs repainting and then let it call your paint() method.

    Within your mouseClicked() method, I removed the call to rect.fillRect() and moved the call to repaint() to the end of the method. I also modified the paint() method to draw a filled rectangle if the cell was alive and an unfilled one otherwise. After doing this, your code appeared to work, in that I could click on some cells and they would turn black.

    I have a few suggestions for improvements to your code. I’ll leave the last two as exercises for you:

    • I’d recommend adding the line j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); to main(). This line makes the application quit properly when you close the window.
    • At the moment, your code is repainting the entire 75 × 75 grid every time a single cell changes. It should be possible to change your code so that it repaints only the changed cell. You can pass a Rectangle to the repaint() method, which tells Swing ‘only this part of my component needs to be repainted’. In the paint method, you can get hold of this rectangle using the getClipBounds() method of the Graphics class and use it to determine which cell or cells to repaint.
    • drawRect only draws the outline of a rectangle. If a cell dies, your paint method won’t clear the existing black rectangle from the grid. You could fix this by drawing dead cells as a white filled rectangle with a black outline rectangle on top.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.