本文介绍了NullPointerException(JavaFX Label.setText())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的javaFx应用程序有问题。每次我调用我的Label的.setText()方法,我得到一个NullPointerException。这是我的代码:这是我的控制器的snnippet: public class HomenizerController implements可初始化{ // ArrayList zum abspeichern der Termine in Listen Form private ArrayList< Date> dateList = new ArrayList(); // ArrayList zum ab speichern der Aufgaben in Listen Form private ArrayList< ToDo> toDoList = new ArrayList(); private属性prop = null; @FXML private Label username; private void setWelcomeTab(){ username.setText(A); } private void loadProperties(String path){ FileInputStream fis = null; try { prop = new Properties(); fis = new FileInputStream(src / homenizer / profiles /+ path +/ config.properties); prop.load(fis); } catch(FileNotFoundException ex){ Logger.getLogger(HomenizerController.class.getName())。log(Level.SEVERE,null,ex); } catch(IOException ex){ Logger.getLogger(HomenizerController.class.getName())。log(Level.SEVERE,null,ex); } finally { try { if(fis!= null) fis.close(); } catch(IOException ex){ Logger.getLogger(HomenizerController.class.getName())。log(Level.SEVERE,null,ex); } } } public void startHomenizer(Stage stage,String username)throws IOException { parent root = FXMLLoader.load (getClass()。getResource(/ homenizer / view / HomenizerView.fxml)); 场景scene = new Scene(root,1100,650); stage.setResizable(true); stage.setTitle(Homenizer); stage.setScene(scene); stage.show(); loadProperties(username); setWelcomeTab(); } @Override public void initialize(URL url,ResourceBundle rb){ // TODO } 这里是我的.fxml: < center> < TabPane fx:id =tabPan> < Tab text =Willkommenclosable =true> < VBox> < TittedPane text =Allgemeinesexpanded =true> < GridPane> < Label text =Benutzername:GridPane.columnIndex =0GridPane.rowIndex =0/> < Label fx:id =usernameGridPane.columnIndex =1GridPane.rowIndex =0/> < Label text =Termine gesamt:GridPane.columnIndex =0GridPane.rowIndex =1/> < Label fx:id =datesGridPane.columnIndex =1GridPane.rowIndex =1/> < Label text =Aufgaben gesamt:GridPane.columnIndex =0GridPane.rowIndex =2/> < Label fx:id =toDosGridPane.columnIndex =1GridPane.rowIndex =2/> < / GridPane> < / TittedPane> < TitatedPane text =Aktuellexpanded =true> < GridPane fx:id =actualPane> < / GridPane> < / TittedPane> < / VBox> < / Tab> < / TabPane> < / center> 每次我想设置用户名的Text我有这个NULLPointerException: 原因:java.lang.NullPointerException at homenizer.controller.HomenizerController.setWelcomeTab(HomenizerController.java:44) at homenizer .controller.HomenizerController.startHomenizer(HomenizerController.java:76)在homenizer.controller.LoginController.onStartRequest(LoginController.java:107) ... 54更多 线程JavaFX应用程序线程中的异常删除目录C:\Users\chris_000\Documents\NetBeansProjects\Homenizer\dist \run1609603179 所以,有人知道我遇到的问题吗?我找不到一个解决方案:( 感谢所有帮助 -GhostfaceChilla - 解决方案 解决方案 您应该将控制器设置为FXMLLoader。例如,在Homenizer启动方法中使用与下面类似的代码。 FXMLLoader loader = new FXMLLoader( getClass()。getResource(/ homenizer / view / HomenizerView.fxml)); loader.setController(this); 父root =(Parent)loader.load(); 说明 为什么必须在Controller类中设置Controller?我设置控制器在我的fxml文件这不工作 因为你的startHomenizer方法不是一个静态方法,创建了一个Homenizer类的实例,它也是一个控制器,因为它有 @FXML 注释成员。 如果您只是要求FXMLLoader在没有首先将您当前的Homenizer实例设置到FXMLLoader之前加载,那么加载器将创建一个新的Hominizer实例,有两个实例,一个由加载器创建,@FXML成员已初始化,一个由您创建,但未初始化@FXML成员。这是混乱,可能不是你想要的。 你可能想要的是Homenizer类的单个实例。为此,您需要将创建的实例设置为FXMLLoader,并且加载器将使用您现有的类,而不是创建一个新的类(只要没有 fx:controller 在加载的FXML中定义)。 在答案中还有一些讨论:传递参数JavaFX FXML 。 I got problem with my javaFx application. Everytime I call the .setText() method of my Label, I get a NullPointerException. Here is my code:This is a snnippet of my Controller:public class HomenizerController implements Initializable{//ArrayList zum abspeichern der Termine in Listen Formprivate ArrayList<Date> dateList = new ArrayList();//ArrayList zum ab speichern der Aufgaben in Listen Formprivate ArrayList<ToDo> toDoList = new ArrayList();private Properties prop = null;@FXML private Label username;private void setWelcomeTab(){ username.setText("A");}private void loadProperties(String path){ FileInputStream fis = null; try { prop = new Properties(); fis = new FileInputStream("src/homenizer/profiles/"+path+"/config.properties"); prop.load(fis); } catch (FileNotFoundException ex) { Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if(fis != null) fis.close(); } catch (IOException ex) { Logger.getLogger(HomenizerController.class.getName()).log(Level.SEVERE, null, ex); } }}public void startHomenizer(Stage stage, String username) throws IOException{ Parent root = FXMLLoader.load(getClass().getResource("/homenizer/view/HomenizerView.fxml")); Scene scene = new Scene(root,1100,650); stage.setResizable(true); stage.setTitle("Homenizer"); stage.setScene(scene); stage.show(); loadProperties(username); setWelcomeTab(); } @Override public void initialize(URL url, ResourceBundle rb) { // TODO }And here is my .fxml:<center> <TabPane fx:id="tabPan"> <Tab text="Willkommen" closable="true" > <VBox> <TitledPane text="Allgemeines" expanded="true"> <GridPane> <Label text="Benutzername:" GridPane.columnIndex="0" GridPane.rowIndex="0" /> <Label fx:id="username" GridPane.columnIndex="1" GridPane.rowIndex="0" /> <Label text="Termine gesamt:" GridPane.columnIndex="0" GridPane.rowIndex="1" /> <Label fx:id="dates" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Label text="Aufgaben gesamt:" GridPane.columnIndex="0" GridPane.rowIndex="2" /> <Label fx:id="toDos" GridPane.columnIndex="1" GridPane.rowIndex="2" /> </GridPane> </TitledPane> <TitledPane text="Aktuell" expanded="true"> <GridPane fx:id="actualPane"> </GridPane> </TitledPane> </VBox> </Tab> </TabPane></center>Everytime I want to set the Text of username I got this NULLPointerException:Caused by: java.lang.NullPointerExceptionat homenizer.controller.HomenizerController.setWelcomeTab(HomenizerController.java:44)at homenizer.controller.HomenizerController.startHomenizer(HomenizerController.java:76)at homenizer.controller.LoginController.onStartRequest(LoginController.java:107)... 54 moreException in thread "JavaFX Application Thread" Deleting directory C:\Users\chris_000\Documents\NetBeansProjects\Homenizer\dist\run1609603179So, does anybody know the problem I got?I can't find a solution :(Thanks for every help-GhostfaceChilla- 解决方案 SolutionYou should set your controller into an FXMLLoader. For example, use code similar to that below inside your Homenizer start method.FXMLLoader loader = new FXMLLoader( getClass().getResource("/homenizer/view/HomenizerView.fxml"));loader.setController(this);Parent root = (Parent) loader.load();Explanation why must I set the Controller inside my Controller class? I set the controller in my fxml file this didn't workBecause your startHomenizer method is not a static method, you must have already created an instance of your Homenizer class, which is also a Controller, because it has @FXML annotated members.If you just ask the FXMLLoader to load without first setting your current instance of your Homenizer into the FXMLLoader, then the loader will create a new Hominizer instance, so you would end up with two instances, one created by the loader with the @FXML members initialized and one created by you without the @FXML members initialized. This is confusing and probably not what you want.What you probably want is a single instance of the Homenizer class. To do that, you need to set the instance you created into the FXMLLoader and the loader will make use of your existing class instead of creating a new one (as long as there is no fx:controller defined in the loaded FXML).There is some more discussion in the answer to: Passing Parameters JavaFX FXML. 这篇关于NullPointerException(JavaFX Label.setText())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-09 18:54