Solved

NullPointer Exception when i try to access array..

Posted on 2003-11-30
3
224 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 20
simple java question 3 55
Java JRE greater than 1.6 5 62
tomcat not starting 6 45
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

776 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