我为“面向对象的Java编程”课程做了模块6分配。我实现了当用户在地震标记上按下鼠标右键时,地图将显示具有相似幅度的地震标记的功能。
在一个类中实现它可以正常工作。但是当我尝试使用该模式时,我得到一个空指针异常。 EarthQuakeCityMap类如下所示:
public class EarthquakeCityMap extends PApplet
{
// The map
protected UnfoldingMap map;
// Markers for each earthquake
protected List<Marker> quakeMarkers;
// NEW IN MODULE 5
protected CommonMarker lastClicked;
private MouseClickBehavior mouse_behavior_;
@Override
public void mouseClicked()
{
if(mouseButton == LEFT) LeftClick();
if(mouseButton == RIGHT) RightClick();
}
private void RightClick()
{
SetClickBehavior(new RightClick());
mouse_behavior_.MouseClick();
}
public void SetClickBehavior(MouseClickBehavior obj_bahavior){ mouse_behavior_ = obj_bahavior; }
}
MouseClickBehavior接口如下:
package module6;
public interface MouseClickBehavior { void MouseClick(); }
发生异常的RightClick类是:
public class RightClick implements MouseClickBehavior
{
public RightClick()
{
map_ = new EarthquakeCityMap();
magnitude_ = -2.0f;
begin_ = end_ = 0.0f;
}
@Override
public void MouseClick()
{
CheckEarthquakesForMagnitude();
}
private void CheckEarthquakesForMagnitude()
{
if (map_.lastClicked != null) {
map_.unhideMarkers();
map_.lastClicked = null;
}
else {
for(Marker m : map_.quakeMarkers)
{
EarthquakeMarker marker = (EarthquakeMarker) m;
if (!marker.isHidden() && marker.isInside(map_.map, map_.mouseX, map_.mouseY))
{
magnitude_ = marker.getMagnitude();
map_.lastClicked = marker;
break;
}
}
ShowMagnitudeMarkers();
}
}
private EarthquakeCityMap map_;
private float magnitude_;
}
最后是堆栈跟踪
Exception in thread "Animation Thread" java.lang.NullPointerException
at RightClick.CheckEarthquakesForMagnitude(RightClick.java:25)
at RightClick.MouseClick(RightClick.java:15)
at EarthquakeCityMap.RightClick(EarthquakeCityMap.java:108)
at EarthquakeCityMap.mouseClicked(EarthquakeCityMap.java:102)
at processing.core.PApplet.mouseClicked(PApplet.java:3149)
at processing.core.PApplet.handleMouseEvent(PApplet.java:2842)
at processing.core.PApplet.dequeueEvents(PApplet.java:2729)
at processing.core.PApplet.handleDraw(PApplet.java:2401)
at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:873)
at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:690)
at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:672)
at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1383)
at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1277)
at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:1131)
at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1394)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:301)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
我只包含了我认为与我遇到的问题相关的代码。如果有人可以向我解释为什么我得到异常以及如何实现行为而不会使EarthquakeCityMap膨胀,那将会很好。
提前致谢。
更新: 感谢所有的评论。我对我的代码进行了一些更改,但是在这里发布的内容太多了。如果有人有兴趣查看代码,我已经创建了一个repo。链接为Github repo
更改代码并调试代码后,我注意到当用户右键单击地图时,初始化了一个新的RightClick对象,但在构造函数完成后,他的成员变量map_被初始化为null。这就是我获得NPE的原因。不知道为什么。感谢我能得到的任何帮助。