Solved

NullPointer Exception when i try to access array..

Posted on 2003-11-30
3
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 68
SequenceInputStream example 3 38
Java program running SQL query 5 67
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

751 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