Re: Enum list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
"Alex Mentis" <foo@invalid.invalid> writes:
Merciadri Luca wrote:
I've done some tests, and the fact is that
==
System.out.println(tableau.setOfCells[1][1]);
==
outputs null in the console (once tableau has been created). Neither
tableau nor tableau.setOfCells point to null, but
tableau.setOfCells[1][1] points to null.
I don't know if it is normal
Yes, that is normal. If you create an array of objects but don't
instantiate any objects of the type contained by the array and put them
into the array, then all the array cells will point to null until you
do so.
but it might be a cause
for what happens at L.25, which is
==
tableau.setOfCells[i][j].setState(State.DEAD);
==
As Joshua Cranmer has pointed out, yes, that is the cause.
Notes:
* I've got a Cell class where are some variables such as a State:
==
public class Cell {
private State state;
// ...
}
==
<...>
==
* I've got a setState method in the Cell class:
==
public void setState(State newState) { // mutator
state = newState;
}
==
* I've got a State enum defined in a State.java file:
==
public static enum State {
DEAD, LIVING
}
==
Any idea?
Does your Cell class have a constructor?
If so, my idea would be to call the constructor to construct a Cell
object at any array location where a cell should exist before trying to
call setState() on that cell.
Thanks for the answers. My Cell class has no constructor except the
default one (the one which is created at compilation). I created one
so that I can master it:
==
public Cell() {
}
==
I constructed a Cell object for each of the Tableau elements, in its
constructor; that is, I put:
==
public Tableau(int n, int s) { // constructor
setOfCells = new Cell[n][n];
dimension = n;
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
setOfCells[i][j] = new Cell();
}
}
generations = s;
}
==
(note that as I'm given n and s, I don't need to use this., but if I
need it some day, I'll use it).
It is good (i.e. what you meant)? And, if so, is it the right way to
do it, or is it better to put the loop with the setOfCells[i][j] = new
Cell() apart from the constructor, e.g. in another method? I thought
it would be nicer in the constructor because this loop contributes in
some way to the Tableau definition, that is, its construction.
- --
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- --
Love is not finding someone to live with; it's finding someone whom
you can't live without.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iEYEARECAAYFAk2O78AACgkQM0LLzLt8MhyKxwCfShvixYwdihhZLrjdBL9/qFho
NvYAnjheaHqz6s1n4c0/s0+IczQpUywE
=7Keo
-----END PGP SIGNATURE-----