Illegal State Exception when creating new Bufferstrategy
Taking a look at the API, this exception is thrown if the component is not displayable. In this case, that's when Canvas.peer
is null
. Taking a look at the peer
field reveals that
The peer is set when the
Component
is added to a container that also is a peer
Since you are starting the update thread from your component's constructor, render
could be called before your component is ever added to another container which would mean the peer
is null
, and then an IllegalStateException
would be thrown.
In my experience with this error and with the code you writing you missing a frame function.
Add where your frames are (ex: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
) and add the function frame.add(game);
In this example, mine is Display game = new Display();
but depending on what your variable for the new display is, it might vary.