title: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException [NetBeans]
body: <p>I'm trying to call draw() method from Agent, Obstacle and MovingEntity classes but I get this error: (I have only error with Obstacle class)</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at java.awt.Graphics.drawPolygon(Graphics.java:732)
      at Obstacle.draw(Obstacle.java:28)
      at World.draw(World.java:121)
      at MyWorldWindow.paint(MyWorldWindow.java:23)
      at javax.swing.RepaintManager$3.run(RepaintManager.java:819)
      at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
      at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
      at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
      at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
      at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
      at java.awt.EventQueue.access$200(EventQueue.java:103)
      at java.awt.EventQueue$3.run(EventQueue.java:694)
      at java.awt.EventQueue$3.run(EventQueue.java:692)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  </code></pre>
  
  <p>world.java:</p>
  
  <pre><code>import java.util.*;
  import java.awt.Color; 
  
  public class World {
  
      int W,H;
      int margin;
      int N;
  
      LinkedList entities;
  
      public World() {
          W = 800;
          H = 600;
          margin = 50;
  
          entities = new LinkedList();
  
          N = 10;            
          for(int i=0; i&lt;N; i++) {            
              Agent e = new Agent ( randomPointInsideWorld(), randomPointInsideWorld(), this );
              entities.add( e );
          }
  
          for(int i=0; i&lt;4; i++) {            
              Obstacle o = new Obstacle ( randomPointInsideWorld(), this );
              entities.add( o );    
          }
  
          for(int i=0; i&lt;3; i++) {            
              MovingEntity me = new MovingEntity ( randomPointInsideWorld(), this );
              entities.add( me );    
          }
      }
  
      int getW() { return W; }
      int getH() { return H; }
  
      Vec2D randomPointInsideWorld() {        
          double x = margin + Math.random() * (W - 2*margin);
          double y = margin + Math.random() * (H - 2*margin); 
          Vec2D p = new Vec2D(x,y);
          return p;
      }
  
      void processCollisions() {        
            for(int i=0; i&lt;N; i++) {
                Entity ei = (Entity) entities.get(i);
                ei.setCollides(false);
            }
  
            for(int i=0; i&lt;N; i++) {
                Entity ei = (Entity) entities.get(i);
                ei.setCollides(false);
                for(int j=0; j&lt;N; j++) {  
                    if(i == j) continue;
                    Entity ej = (Entity) entities.get(j);
                    if(ei.collides(ej)) {
                        ei.setCollides(true);
                    }
                }
            }
      }
  
      void removeOutsiders() {
          for(int i=0; i&lt;entities.size(); i++) {
              Entity ei = (Entity) entities.get(i);            
              if((ei.getPos().getX() &lt; 0) ||
                 (ei.getPos().getX() &gt; W) ||
                 (ei.getPos().getY() &lt; 0) ||
                 (ei.getPos().getY() &gt; H))
              {
                  entities.remove(ei);
              }
          }
  
          N = entities.size();
      }
  
      void update() {              
          removeOutsiders();
          processCollisions();
  
          for(int i=0; i&lt;N; i++) {
              Entity ei = (Entity) entities.get(i);
              ei.update();
  
              if ( ei instanceof Agent ) {
  
                  Agent a = (Agent) ei;
                  if ( a.objReached() ) {
                      a.setObj(randomPointInsideWorld() );
                  }
              }
          }
      }
  
      public void run(int steps) {
          for(int i = 0; i&lt;steps; i++) update();
      }
  
      public void draw( java.awt.Graphics g ) {
          N = entities.size(); 
          for(int i=0; i&lt;N; i++) {
              Entity ei = (Entity) entities.get(i);
              Vec2D p = ei.getPos();
              double wa = 2;
              g.setColor(Color.BLUE);    
              g.fillOval((int)(p.getX() - wa), (int)(p.getY() - wa), 2*(int)wa, 2*(int)wa);
              if ( ei instanceof Agent ) {
                  Agent a = (Agent) ei;
                  a.draw(g);
              }
              if ( ei instanceof MovingEntity ) {
                  MovingEntity me = (MovingEntity) ei;
                  me.draw(g);
              }
              if ( ei instanceof Obstacle ) {
                  Obstacle obst = (Obstacle) ei;
                  obst.draw(g);
              }
          }
      }
  }
  </code></pre>
  
  <p>The error comes out only when I call the draw method of the Obstacle class</p>
  
  <p>Obstacle Class:</p>
  
  <pre><code>  import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Polygon;
  
   // Clase Obstacle Hereda atributos de la Clase Entity
   public class Obstacle extends Entity {
  
        private MyPolygon p;
  
        // Método Constructor Obstacle
        public Obstacle (Vec2D p, World wi) {
            super (p, wi);  
            MyPolygon p1 = new MyPolygon();
            p1.randomPolygon();
            p1.center();
            p1.translate((int)pos.getX(), (int)pos.getY());
        }
  
       // Getter Obstacle
       public MyPolygon getObstacle() {
            return p;
        }   
  
       public void draw(Graphics g) {
           if(bCollides)g.setColor(Color.RED);
           else         g.setColor(Color.ORANGE);
           g.drawPolygon(p);
       }
  }
  </code></pre>
  
  <p>I'm newbie in Java, but I have a feeling it has to do with the fact that some value is not initialized properly in the Obstacle class ... can it be?</p>
  
  <p>Thank you all.</p>