2 Replies Latest reply: Oct 5, 2012 1:45 PM by shakir.gusaroff RSS

    JavaFX 2.2 meet LAUNCH problem. Can anybody help ?

    966451
      I already download and use JDK 1.7 with JavaFX 2.2 version (Latest one at this time 2012/10/05).
      Download link is here ==>
      http://www.oracle.com/technetwork/java/javase/downloads/index.html

      My Eclipse verioson is also the latest one on the web.
      Eclipse Java EE IDE for Web Developers.
      Version: Juno Release
      Build id: 20120614-1722


      Compile has passed, but bumped into error. Please see following trace dump. Why Javafx 2.0 has this error ?? How come ?
      Can anybody help me on this ??

      I search to the web and someone says that is a bug for JavaFX 2.0, but I use 2.2 instead? Dose this bug do not fix in 2.2 version ??

      I create a simple java application and cause the following run time error !!
      My platform is MS Windows 7 64-bit. I have installed "jdk-7u7-windows-x64.exe"
      Exception in Application start method
      Exception in thread "main" 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(Unknown Source)
      Caused by: java.lang.NullPointerException
      at com.engine.SpiderEngine$MyBrowser.<init>(SpiderEngine.java:55)
      at com.engine.SpiderEngine.start(SpiderEngine.java:39)
      at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
      at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
      at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
      at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
      at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
      at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
       
      This code is simple copy from someone else and create a new project and paste into it.
      ALL COMPILING ISSUE is resolved, but with run time ERROR !!

      Meamwhile: How come HBox cannot pass error check for .addAll(TextField) ?? What I can do is simple mark them out !
      package com.engine;
      
      import java.awt.TextField;
      import java.net.URL;
      
      import javafx.application.Application;
      //import javafx.application.Platform;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.HPos;
      import javafx.geometry.Insets;
      import javafx.geometry.VPos;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Label;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Region;
      import javafx.scene.web.WebEngine;
      import javafx.scene.web.WebEvent;
      import javafx.scene.web.WebView;
      import javafx.stage.Stage;
      
      public class SpiderEngine extends Application
      {
      
           public static void main(String[] args)
           {
                // TODO Auto-generated method stub
                 Application.launch(args);
           }
      
           private Scene scene;
           MyBrowser myBrowser;
           
           @Override
           public void start(Stage primaryStage)
           {
                primaryStage.setTitle("java-buddy.blogspot.com");
                myBrowser = new MyBrowser();
                scene = new Scene(myBrowser, 640, 480);
                primaryStage.setScene(scene);
                primaryStage.show();
           }
          
           class MyBrowser extends Region
           {
                HBox toolbar;
                WebView webView = new WebView();
                WebEngine webEngine = webView.getEngine();
                
                public MyBrowser()
                {
                     final Label labelWebTitle = new Label();
                     final URL urlHello = getClass().getResource("hello.html");
                     webEngine.load(urlHello.toExternalForm());
                     webEngine.setOnAlert(new EventHandler<WebEvent<String>>(){
                          @Override
                          public void handle(WebEvent<String> arg0) {
                               labelWebTitle.setText(webEngine.getTitle());
                          }
                     });
                     
                     final TextField textField = new TextField ();
                     textField.setText("Hello! Who are?");
                     Button buttonEnter = new Button("Enter");
                     buttonEnter.setOnAction(new EventHandler<ActionEvent>(){
                          @Override
                          public void handle(ActionEvent arg0)
                          {
                               webEngine.executeScript( " updateHello(' " + textField.getText() + " ') " );
                          }
                     });
                     
                     Button buttonClear = new Button("Clear");
                     buttonClear.setOnAction(new EventHandler<ActionEvent>(){
                          @Override
                          public void handle(ActionEvent arg0)
                          {
                               webEngine.executeScript( "clearHello()" );
                          }
                     });
                     
                     toolbar = new HBox();
                     toolbar.setPadding(new Insets(10, 10, 10, 10));
                     toolbar.setSpacing(10);
                     toolbar.setStyle("-fx-background-color: #336699");
                     toolbar.getChildren().add(labelWebTitle);
                     //toolbar.getChildren().add(textField);    // <==  THIS LINE CANNOT PASS COMPILER !!
                     toolbar.getChildren().add(buttonEnter);
                     toolbar.getChildren().add(buttonClear);
                     
                     getChildren().add(toolbar);
                     getChildren().add(webView);
                }                 
               
                @Override
                protected void layoutChildren()
                {
                     double w = getWidth();
                     double h = getHeight();
                     double toolbarHeight = toolbar.prefHeight(w);
                     layoutInArea(webView, 0, 0, w, h-toolbarHeight, 0, HPos.CENTER, VPos.CENTER);
                     layoutInArea(toolbar, 0, h-toolbarHeight, w, toolbarHeight, 0, HPos.CENTER, VPos.CENTER);
                }
           }
      }
      Edited by: 963448 on 2012/10/5 上午 8:41
        • 1. Re: JavaFX 2.2 meet LAUNCH problem. Can anybody help ?
          James_D
          As jsmith already pointed out, this is caused by a NullPointerException at line 55, meaning that something on line 55 has not been properly initialized.

          Line 55 is
          webEngine.load(urlHello.toExternalForm());
          so the two candidates for uninitialized references are webEngine and urlHello.

          WebEngine is initialized as webView.getEngine(), which should be fine. urlHello is initialized with getClass().getResource("hello.html").

          If you look at the javadocs for getResource (in java.lang.Class), they explicitly state

          Returns:
          A URL object or null if no resource with this name is found

          So the resource "hello.html" probably can't be found. Check that it exists and is in the correct location.

          For the toolbar.getChildren().add(textField), the problem is the type of the textField. The add method is expecting a Node. You have imported java.awt.TextField, which is not a Node; you need to import javafx.scene.control.TextField instead.
          • 2. Re: JavaFX 2.2 meet LAUNCH problem. Can anybody help ?
            shakir.gusaroff
            Hi. The url of your html file is:
            final URL urlHello = getClass().getResource("hello.html");
            Your java class and the hello.html file should be in the same place. Your project structure
            should be like this:
                 packageName
                      -  SpiderEngine.java
                      -  hello.html   
               
            Also replace java.awt.TextField with javafx.scene.control.TextField;