Why might I be getting the following exception with the below code?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Component$BltBufferStrategy.showSubRegion(Unknown Source)
at java.awt.Component$BltSubRegionBufferStrategy.show(Unknown Source)
at javax.swing.BufferStrategyPaintManager.flushAccumulatedRegion(Unknown Source)
...
It only happens about every other time I run it but always right at the start. I'm using if(bs.contentLost()){...}
so I don't understand why it would be having problems.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.Random;
public class MLM2 extends JFrame implements ActionListener {
private final Timer timer = new Timer(20, this);
private Insets insets;
private BufferStrategy bs;
private BufferedImage drawing;
int lastW;
int lastH;
int pos = 0;
public static void main (String[] args) {
MLM2 ex = new MLM2();
}
public void actionPerformed(ActionEvent e) {
if(bs.contentsLost()) return;
Graphics2D g = null;
g = (Graphics2D)bs.getDrawGraphics();
int w = getWidth() - insets.left - insets.right;
int h = getHeight() - insets.top - insets.bottom;
if(w!=lastW || h!=lastH) {
drawing = (BufferedImage) this.createImage(w,h);
lastW = w;
lastH = h;
}
Graphics2D drawingBoard = drawing.createGraphics();
drawingBoard.setColor(Color.PINK);
drawingBoard.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
drawingBoard.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawingBoard.fillRect(0, 0, w, h);
drawingBoard.setColor(Color.RED);
drawingBoard.fillRect(pos, 100, 100, 100);
pos++;
if(pos>=w) pos=0;
g.drawImage(drawing, insets.left, insets.top, null);
drawingBoard.dispose();
if (!bs.contentsLost()) {
bs.show();
}
}
public MLM2() {
super();
setTitle("Mirror Land");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 420);
setLocationRelativeTo(null);
insets = this.getInsets();
int insetWide = insets.left + insets.right;
int insetTall = insets.top + insets.bottom;
setSize(getWidth() + insetWide, getHeight() + insetTall);
setVisible(true);
setIgnoreRepaint(true);
createBufferStrategy(2);
bs = getBufferStrategy();
drawing = (BufferedImage) this.createImage(getWidth(),getHeight());
timer.start();
}
}
Intermittent errors make me worry about concurrency issues. Make sure that you start his application on the Swing thread and see if that helps. In your main do something like,
ReplyDeletepublic static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MLM2 ex = new MLM2();
}
});
}
Attach the Java source code to your project so you can get more details.
ReplyDeleteI was able to compile and run your program just fine. I tried it several times, resized the window, minimized and maximized it, and moved it around. I could not get any errors.
Be sure you attach the Java source code to your project. Then you can get line numbers for the errors rather than "Unknown Source". When you look at the source, you can see what is causing the null pointer. This will help you determine the problem.
FYI - I ran it from my Eclipse development environment (Indigo SR1) on Red Hat Linux 6.2 (Santiago) in both regular and debug modes. I am using java 1.6.0_23-b05. So this might be something specific to your platform, your java version, or anything else in your environment.