2013-08-30 111 views
0

我正在尝试实现here所述的多语言支持解决方案。无法找到JavaFX的fxml错误

但我得到“java.lang.NullPointerException:位置是必需的。” 请注意以下相关代码中的缺陷。 感谢

MultiLanguageSupport.java

public class MultiLingualSupport extends Application { 

Stage currentStage; 
void setStage(Stage stage) 
{ 
    currentStage=stage; 
} 
@Override 
public void start(Stage stage) throws Exception { 

    setStage(stage); 
    replaceScene("LanguageUIFXML.fxml",this,new Locale("en","EN")); 

} 


public static void main(String[] args) { 
    launch(args); 
} 
public void replaceScene(String fxml, Object aThis, Locale locale) //throws Exception 
{ 
    System.out.println("Replacing Scene"); 
    try 
    { 
     Parent root; 
     FXMLLoader loader=new FXMLLoader(); 
     loader.setResources(ResourceBundle.getBundle("TextData.AllText", locale)); 
     root = loader.load(this.getClass().getResource(fxml),null, new JavaFXBuilderFactory()); 
     Scene scene=currentStage.getScene(); 
     if(scene==null) 
     { 
      scene=new Scene(root); 
      currentStage.setScene(scene); 
     } 
     else 
      currentStage.getScene().setRoot(root); 

     currentStage.sizeToScene(); 
     currentStage.show(); 
    } 
    catch (IOException ex) 
    { 
     ex.printStackTrace(); 
    } 


} 
} 

LanguageUIFXML.fxml

<?import java.lang.*?> 
<?import java.net.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml" fx:controller="multilingualsupport.LanguageUIFXMLController"> 
    <children> 
    <Label fx:id="lblDemo" layoutX="284.0" layoutY="72.0" prefHeight="26.0" prefWidth="144.0" text="%keyDemo" /> 
    <TextField fx:id="txtDemo" layoutX="284.0" layoutY="124.0" prefWidth="200.0" text="%keyTextField" /> 
    <TextArea fx:id="txtareaDemo" layoutX="284.0" layoutY="164.0" prefWidth="200.0" text="%keyTextArea" wrapText="true" /> 
    <Button fx:id="btnDemo" layoutX="356.0" layoutY="347.0" mnemonicParsing="false" text="%keyButton" /> 
    <ListView fx:id="listviewDemo" layoutX="35.0" layoutY="85.0" orientation="HORIZONTAL" prefHeight="218.0" prefWidth="221.0" /> 
    </children> 
    <stylesheets> 
    <URL value="@/css/langageuifxml.css" /> 
    </stylesheets> 
</AnchorPane> 

LanguageUIFXMLController.java

public class LanguageUIFXMLController implements Initializable { 

private Label label; 
@FXML 
private Label lblDemo; 

@FXML 
private TextField txtDemo; 
@FXML 
private TextArea txtareaDemo; 
@FXML 
private Button btnDemo; 
@FXML 
private ListView<String> listviewDemo=new ListView<>(); 
private ResourceBundle bundle; 

    private void handleButtonAction(ActionEvent event) { 
    System.out.println("You clicked me!"); 
    label.setText("Hello World!"); 
} 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
    bundle=rb; 
    lblDemo.setText(rb.getString("keyDemo")); 
    txtDemo.setText(rb.getString("keyTextField")); 
    txtareaDemo.setText(rb.getString("keyTextArea")); 
    btnDemo.setText(rb.getString("keyButon")); 



}  
} 

错误消息:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at com.javafx.main.Main.launchApp(Main.java:642) 
at com.javafx.main.Main.main(Main.java:805) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) 
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) 
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) 
at java.lang.Thread.run(Thread.java:722) 
Caused by: java.lang.NullPointerException: Location is required. 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2737) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721) 
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707) 
at multilingualsupport.MultiLingualSupport.replaceScene(MultiLingualSupport.java:58) 
at multilingualsupport.MultiLingualSupport.start(MultiLingualSupport.java:35) 
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) 

.properties文件在包中定义的TextData

+0

可能不是问题,但'私人的ListView listviewDemo =新的ListView <>(); '也许应该是'private ListView listviewDemo;' – assylias

+0

不,这不是问题 –

+0

'this.getClass()。getResource(fxml)'可能返回null。你应该检查一下。 – assylias

回答

1

对不起,这是2个拼写错误的结果&一个不正确的CSS路径。 所需的更改: 在FXML文件, 我应该已经:

<URL value="@/css/langageuifxml.css" /> 

竟是

<URL value="@css/langageuifxml.css" /> 
1

可以验证FXML文件和MultiLingualSupport类在同一个包?

而且你调用非静态实例fxmlloader的静态方法,你被

设置之后重新设置资源包为空
loader.setResources(ResourceBundle.getBundle("TextData.AllText", locale)); 

你可以尝试

root = (AnchorPane) loader.load(this.getClass().getResource(fxml).openStream()); 
+0

+1 Uluk是对的。不要使用静态加载方法,它们很容易混淆并且容易出错。只需使用实例加载方法。静态FXMLLoader.load方法[无论如何都计划弃用](https://javafx-jira.kenai.com/browse/RT-24127)。 – jewelsea

+0

你好,我尝试用你的方法初始化根,但它仍然给出相同的NullPointerException没有消息“位置是必需的”。并原谅无知,但什么是替代静态方法ResourceBundle.getBundle()?它的实例似乎仅在控制器的初始化方法中可用 –