Solved

NullPointer Exception when i try to access array..

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now