nullpointerexception
投げられる理由を理解するのに問題があります。すべてが適切に描画されるため、実際には何にも影響しません (少なくとも私はそうは思いません) が、エラー メッセージは好きではありません。
ご覧のとおり、paint() は this.game.paint(g2) を呼び出します。デバッグを試みるために、this.game.print 内のすべてがコメント アウトされています。この関数は何もしないので、問題の一部ではないはずです。
public class Application extends JFrame {
private Game game;///<stores the rules for the game and facilitates interactions
public Application() throws RoyException {
this.game = new Game(this, 2);
}
// @Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
try{
this.game.paint(g2);
}catch(Exception e)
{
System.out.println("CAUGHT " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
Application app = new Application();
} catch (RoyException e) {
System.out.println("An error occurred while running the program: " + e.getMessage());
System.out.print("Stack trace: ");
e.getStackTrace();
}
}
}
そして、これがスタックトレースです - Application.java:153 is this.game.paint(g2);
CAUGHT null java.lang.NullPointerException
at bullshitakemushrooms.Application.paint(Application.java:153)
Game created.
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)