# Accessing two subclass within one class

**URL:** <https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457>\
**Category:** Coding Questions\
**Created:** [February 16, 2019, 6:12pm UTC](https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457 "2019-02-16T18:12:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feltopi](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Feltopi](https://discourse.processing.org/u/Feltopi)\
**Post date:** [February 16, 2019, 6:12pm UTC](https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457/1 "2019-02-16T18:12:48Z")

</div>

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](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 😃

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [February 17, 2019, 2:50am UTC](https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457/2 "2019-02-17T02:50:38Z")

</div>

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_

```auto
prut_array.get(i).aktivation();

```

_to_

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

```

_and_

```auto
void aktivation() {

```

_to_

```auto
void aktivation(Bridge bridge) {

```

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

---

<div class="post-metadata">

**Author:** ![Feltopi](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Feltopi](https://discourse.processing.org/u/Feltopi)\
**Post date:** [February 17, 2019, 10:38pm UTC](https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457/3 "2019-02-17T22:38:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [February 17, 2019, 11:20pm UTC](https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457/4 "2019-02-17T23:20:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Feltopi](https://avatars.discourse-cdn.com/v4/letter/f/8c91f0/32.png) [@Feltopi](https://discourse.processing.org/u/Feltopi)\
**Post date:** [February 18, 2019, 10:05am UTC](https://discourse.processing.org/t/accessing-two-subclass-within-one-class/8457/5 "2019-02-18T10:05:49Z")

</div>

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 😃 and then i shouldnt have any issue with references.
