I noted this was my third post today, please bare with me. :
When I try to run my program (see below) it sometimes runs without problems (except for it’s slow because of the use of a thread for frame rendering) but sometimes it crashes just when it has started. Here’s the trace. The line number aren’t correct anymore but’s it’s in the call to renderFrame(). It seems like either the buffer’s draw graphics or the buffer itself got lost.
java.lang.NullPointerException
at wizards.Wizards.renderFrame(Wizards.java:137)
at wizards.Wizards.paint(Wizards.java:76)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Here’s the code somewhat shorten to just include points of importance…
/*
* Created on Apr 26, 2003
*/
package wizards;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferStrategy;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import wizards.gui.AnimationThread;
import wizards.gui.ImageLibrary;
import wizards.map.Map;
import wizards.map.Scenery;
/**
* @author Johan Tibell
*/
public class Wizards extends JFrame
{
public static int MAP_WIDTH = 15;
public static int MAP_HEIGHT = 15;
public static int TILE_SIZE = 32;
private BufferStrategy strategy;
private GameController controller;
private Map map;
public Wizards()
{
// Set the window's title.
super("Wizards");
// Set the application to exit on window closure.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the game's controller to listen for mouse clicks.
controller = new GameController(this);
addMouseListener(controller);
// Create an image library and load it with images.
ImageLibrary library = ImageLibrary.getLibrary();
library.addDirectory("images");
// Set the window's size.
setSize(MAP_WIDTH * TILE_SIZE, MAP_HEIGHT * TILE_SIZE);
setVisible(true);
// Create a double buffer.
try {
createBufferStrategy(2);
} catch (IllegalStateException e) {
// TODO: Tidy upp error handeling.
System.out.println(e.getMessage());
System.exit(1);
}
strategy = getBufferStrategy();
// Initialize the game.
initializeGame();
new AnimationThread(this).start();
}
public void paint(Graphics g)
{
super.paint(g);
renderFrame();
}
/**
* Initialize the game parameters.
*/
public void initializeGame()
{
// Create a new map.
map = new Map(MAP_WIDTH, MAP_HEIGHT, TILE_SIZE);
// Removed alot of game specific code...
}
/**
* Render the next frame.
*/
public void renderFrame()
{
// If we lost our buffer recreate it.
if (strategy == null)
{
createBufferStrategy(2);
strategy = getBufferStrategy();
}
if (!strategy.contentsLost())
{
// Get a buffer to draw on.
Graphics g = strategy.getDrawGraphics();
// Draw the map.
map.draw(g);
// Removed alot of game specific image drawing (a couple of BufferedImage:s)
// Show the frame on the screen.
try {
strategy.show();
} catch (IllegalStateException e) {
return;
}
g.dispose();
}
}
/**
* Entry point of application. Creates a window and starts the application.
* @param args the command line arguments
*/
public static void main(String[] args)
{
new Wizards();
}
}