title: How to avoid NullPointerException in ArrayList with 2 threads
body: <p>I'm creating an Android game and I encountered a problem. I have 2 threads: OpenGL thread and game loop. I use ArrayLists to store my renderable objects and here's the problem. Sometimes I need to delete these objects in game thread and sometimes it causes OpenGL thread to get NullPointerException because, as I understand, it goes through the collection and suddenly an element gets deleted. I think I should let OpenGL thread delete objects but is there any other solution and what if game loop will start to get null elements? Maybe I can make synchronize threads somehow though I still don't understand how "synchronized" works. Thanks.</p>
  
  <p>Will this help? And should I put it everwhere I do something with my ArrayList? </p>
  
  <pre><code>synchronized (arrayListName) 
  { 
      arrayListName.remove(object); 
  }
  </code></pre>