Adding parent class to arraylist with internal function

I’m trying to build a game where you connect different pipes to complete a rube-goldberg machine level, this class is meant to be a connector wire between two blocks. I’m having an issue within the internal findend() function which cannot add the class it is inside of into an arraylist, I know I’m doing this wrong, so how would I make this class add itself to an arraylist?

class connector {
  int inx, iny, nch, pch;
  String parent, child;
  connector aep;
  String[] payload;
  int tx, ty, sty, stx;
  connector(int _inx, int _iny, String _parent, String[] _payload, int _pch) {
    inx=_inx;
    iny=_iny;
    parent=_parent;
    payload=_payload;
    pch=_pch;
  }
  void dline() {
    fill(0);
    if (aep==null) {
      findend();
      line(inx, iny, mouseX, mouseY);
    } else {
      for (floebox i : elements) {
        if (i.id==child) {
          stx=i.spx;
          sty=i.spy+(15+nch*10);
        }
        if (i.id==parent) {
          tx=i.spx;
          ty=i.spy+(15+pch*10);
        }
      }
      line(tx, ty, stx, sty);
    }
    fill(255);
  }
  void findend() {
    if (mousePressed) {
      println("one");
      for (floebox i : elements) {
        println("two");
        for (int z=0; z<i.il; z++) {
          println("three");
          if (5>dist(mouseX, mouseY, i.spx, i.spy+(15+z*10))) {
            println("four");
            i.incon.add(z, this);//returns outofbounds, says that incon is of length zero and im trying to insert at 2
            child=i.id;
            nch=z;
          }
        }
      }
    }
  }
}

How is incon defined? Please, either give code cut down to the issue barebones, or all of it! .-.

1 Like
class connector {
  void findend() {
//i is the element accessed from a foreach loop, and incon is supposed to be an arraylist containing container classes
            i.incon.add(z, this);I'm trying to add this class to that arraylist
  }
}

Here’s a simplified version

We’re still gonna need a definition of what a floebox is so we can know what incon.add() does. When Architector says

he means we can’t help you because what you’re doing wrong might not be in the code you’re giving us, but in the implementation of the classes and functions you’ve written instead.

Sorry, I’m not too good at cutting down code… I didn’t redefine .add, its a built-in java function that allows you to add an element in at a given position, Z is meant to be the variable that stores the position. Anything else?

.add isn’t really the questionable part here.

Architector asked

and incon appears to be a member of object i which appears to be of type floebox

We don’t know what a floebox is, how it’s defined, or the type of any of its members.

You mentioned that .add is a built in java function but we still don’t know what type of object incon is, since .add is a very ambiguous name that is used in several built in Java classes.

Please take a look at the Guidelines—Tips on Asking Questions page and take special note to the following sections:

  • Is your problem isolated?
  • Can we run your code to see the same thing as you?
1 Like

I apologize for incoherence, Incon is an arraylist nested in the floebox class, Nothing special, just an empty arraylist defined at the start of a class, like this:

class floebox(){
[...]
  ArrayList<connector> incon;
  ArrayList<connector> outcon;
  String[] in, out, TEMPDUMMYPAYLOAD;
  int il, ol, spx, spy, swidth, xofs, yofs;
  floebox(blockmd b, int _spx, int _spy, String _id) {
    name=b.name;
    il=b.in.length;
    ol=b.out.length;
    spx=_spx;
    spy=_spy;
    swidth=int(textWidth(name))+50;
    id=_id;
    outcon=new ArrayList<connector>(ol);
    incon=new ArrayList<connector>(il);
  }
[...]
}

I’m really sorry about the example code, I write really messy spaghetti code and fix it afterwards, if It dosen’t peeve you too much, just tell me what I leave out

1 Like

That’s fine. I think this information here should make debugging this a lot easier. I’m a bit busy right now but if nobody picks this up soon I’ll take a look at it again later.

I think what you have here might be enough information to start looking at solutions.

Alright, no rush, I appreciate your kindness

I finally got to looking at your code but when I got everything together, I found an issue with trying to get these classes to understand each other. It seems that the definition of floebox is reliant on the definition of connector, but connector relies on floebox. You have a circular dependency in your code.

Also, floebox seems to rely on blockmd which I don’t have access to. name inside of floebox’s definition is also never defined, so its purpose cannot be accommodated for.

I’m going to reiterate once again to look at the Guidelines–Tips on Asking Questions sections labeled:

  • Is your problem isolated?
  • Can we run your code to see the same thing as you?

Final note: rereading your question which is specifically regarding the behavior of findend(), which has several issues keeping it from being testable on our end, I can see that your original problem revolved around adding elements to an ArrayList.

I would consider using ArrayList’s overloaded add() function that only takes in one operator: the item you’re adding. You don’t need to use an index to add an ArrayList to the next index.

i.incon.add(this);

It might solve your issue. I can’t be sure it will, as I don’t have enough information on your problem.

3 Likes