Solved

NullPointer Exception when i try to access array..

Posted on 2003-11-30
3
225 Views
Last Modified: 2011-04-14
Hi there...
I ve got the following class where the array is initialized in the class' constructor..

public class Board {
  public Cell board[][] ;
  Board(int x)
  {
board=new Cell[x][x] ;
  }
Is the above correct?

When i try to access the board array after i declare a Board object
for example: a.board[0][1].someMethod()

I get java.lang.NullPointerException..

I am a java newb so any help would be appreciated..




0
Comment
Question by:gatas
  • 2
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9846470
YOu have to fill all the array elements too with Cell objects
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9846494

for (int i=0; i<board.length; i++)
   for (int j=0; j<board[i].length; j++)
      board[i][j] = new Cell();
0
 
LVL 92

Expert Comment

by:objects
ID: 9847156
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 62
jboss wildfly 10.1 10 234
JavaFX TableView not displaying correctly 3 58
servlet and mdb, jms error 1 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question