参考MediaPlayer Breaks Script? NullPointerException(JavaFX)

时间:2017-05-20 22:14:58

标签: java file javafx nullpointerexception media-player

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)

以下代码的触发器来自gameRun.java

worldMap map = new worldMap();
map.startGame(game);

我尝试单击播放按钮时出现的错误。 worldMap.java中的代码片段如下

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);
}

Game.updateLocation()是指game.java

public void updateLocation (Location newLocation){
...
}

Location是它自己独立的类,是一个Object。理想情况下,每次按下具有与当前位置不同的位置的按钮时,此代码应该做的是更改newLocation。显示的代码的目标是将默认Location设置为YourHometown Location对象。

但是,一旦我们运行Game.updateLocation(newLocation);并且出现以下错误,代码就会中断,虽然我知道NullPointerException是什么,但我不知道如何在给定的JavaFX上下文中修复它。

我的理由是为了运行代码,使用createLocations();方法将代码强制转换为startGame(),因此不应该成为newLocation的原因为空(因为newLocations设置为YourHometown)。为了测试我的逻辑,我在startGame()下运行了以下代码,它仍然返回了相同的错误:

public void startGame(game Game)
{
    createLocations();
    if(newLocation != null)
    {
        Game.updateLocation(newLocation);
    }
    else
    {
        System.out.println("Break");
    }
}

控制台没有打印" Break" line,表示newLocation确实不为null,但运行updateLocation()的脚本并返回错误

TL; DR结论

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();
}

除了上面给出的方法之外,一切都应该有效。我认为这可能是某种文件/媒体播放器问题,但我不知道如何解决它。 updateLocation()的功能应该是从前一个位置的音乐到下一个位置的平滑过渡(之前的音乐淡出 - > nextLocation音乐淡入)。它调用位于脚本所在文件夹中的.mp3文件。

任何帮助表示赞赏,如果缺少任何额外信息,我可以提供。我试着简化它以减少阅读。

1 个答案:

答案 0 :(得分:0)

致电时

map.startGame(game);

游戏为空。我无法看到您是否从粘贴的代码中为其指定了任何值。