我正在尝试使用fileChooser来更改我的媒体播放器文件。在运行期间,当我尝试打开时,我得到Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
。 main.java文件如下:
package application;
import java.io.*;
import java.net.MalformedURLException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
Player player;
FileChooser fileChooser;
public void start(final Stage primaryStage) {
MenuItem open = new MenuItem("Open");
Menu file = new Menu("File");
MenuBar menu = new MenuBar();
file.getItems().add(open);
menu.getMenus().add(file);
fileChooser = new FileChooser();
open.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
player.player.pause();
File file = fileChooser.showOpenDialog(primaryStage);
if(file != null) {
try {
player = new Player(file.toURI().toURL().toExternalForm());
Scene scene = new Scene(player,720,580,Color.BLACK);
primaryStage.setScene(scene);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
}
});
//Creating file object for media source
File files =new File("D:/music.mp3");
// Change URI to string format in file
final String source=files.toURI().toString();
Player player = new Player(source);
player.setTop(menu);
Scene scene = new Scene(player,720,580);
primaryStage.setScene(scene);
primaryStage.setTitle("KoroPlay");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
显示的错误是:
线程“JavaFX应用程序线程”中的异常 java.lang.NullPointerException at application.Main $ 1.handle(Main.java:37)at application.Main $ 1.handle(Main.java:1)at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)at javafx.event.Event.fireEvent(Event.java:198)at javafx.scene.control.MenuItem.fire(MenuItem.java:462)at at com.sun.javafx.scene.control.skin.ContextMenuContent $ MenuItemContainer.doSelect(ContextMenuContent.java:1405) 在 com.sun.javafx.scene.control.skin.ContextMenuContent $ MenuItemContainer.lambda $ createChildren $ 343(ContextMenuContent.java:1358) 在 com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 在 com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 在 com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 在 com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在 com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 在 com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)at javafx.event.Event.fireEvent(Event.java:198)at javafx.scene.Scene $ MouseHandler.process(Scene.java:3757)at javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3485)at at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:380) 在 com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:294) 在java.security.AccessController.doPrivileged(Native Method)at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:416) 在 com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 在 com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) 在com.sun.glass.ui.View.handleMouseEvent(View.java:555)at com.sun.glass.ui.View.notifyMouse(View.java:937)at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)at com.sun.glass.ui.win.WinApplication.lambda为$ null $ 148(WinApplication.java:191) 在java.lang.Thread.run(未知来源)
主要:Java:37是
player.player.pause();
播放器类在另一个文件中创建。如果需要,这是Player.java:
package application;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
public class Player extends BorderPane {
Media media;
MediaPlayer player;
MediaView view;
Pane pane ;
MediaPanel panel;
public Player(String file) {
media= new Media(file);
player = new MediaPlayer(media);
view = new MediaView(player);
// Setting mediaView dimension w.r.t the media(video)
view.setTranslateX(view.getFitWidth() / 2 + 50);
view.setTranslateY(view.getFitHeight() / 2 - 13);
view.setScaleX(0.90); view.setScaleY(0.95);
//Setting pane location
pane=new Pane();
pane.getChildren().add(view);
setCenter(pane);
panel=new MediaPanel(player);
setStyle("-fx-background-color:BLACK");
setBottom(panel);
panel.setStyle("-fx-background-color:#ffffff");
player.play();
}
//public MediaPlayer(Media media2) {
// TODO Auto-generated constructor stub
// }
}
如何解决此问题?默认情况下设置的媒体不会按要求停止并请求打开新文件。发生此错误。