Accessing two subclass within one class

Hello,

i dont know if its even possible, but here is my problem.

My goal is find the best shape for my IRL construction and i would like to use genetic algorithm (yeah… high hopes) and for that so i want to make a random construction, that i would coppy around and eventualy end up with the best one.

For that i wanted to make a class Construction and there i would do all the calculations and all.

My problem is, that my construction is made from rods (lines) and joints (points), each with specific parameters so i made a class for rods and joints.

I have already made a script for one construction, but when i tried to copy them it gave me an error so i tried to do it step by step and eventualy i found out, that if im trying to do something in rods class with joints class informations, it says, that the joint class cant be found.

Here is the code: https://pastebin.com/PuP7KK8y

The error says:
“NullPointerException
Could not run the sketch (Target VM failed to initialize).
For more information, read revisions.txt and Help ? Troubleshooting.”

and point to:
if (xa == most.joint_array.get(i).x & za == most.joint_array.get(i).z & xb == most.joint_array.get(j).x & zb == most.joint_array.get(j).z) {

On one hand i think it should be possible, but on other hand its not “global variable?” so if im working in “prut”(those are rods) class processing cant see outside of it.

I already though about some dirty solution with 3 global (i hope its called global) arrays with one being joints, other rods and the 3rd being the construction itself.

I have shortened the script and tweaked it a bit so its not in my native language and hopefuly easier to read. Each line represent new tab.

Thanks everyone who at less read it :smiley:

1 Like

you’re referencing bridge in the Prut class without passing it to the activation function. i think if you change these two lines you should be right

change

prut_array.get(i).aktivation();

to

prut_array.get(i).aktivation(this);

and

void aktivation() {

to

void aktivation(Bridge bridge) {

though you might want to just build the pruts and then in another iteration activate them.

1 Like

Sweet it worked! So if i understand correctly it works like that:

Because i have class withing the class, lets say sub-class withing a class, and i referencing things in sub-class from a class, i have to connect them, so i use “this” keyword that will link them together, and there is (Bridge bridge) because im referencing to Bridge class clalled “bridge”

Bridge bridge;

void setup() {

size(1000, 700);

frameRate(60);

translate((width / 2) - (750 / 2), height / 2);

bridge = new Bridge(5, 9);

}

is that right?

Im trying to understand it so i can use it in future with some background

basically you were trying to use bridge before it had been instantiated. i would probably just use the Prut class like a struct and move the activation functions guts into where the activation was called from bridge. anyways glad it worked.

Thank you very much. I though about it and i will redo the whole data structure.
I will make a Bridge class with prut sub-class with joint sub-sub-class, because my bridge is made with pruts, and those are made with reference to a joint coordinates. Maybe thats whats what you were saying :smiley: and then i shouldnt have any issue with references.