• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a 2D array in a class constructot

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is wrong with this code? It does not like the position for:
Association[][] positions = new positions[s][s];
/////////
public class Board

{
private int size;
private Association[][] positions;


public Board(int s)

{
size = s;
Association[][] positions = new positions[s][s];
}
}
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

should be:

positions has already been declared, so this line will create a 2D array of Association handles, which will all be initialized to null. So to use your array, you will then need:

[ August 08, 2002: Message edited by: Neil Laurance ]
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still get a null pointer exception...
Right at this line: pos[i][j]=j+1;
I even tried to init the array in my class constructor....

//////////////
private int size;
//private Association[][] positions;
private int[][] pos;


public Board(int s)

{

size = s;
//Association[][] positions = new Association[s][s];
int[][] pos = new int[s][s];
for(int i=0; i<s; i++)
for(int j=0; j<s; j++)
pos[i][j] = 0;
setupBoard();
System.out.println("2!!!");
}


public void setupBoard()

{
try
{
for (int i = 0; i!=size; i++)
for (int j = 0; j!=size; j++)
{
if (i < size/4 )
{
System.out.println("~!!!i:" + i);
System.out.println("~!!!j:" + j);
pos[i][j]=j+1;
System.out.println("~~~~~~~!!!");
}
if (i> (size - size/4))
{
System.out.println("!!!");
pos[i][j]=(j+1) * -1;
}
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two different pos variables - an instance variable, and a local variable in your constructor. Although they share the same name, they have nothing to do with one another (though I expect you want them to). If you want pos to be an instance variable only, don't redeclare it in the constructor - just use it:
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wont know the size until I call the constructor is my problem... SO where wil I declare the size? When I try to in the constructor it seems to make another instance of it.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there:
This seems like an interesting problem. The array pos is defined within class. But the object is actual created inside the scope of a method. Seems like this object goes out of scope when control goes out of this method. So I modified your code and declare and created the array object pos within class (outside of method). And did not get nullpointererror. Note, I have modified your code little bit so that I can see output on one screen. Here is the code:

class Board {
private int size;
private int[][] pos = new int[2][2];
public Board(int s) {
size = s;
// int[][] pos = new int[s][s];
for(int i=0; i<s; i++)
for(int j=0; j<s; j++) {
pos[i][j] = 0;
System.out.println(i+" "+j+" "+pos[i][j]);
}
setupBoard();
System.out.println("2!!!");
}

public void setupBoard() {
for (int i = 0; i!=size; i++)
for (int j = 0; j!=size; j++) {
if (i < size/2 ) {
System.out.println(i+" "+j+" "+pos[i][j]);
pos[i][j]=j+1;
System.out.println(i+" "+j+" "+pos[i][j]);
}
if (i> (size - size/4)) {
System.out.println("!!!");
pos[i][j]=(j+1) * -1;
}
}
}
}

public class Board_test {
public static void main(String[] args) {
Board B = new Board(2);
}
}
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anthony:
In that case, use "this" key word to refer
to instance variable:
this.pos = new int[s][s];
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry forgot to paste the code. This is working
fine:

class Board {
private int size;
private int[][] pos;
public Board(int s) {
size = s;
this.pos = new int[s][s];
for(int i=0; i<s; i++)
for(int j=0; j<s; j++) {
pos[i][j] = 0;
System.out.println(i+" "+j+" "+pos[i][j]);
}
setupBoard();
System.out.println("2!!!");
}

public void setupBoard() {
for (int i = 0; i!=size; i++)
for (int j = 0; j!=size; j++) {
if (i < size/2 ) {
System.out.println(i+" "+j+" "+pos[i][j]);
pos[i][j]=j+1;
System.out.println(i+" "+j+" "+pos[i][j]);
}
if (i> (size - size/4)) {
System.out.println("!!!");
pos[i][j]=(j+1) * -1;
}
}
}
}

public class Board_test {
public static void main(String[] args) {
Board B = new Board(2);
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic