Null pointer exception error

edited February 2016 in Questions about Code

Hello,

I am having difficulty sorting a null pointer exception error that appears after several minutes on the following code:

  void killCreatures() {
    for (int i = 0; i < predatorAgents.size(); i++) {
      PredatorAgent a = predatorAgents.get(i);
      for (int j = 0; j < creatureAgents.size(); j++) {
        CreatureAgent b = creatureAgents.get(j);
        float ab = a.location.dist(b.location); //NULL POINTER EXCEPTION
        if (ab < 50) {
          b.alive = false;
        }
      }
    }
  }

The sketch essentially contains 2 classes, one for predators and one for creatures.

When the predators get close to the creatures they 'kill' them. The above code is found inside the predator class. and at the end of the draw loop I have the following, removing the dead predators and creatures:

     for (int i=((predatorAgents.size())-1); i >(-1); i--) { 
        PredatorAgent b = predatorAgents.get(i);
        if (b.alive == false) {
          predatorAgents.remove(i);
          allPredators.remove(i);
        }
      }


      for (int i = agents.size()-1; i>-1; i--) {
        CreatureAgent a = creatureAgents.get(i); 
        if (a.alive == false) {
          creatureAgents.remove(i); 
        }
      }

Notes to mention:

As mentioned, predators kill creatures if they are too close If creatures get too close then one of them is killed Both creatures and predators have a life span and die when they reach a certain 'age'

The error is occurring I assume because part of one of the arraylists is becoming unreachable (maybe being killed while the loop is in progress?)

If I haven't supplied enough code then I can give more - maybe more information about the draw loop and the class functions will be needed, but it isn't a very complex sketch.

If anyone can offer a solution to preventing this error then that would be great! (I'm also aware that this might not be very well written code but it serves it;s purpose for now).

Answers

  • you don't check whether creatureAgents is empty ?

    are we sure location is set in both classes?

  • Hi,

    The error is happening after around 10 minutes when there are lots of 'creatureAgents' on the screen, but no I don't think I am checking - I did use while loop that made sure that the location.x && location.y of the creatureAgent is greater than 0, but that didn't work and I'm new to classes so I'm unsure of how to tackle this problem.

  • what do you mean?

    he is already doing it backward?

  • edited February 2016

    He is already doing it backward?

    Oops, my bad! 3:-O But at least that link got lotsa good examples about it! :D
    Back to business, I find these statements suspicious:

    predatorAgents.remove(i);
    allPredators.remove(i);
    

    Are predatorAgents & allPredators respective List containers sharing same indices by any chance??? @-)

  • well spotted

  • edited February 2016

    when your npe is in you code block 1 above, in line 6:

    use println before that line

    println (b.location.x);
    
    println (a.location.x);
    

    etc.

    thus you know what exactly causes the npe

  • Hi,

    predatorAgents.remove(i);
    allPredators.remove(i);
    

    Refer to two array lists - one deals with the instances of the class and the other contains text - essentially each predator is assigned a piece of text that is stored in the separate array list.

    I will have a go at the above suggestion.

    Thanks!

  • It'd be much easier to manage that if you expanded PredatorAgent to have that piece of info as well.
    That way you'd only need 1 List container. *-:)

  • I'm not exactly sure how I would do that, would I use a 2D arrayList? It would need to contain a string and a pVector.

  • edited February 2016

    Refer to two array lists - one deals with the instances of the class and the other contains text - essentially each predator is assigned a piece of text that is stored in the separate array list.

    what gotoloop means is that you can bring the text from the 2nd ArrayList into the first mentioned class.

    That's what classes are for: to pack the data for one item into one package.

    predatorAgentsClass {
    
        float x;
        float y;
    
        String text;
    
        // ...................
    
    }
    
  • Ah I see, i'll give that a go, I'm still having trouble with the npe and using the println method causes processing to freeze as it is having to print out so much data

  • if (a==null) 
       println("a was null. wth?"); 
    if(a.location==null)
       println("a.location was null. I don't know what's going on."); 
    
  • Thanks, so it is b that is null.. b refers to the creature being 'killed' so maybe the size of the array list storing the creatures is changing when the above nested for loops are in progress?

  • b or b.location?

  • As said, maybe creatureAgents is empty ?

  • Answer ✓

    but anyway

    just execute the lines only when b!=null

    if(b!=null) {

    .....

  • Thats what i've done and it seems to work, i'll leave it running for a while just to be sure. Thanks!

Sign In or Register to comment.