gpt4 book ai didi

java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常 [NetBeans]

转载 作者:行者123 更新时间:2023-11-30 04:08:35 25 4
gpt4 key购买 nike

我尝试从 Agent、Obstacle 和 MovingEntity 类调用 draw() 方法,但收到此错误:(我只遇到 Obstacle 类错误)

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)

世界.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);
}
}
}
}

只有调用Obstacle类的draw方法时才会出现错误

障碍等级:

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

我是 Java 新手,但我有一种感觉,这与 Obstacle 类中某些值未正确初始化有关……可能吗?

谢谢大家。

最佳答案

将其更改为

private MyPolygon p = new MyPolygon();

关于java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException 中出现异常 [NetBeans],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20178041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com