Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing Canvas Returns With NullPointerException  RSS feed

 
Steven Laabs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...

This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:


It is initialized and runs in the BreakoutMain class:


This is the State class that all other states extend from (i.e GameState, MenuState, etc.) which in turn extends the Canvas class:


The code that calls the change of state in MainMenuState is called in the update method:


Here is the full stack trace when clicking the area to change states:


Looking at the stack trace and the line numbers I assumed it would be either g or b in the State class' render method that are coming up as null but I can't seem to find why or when that could be happening. I printed out the value of g and got:

It gives that same value both before and after clicking on the region.
Printing out b after the State is changed to GameState gives the following:


Any help with explaining what exactly is going wrong here would be very much appreciated. I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong. Thanks in advance.
 
Tony Docherty
Bartender
Posts: 3201
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch,

BTW Why are you using Canvas with JFrame, you can get problems mixing AWT and Swing components (although I think recent releases have fixed a number of these problems).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!