[Solved]AWTException createBufferStratergy

Hi guys,

I am a newbie to Java. I thought of making a small game with it. (When I mean small I actually mean it.) I am creating a bufferStrategy with the Window class. I just fixed the illegal state exception and now I have a new problem. When the line is executed it throws the AWTException.

Window w = new Window(frame);
		
		w.setVisible(true);
		try{
		w.createBufferStrategy(2);
		}catch (Exception e){
			System.out.println(e.getLocalizedMessage());
			System.out.println(e.getMessage());
		}
		bs = w.getBufferStrategy();

And I can’t continue. This error occurs only when I debug the application in Eclipse. Other wise a NullPointerException occurs. It is basically due to this.
So could some one help me?

Regards,
Abhi2011

Bump

Please do not bump your own threads.

Can you show us how you initialize Frame? Nothing in this code looks wrong.
Also, it is best to call exc.printStackTrace() instead of those two printlns in your code.

If you knew what a nullpointerexception was, I think you would have more code included, so - a nullpointerexception is thrown when you try to utilize an object that has a value of ‘null’, or nothing. Usually when you forget to initialize an object. And, yes, please don’t bump your threads, especially after only 2 hours. If you really need to, discreetly post an ‘update’ post. :wink:

Why are you using AWT Window?

Why are you using Java2D at all? Why not a more modernized framework like JavaFX, or better yet, something based on OpenGL (hint: LibGDX).

Sorry for the bump. I will post the code soon. I did the Println just for testing purposes. The exception is never handled either way.

I know what a null value and what the exception means. The reason for the null pointer is due to the graphics object never being initialized because the buffer strategy was never created.
And thanks for the bumping tip. :stuck_out_tongue:

Well for starters, I need to get used to Java’s API before I get into anything deeper. And also this is a very small project. I just want to get used to Java. Maybe later on I’ll use JavaFX or LibGDX. But still thanks for the framework examples. I haven’t heard of either of them.

Sorry for the double post. Didn’t want to make to make the prev post big.


		
		frame.setSize(new Dimension(575,500));
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setVisible(true);
		
		frame.addWindowListener(new WindowAdapter(){
				
			public void windowClosing(WindowEvent e){
				isRunning = false;
			}
		});
		
		isRunning = true;
		main.start();

Edit: It seems that the problem doesn’t occur when only single buffering is being used.

BUMP!!
Please help!

Maybe this will help.

I just noticed: you never listed the full exception. Please print the full exception you see when you do exc.printStackTrace().

I solved this. I just added in a Canvas into my JFrame and the Buffer was created.
Thanks for all your help guys.

I though it generally wasn’t a good idea to mix awt/swing components?

Tell me a better way to do it.

Use a JPanel not a Canvas.

Okay thanks. But are there any performance issues due to this?

Not really -but you may find some functionality problems down the road.
http://www.leepoint.net/JavaBasics/gui/gui-commentary/guicom-25-swingawtswtxul.html
http://www.ibm.com/developerworks/grid/library/os-swingswt/