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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex