Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question

1 Answer

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;
}
share|improve this answer
Thank you, that worked....how my only issue is figuring out where the null pointer exception is occurring and why -_- – user2489837 11 hours ago
@user2489837 - No problem, happy to help :). Perhaps if you post your null pointer exception call stack we can help debug your issue. – Aiias 11 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.