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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.