简体   繁体   中英

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException [NetBeans]

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)

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)

world.java:

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<N; i++) {            
            Agent e = new Agent ( randomPointInsideWorld(), randomPointInsideWorld(), this );
            entities.add( e );
        }

        for(int i=0; i<4; i++) {            
            Obstacle o = new Obstacle ( randomPointInsideWorld(), this );
            entities.add( o );    
        }

        for(int i=0; i<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<N; i++) {
              Entity ei = (Entity) entities.get(i);
              ei.setCollides(false);
          }

          for(int i=0; i<N; i++) {
              Entity ei = (Entity) entities.get(i);
              ei.setCollides(false);
              for(int j=0; j<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<entities.size(); i++) {
            Entity ei = (Entity) entities.get(i);            
            if((ei.getPos().getX() < 0) ||
               (ei.getPos().getX() > W) ||
               (ei.getPos().getY() < 0) ||
               (ei.getPos().getY() > H))
            {
                entities.remove(ei);
            }
        }

        N = entities.size();
    }

    void update() {              
        removeOutsiders();
        processCollisions();

        for(int i=0; i<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<steps; i++) update();
    }

    public void draw( java.awt.Graphics g ) {
        N = entities.size(); 
        for(int i=0; i<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);
            }
        }
    }
}

The error comes out only when I call the draw method of the Obstacle class

Obstacle Class:

  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);
     }
}

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?

Thank you all.

private MyPolygon p; is not initialized in your Obstacle.java

Change

 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());
  }

to

 public Obstacle (Vec2D v, World wi) {
      super (v, wi);  
      p = new MyPolygon();
      p.randomPolygon();
      p.center();
      p.translate((int)pos.getX(), (int)pos.getY());
  }

更改为

private MyPolygon p = new MyPolygon();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM