Skip to main content

Why this null pointer exception when using BufferStrategy with swing timer in Java?



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

}

}




Comments

  1. 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,

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    MLM2 ex = new MLM2();
    }
    });
    }

    ReplyDelete
  2. Attach the Java source code to your project so you can get more details.

    I 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

Java Urban Myths

Along the line of C++ Urban Myths and Perl Myths : What are the Java Urban Myths? That is, the ideas and conceptions about Java that are common but have no actual roots in reality . As a Java programmer, what ideas held by your fellow Java programmers have you had to disprove so often that you've come to believe they all learned at the feet of the same drunk old story-teller? Ideally, you would express these myths in a single sentence, and include an explanation of why they are false.

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...