Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at game.updateLocation(game.java:52)
at worldMap.startGame(worldMap.java:72)
at gameRun.lambda$start$0(gameRun.java:63)
at gameRun$$Lambda$1/424873723.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Node.fireEvent(Node.java:8175)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)
The trigger for the following code is from gameRun.java
:
worldMap map = new worldMap();
map.startGame(game);
Is the error I get when I attempt to click the play button. The code snippet in worldMap.java is as follows
Location YourHometown, Beach;
Location newLocation;
public void createLocations()
{
YourHometown = new Location("Your Hometown", "Everyone smiles here and talks about sunshines and rainbows. It has a familiar scent of roasted pigs and handknit socks.", "YourHometown.mp3", "Town");
newLocation = YourHometown;
Beach = new Location("Beach", "It's Beachy", "Beach.mp3", "Zone");
}
public void startGame(game Game)
{
createLocations();
Game.updateLocation(newLocation);
}
And Game.updateLocation()
refers to game.java
public void updateLocation (Location newLocation){
...
}
Location is its own separate class, and is an Object. ideally, what this code is supposed to do is change newLocation
every time a button with a different Location than the current one is pressed. The objective of the code displayed is to set the default Location
to the YourHometown
Location
Object.
However, the code breaks once we run Game.updateLocation(newLocation);
with the following error, and though I know what a NullPointerException is, I don't know how to fix it in the given JavaFX context.
My reasoning is in order for the code to run, the code is forced into createLocations();
with the startGame()
method, so there shouldn't ever be a reason why newLocation
is null (Since newLocations
is set equal to YourHometown
). To test my logic, I ran the following code under startGame()
, and it still returned the same error:
public void startGame(game Game)
{
createLocations();
if(newLocation != null)
{
Game.updateLocation(newLocation);
}
else
{
System.out.println("Break");
}
}
The console did not print the "Break" line, meaning that newLocation
is indeed not null, but the script for updateLocation()
runs and the error is returned
TL;DR Conclusion
public void updateLocation (Location newLocation){
if(bgm.getStatus().equals(Status.PLAYING))
{
timeline = new Timeline(new KeyFrame(Duration.seconds(2), new KeyValue(bgm.volumeProperty(), 0)));
timeline.play();
bgm.stop();
}
bg = new Media(new File(newLocation.getMusic()).toURI().toString());
bgm = new MediaPlayer(bg);
bgm.play();
timeline = new Timeline(new KeyFrame(Duration.seconds(2), new KeyValue(bgm.volumeProperty(), 1)));
timeline.play();
}
Everything is supposedly working except for the method given above. I think this might be some sort of File/Mediaplayer issue, but I don't know how to approach solving it. The function for updateLocation()
is supposed to be a smooth transition from the music in the previous location to the next (previous music fade out --> nextLocation music fade in). It calls the .mp3 files that are located in the same folder that the script is in.
Any help is appreciated, and I can include any extra info if it is missing. I tried simplifying it to make it less of a read.