0

How do I access an array of objects inside of an array of objects?

my code:

private boolean intersect(Polygon[] polygons, Line[] the_path, int i, int j)
{
   int k = 0;
   boolean intersect;

   if(intersect == true)
   {
       for(i = 0; i < polygons.length; i++)
        for(j = 0; j < polygons._lines.length; j++)
           for(k = 0; k < the_path.length; k++)
           intersect = polygons._lines[j].intersect(the_path[k]);
   } 

   return intersect;
}

The intersect method in the array of lines returns a boolean, but there is a separate array of line objects in each of the polygons....how do I go about accessing that method? (note..I don't know if this exact code will do what I want yet, but either way I need to be able to access that method)

1 Answer 1

1

I think you accidentally left out the index into polygons (e.g. polygons[i]). Also, currently you have intersect being assigned the value of intersect() which means it is overwriting any other values given to the boolean intersect in previous loop iterations. I have added an if statement that will break out of the function immediately if that case if found instead. However, you could instead do something like intersect = intersect || ... .intersect() if you want to keep that variable.

Try this:

private boolean intersect(Polygon[] polygons, Line[] the_path, int i, int j) {
  int k = 0;

  for (i = 0; i < polygons.length; i++) {
    for (j = 0; j < polygons[i]._lines.length; j++) {
      for (k = 0; k < the_path.length; k++) {
        if (polygons[i]._lines[j].intersect(the_path[k])) {
          return true;
        }
      }
    }
  }

  return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that worked....how my only issue is figuring out where the null pointer exception is occurring and why -_-
@user2489837 - No problem, happy to help :). Perhaps if you post your null pointer exception call stack we can help debug your issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.