This discussion is archived
4 Replies Latest reply: Sep 8, 2012 11:22 PM by sabre150 RSS

Cannot getDrawGraphics() from buffer_strategy

960645 Newbie
Currently Being Moderated
Hello everyone, i'm working on a little starting more advanced beginners project trying to create a basic 2d graphics engine. I have created a JFrame and a Canvas and added them both. The canvas is runnable and I have implemented a loop. When i want to getDrawGraphics from my buffer_strategy in the render method it gives me an error saying:

Exception in thread "Thread-2" java.lang.NullPointerException
     at com.base.engine.Main.render(Main.java:126)
     at com.base.engine.Main.run(Main.java:93)
     at java.lang.Thread.run(Unknown Source)

I do not get this and have searched the internet for answers. I was not able to find them even on this forum. So I'm asking you to take a look at the code and tell me what might be wrong. Thank you :)

Link to source code at pastebin with linenumbers: http://pastebin.com/yJJP3XeC

Edited by: 957642 on 2012-sep-08 13:23

Edited by: 957642 on 2012-sep-08 13:25
  • 1. Re: Cannot getDrawGraphics() from buffer_strategy
    sabre150 Expert
    Currently Being Moderated
    From the stack trace the NPE originates from Main.java:126 . We don't know which is line 126.

    Note - Unlikely to be the cause of your problem but Canvas is an AWT component and should not be used with Swing (JFrame). You should use a JPanel or JComponent and override the paintComponent() method.
  • 2. Re: Cannot getDrawGraphics() from buffer_strategy
    960645 Newbie
    Currently Being Moderated
    I have now inserted the sourcecode and posted it on pastebin where linenumbers are visible
  • 3. Re: Cannot getDrawGraphics() from buffer_strategy
    960645 Newbie
    Currently Being Moderated
    I have now added a link to pastebin where line nubers are visible :)
  • 4. Re: Cannot getDrawGraphics() from buffer_strategy
    sabre150 Expert
    Currently Being Moderated
    957642 wrote:
    I have now inserted the sourcecode and posted it on pastebin where linenumbers are visible
    Three points :-
    1) the posted code is not that associated with the stack trace you posted since nothing on line 126 could cause a NPE;
    2) don't expect forum members to follow a link to code - post it here (using
     [/code ]tags);
    3) given a stack trace that matches the published code you should easily be able to deduce what is wrong.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points