在JavaFX中运行应用程序时出现异常

时间:2015-01-26 18:25:31

标签: exception javafx

我遇到了一个大问题,确保所有文件中没有错误,当我运行JavaFX Application时,我得到一个小窗口包含“运行应用程序时出现异常”。输出突出显示问题出在Main Class中,所以这就是我在Main类中的内容:

      package amnet23;

       import javafx.application.Application;
       import javafx.fxml.FXMLLoader;
        import javafx.scene.Parent;
       import javafx.scene.Scene;
      import javafx.stage.Stage;

     public class AmNet23 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("WorkSpace.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
here's the Controller class:

    package amnet23;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javax.swing.text.html.HTMLEditorKit;

/**
 *
 * @author Ahmed
 */
public class FXMLDocumentController implements Initializable {
    Stage tt;
    //anchor
    AnchorPane ancpa;
   //labels fxid
    Label labeln1;
    Label labeln2;
    Label labelslo; // text field
    Button labelsear; // help
    Button labelpop; // about
    Label labeloren; // nejma
    Label labelpros; // search
    Label labelno;
   // text field
    TextField text11;
   //buttons fxid 
    //button links
    Button but1;
    Button but2;
    Button but3;
    Button but4;
    Button but5;
    Button but6;
    Button but7;
    Button but8;
    Button but9;
    Button but10;
    Button but11;
    Button but12;
    Button but13;
    Button but14;
    Button but15;
    Button but16;
    // other buttons
    Button butser;
    Button butnext;
    Button butprev;
    Button butref;
    Button buth;
    Button butg;
    Button but1f;
    Button butnej;
    Button butab;
   // tabpanes
    TabPane tabpane1;
    Tab tab1;
    Tab tab2;
    Tab tab3;
    Tab tab4;
    Tab tab5;
    Tab tab6;
    Tab tab7;
    Tab tab8;
    Tab tab9;
   // wabpanes
    WebView web1;
    WebView web2;
    WebView web3;
    WebView web4;
    WebView web5;
    WebView web6;
    WebView web7;
    WebView web8;
    WebView web9;
    //other window (chooser)
    @FXML
    Label que;
    @FXML
    Button tab1b;
     Button tab2b;
     Button tab3b;
     Button tab4b;
     Button tab5b;
     Button tab6b;
     Button tab7b;
     Button tab8b;
     Button tab9b;
    @FXML
    AnchorPane panoo;
@FXML
     private void  searchButton (ActionEvent evt) throws IOException {

       Stage stage = new Stage();
             Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.show();

         text11.setOnKeyTyped(new EventHandler<KeyEvent>(){
             public void hendeller (KeyEvent evty) throws IOException{
                 if (evty.getCode() == KeyCode.ENTER){
         Stage stage = new Stage();
          Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
              Scene scene = new Scene(root);
              stage.setScene(scene);
              stage.show();
                 }
             }

             @Override
             public void handle(KeyEvent t)  {

             }


         });
     }
    @FXML
    private void tabfirst (ActionEvent ee) throws IOException { // for tha Chooser frame text.

                String hh = text11.getText();
                Socket socket = new Socket();

        try {
              web1.setVisible(true);
            //open cursor
            panoo.setCursor(Cursor.WAIT);
            que.setCursor(Cursor.WAIT);
            //add
            ancpa.setCursor(Cursor.WAIT);
            web1.setCursor(Cursor.WAIT);
            web2.setCursor(Cursor.WAIT);
            web3.setCursor(Cursor.WAIT);
            web4.setCursor(Cursor.WAIT);
            web5.setCursor(Cursor.WAIT);
            web6.setCursor(Cursor.WAIT);
            web7.setCursor(Cursor.WAIT);
            web8.setCursor(Cursor.WAIT);
            web9.setCursor(Cursor.WAIT);
            //do work
            URL url = new URL (hh);
            url.getContent();
            WebEngine myWebEngine = web1.getEngine();
            myWebEngine.load(url.toExternalForm());
            //close the window chooser
            Stage stage = new Stage();
              Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
              Scene scene = new Scene(root);
             stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                  @Override public void handle(WindowEvent t) { } });
            //close cursor
            ancpa.setCursor(Cursor.DEFAULT);
            web1.setCursor(Cursor.DEFAULT);
            web2.setCursor(Cursor.DEFAULT);
            web3.setCursor(Cursor.DEFAULT);
            web4.setCursor(Cursor.DEFAULT);
            web5.setCursor(Cursor.DEFAULT);
            web6.setCursor(Cursor.DEFAULT);
            web7.setCursor(Cursor.DEFAULT);
            web8.setCursor(Cursor.DEFAULT);
            web9.setCursor(Cursor.DEFAULT);
        }
       catch (IOException e){
           final  Stage stg = new Stage();           
            stg.initModality(Modality.APPLICATION_MODAL);
            stg.initOwner(stg);
            stg.setTitle("Cannot connect to the internet /n Please Verify your connection internet");
            labelno.setText("Cannot connect to the internet...");
            //close chooser
            Stage stage = new Stage();
             Parent root = FXMLLoader.load(getClass().getResource("Choose.fxml"));
             stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                  @Override public void handle(WindowEvent t) { } });

           //set cursor
             ancpa.setCursor(Cursor.DEFAULT);
            web1.setCursor(Cursor.DEFAULT);
            web2.setCursor(Cursor.DEFAULT);
            web3.setCursor(Cursor.DEFAULT);
            web4.setCursor(Cursor.DEFAULT);
            web5.setCursor(Cursor.DEFAULT);
            web6.setCursor(Cursor.DEFAULT);
            web7.setCursor(Cursor.DEFAULT);
            web8.setCursor(Cursor.DEFAULT);
            web9.setCursor(Cursor.DEFAULT);
       } finally{
           try{ socket.close(); } catch (Exception e){ }
           }

    }
    @FXML
    private void err (ActionEvent eer) {
              web1.setVisible(true);
              ancpa.setCursor(Cursor.WAIT);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        }

}

这是StuckTrace

ant -f C:\\Users\\Midoo\\Documents\\NetBeansProjects\\AmNet23 jfxsa-run
init:
Deleting: C:\Users\Midoo\Documents\NetBeansProjects\AmNet23\build\built-jar.properties
deps-jar:
Updating property file: C:\Users\Midoo\Documents\NetBeansProjects\AmNet23\build\built-jar.properties
compile:
Detected JavaFX Ant API version 1.2
jfx-deployment:
jar:
Copying 12 files to C:\Users\Midoo\Documents\NetBeansProjects\AmNet23\dist\run1416917689
jfx-project-run:
Executing C:\Users\Midoo\Documents\NetBeansProjects\AmNet23\dist\run1416917689\AmNet23.jar using platform C:\Program Files\Java\jdk1.7.0_65\jre/bin/java
java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class amnet23.FXMLDocumentController with modifiers ""
file:/C:/Users/Midoo/Documents/NetBeansProjects/AmNet23/dist/run1416917689/AmNet23.jar!/amnet23/WorkSpace.fxml:15
  at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:664)
  at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:575)
  at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2356)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2172)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2830)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
  at amnet23.AmNet23.start(AmNet23.java:23)
  at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
  at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
  at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
  at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
  at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
  at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
  at java.lang.Thread.run(Thread.java:745)

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:606)
    at com.javafx.main.Main.launchApp(Main.java:698)
    at com.javafx.main.Main.main(Main.java:871)
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:745)
Caused by: java.lang.RuntimeException: java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class amnet23.FXMLDocumentController with modifiers ""
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:664)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:575)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2356)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2172)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2830)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2782)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2771)
    at amnet23.AmNet23.start(AmNet23.java:23)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:219)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:182)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:179)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    ... 1 more
Caused by: java.lang.IllegalAccessException: Class javafx.fxml.FXMLLoader$ValueElement can not access a member of class amnet23.FXMLDocumentController with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
    at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
    at java.lang.reflect.Field.set(Field.java:738)
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:662)
    ... 21 more
Java Result: 1
Deleting directory C:\Users\Midoo\Documents\NetBeansProjects\AmNet23\dist\run1416917689
jfxsa-run:
BUILD SUCCESSFUL (total time: 6 seconds)

所以我不明白这个问题是由任何人帮助我所能引起的,并提前致谢:)

1 个答案:

答案 0 :(得分:1)

你还没有展示FXML,所以这是一个猜测,但你的控制器中的大多数字段都没有@FXML - 注释,似乎没有在其他地方初始化。所以我怀疑FXMLLoader正在尝试注入一个(或多个)没有@FXML注释的字段。您需要注释从FXML文件注入的所有字段。