File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes a 2D array in a class constructot Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "a 2D array in a class constructot" Watch "a 2D array in a class constructot" New topic
Author

a 2D array in a class constructot

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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];
}
}
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183

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

Joined: Sep 10, 2001
Posts: 285
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;
}
}
}
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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:


"I'm not back." - Bill Harding, Twister
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 05, 2002
Posts: 787
Anthony:
In that case, use "this" key word to refer
to instance variable:
this.pos = new int[s][s];
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a 2D array in a class constructot
 
Similar Threads
Association ,Composition, Aggregation
Association Classes
Association mapping
any advice?
Stuck on manyToMany association