#NullPointerException Creating new objects in ArrayList of class members
22 messages ยท Page 1 of 1 (latest)
It looks to me that your problem occurs because the arrayList is null. This means it has nothing in it. It is not even zero.
I see this class defines an array of marbles called hole.
class Hole {
ArrayList<Marble> hole;
Hole(){
hole = new ArrayList<Marble>();
}
}```
This is your marble class, but I dont see a method that adds a marble to the list.
An array list can hold an object or in your case a marble.
class Marble {
PVector offset;
color c;
//byte id;
Marble() {
colorMode(HSB);
c = color(5.0 * ceil(random(48)) , 255 , 127);
//id = in_id;
offset.set(random(-1.0,1.0),random(-1.0,1.0));
offset.setMag(random(1.0));
}
}```
You define its color and position but you dont display or create it.
I am unsure what you are doing with your code.
What does class Wood do?
The Hole class doesnt seem to be doing anything. It just defines and ArrayList of marble objects.
Your classes have constructors but no methods so their calls arent doing anything.
This if statement is trying to evaluate two arrays. wood and hole. Im not sure if this syntax is correct.
if (wood[0][index].hole.size()==0)```
If I were you, I would scale back and try to do one step at a time.
I don't think you even need a class to do what you want.
I always ask a guy named Loud_Cat (he/they). He is better at explaining than I am.
He always answers me if I ping him.
Thank you @slow dove
Yeah, this set of classes actually does serve a purpose even though there aren't any nethods for Marble and Hole.
The Wood class encapsulates the purpose and role of a Mancala wooden play-board.
It is the one class that along with void setup() { ... void draw() { ...
enable the play of the game.
I have had another version with virtually no classes but my end goal is to enable moving each seperate uniquely colored marble in the direction and strictly defined rules of
the spiritual ethnic and iconic game.
@flint kraken Could you please look at a way to help me with initializing the contents of the Array[][] -> Hole class objects -> ArrayList variable -> Marble class objects?
Thank you Mick and Cat!
@sterile mirage I am getting a homework vibe from this and we are not supposed to do you homework for you.
When I said you should scale back and take one step at a time, I meant you may have to work on initializing your arrays.
The code examples above have a few holes in them.
If you bring an attempt at doing the work someone in this sever can point you in the right direction. Asking Loud Cat to do it for you seems inappropriate for homework.
This is no homework. I can't prove this entirely without self doxxing but I can minimize my question and speficy I just want function init to work.
I have read and agreed to the rules before posting
My dad who needlessly gave me the assignment fixed the error via iterating over every hole in the Wood() constructor this way
class Wood {
Hole wood[][];
Wood() {
wood = new Hole[2][6];
for(byte i=0;i<6;i++){
for(byte j=0;j<2;j++){
//Hole test[] = wood[1];
wood[j][i] = new Hole();
}
}
} ...