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

  • Home
  • SEARCH
  • 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 699023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:20:24+00:00 2026-05-14T03:20:24+00:00

All, I am trying to code a Connect4 game. For this, I have created

  • 0

All,

I am trying to code a Connect4 game. For this, I have created a P4Game class and a P4Board class which represents the i X j dimensions of the Connect4 board.

In P4Game, I have the following:

public class P4Game{
  //INSTANCE VARIABLES
  private int nbLines;
  private int nbColumns;
  private P4Board [][] position;  

//CONSTRUCTOR  
  public P4Game(int nbLines, int nbColumns){
    this.nbColumns = nbColumns;
    this.nbLines = nbLines;
    P4Board [][] position = new P4Board [nbLines][nbColumns]; //Creates the table to receive the instances of the P4Board object.*/
    for (int i=0; i<nbLines; i++){
      for (int j=0; j<nbColumns; j++){
        this.position[i][j] = new P4Board(i,j); //Meant to create each object at (line=i, column=j)
      }
    } 
  }

This causes a NullPointerException in the nested loops where I mention this.position[i][j]. I reference those objects in other methods of this class so I need them to be instance variables. I suppose the exception is due to the fact that I have not listed the table element position[i][j] as an instance variable at the beginning of the class.

my question to people here is (1) is my assumption correct, and if so (2) what would be the syntax to declare instance variables of this form?

Thank you all for your help with what I realize is a very basic question. Hopefully it will also benefit other newbies.

Cheers,

JDelage

  • 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-05-14T03:20:25+00:00Added an answer on May 14, 2026 at 3:20 am

    See added comment inlined… You code is fine except for one little detail, where you’re creating a new position variable where you actually mean to use the instance variable.

    public class P4Game{
        //INSTANCE VARIABLES
        private int nbLines;
        private int nbColumns;
        private P4Board [][] position;  
    
        //CONSTRUCTOR  
        public P4Jeu(int nbLines, int nbColumns){
            this.nbColumns = nbColumns;
            this.nbLines = nbLines;
            // You're creating a LOCAL variable called position here if you don't comment what's commented:.
            /*P4Board [][] */position = new P4Board [nbLines][nbColumns]; //Creates the table to receive the instances of the P4Board object.*/
            for (int i=0; i<nbLines; i++){
                for (int j=0; j<nbColumns; j++){
                    this.position[i][j] = new P4Board(i,j); //Meant to create each object at (line=i, column=j)
                }
            } 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to get this code to work for hours! All I need
I've been trying to code a Perl script to substitute some text on all
I'm trying to abstract out all database code into a separate library and then
Trying to keep all the presentation stuff in the xhtml on this project and
I am trying to select all the records from a sqlite db I have
All I am trying to do is XmlSerializer serializer = new XmlSerializer(typeof(Stack<int>)); and I
All I am currently trying implement something along the lines of dim l_stuff as
I'm trying to redirect all requests to my domain to another domain using mod_rewrite
I'm trying to fully understand all of Haskell's concepts. In what ways are algebraic
I'm trying to iterate all the controls on a form and enable ClearType font

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.