如何从其他包实例化Javafx控制器中的接口?

时间:2019-06-05 18:38:55

标签: javafx controller initialization ui4j

我正在通过Web搜寻器ui4j实现JavaFX应用程序。我在控制器类中得到一个空指针异常。我不知道如何在初始化方法中实例化作为接口类的对象。

我已经阅读过文档,没有找到类似的东西。有人可以向我提供一些教育人员,对此深表感谢。

public class AppController implements Initializable {

    @FXML
    private Label wronglabel;
    @FXML
    private CheckBox savein;

    @FXML
    private Button signin;

    @FXML
    private TextField email;

    @FXML
    private PasswordField pass;

    @FXML
    private URL url;

    @FXML
    private ResourceBundle rb;
    @FXML
    private Preferences preferences;
    @FXML
    private BrowserEngine webkit;
    @FXML
    private PageConfiguration configuration;
    @FXML
    private Page page;
    @FXML
    private Document document;
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO Auto-generated method stub

    email = new TextField();
    pass = new PasswordField();
    preferences = Preferences.userNodeForPackage(AppController.class);
     BrowserEngine webkit = BrowserFactory.getWebKit();
     PageConfiguration configuration = new PageConfiguration();
     configuration.setSelectorEngine(SelectorType.SIZZLE);
     configuration.setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");

        Page page = webkit.navigate("https://www.amazon.com/ap/signin?clientContext=132-0119732-2425437&openid.return_to=https%3A%2F%2Frelay.amazon.com%2F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=amzn_relay_desktop_us&openid.mode=checkid_setup&marketPlaceId=ATVPDKIKX0DER&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_relay_desktop_us&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=86400&siteState=clientContext%3D136-7029875-0274320%2CsourceUrl%3Dhttps%253A%252F%252Frelay.amazon.com%252F%2Csignature%3Dnull", configuration);
        page.show();

        Document document = page.getDocument();

        if(preferences!=null) {
            if(preferences.get("username",null)!=null &&!preferences.get("username",null).isEmpty()) {
                email.setText(preferences.get("username",null));
                pass.setText(preferences.get("password", null));
            }
        }
    }


    public void signIn(ActionEvent event) throws Exception {
        String username = email.getText();
        String password = pass.getText();
        if(savein.isSelected()) {
        preferences.put("username", email.getText());
        preferences.put("password", pass.getText());
        }
        else {
            preferences.put("username", "");
            preferences.put("password", "");
        }




        document
            .query("#ap_email")//ERROR STARTS HERE
            .setValue(username);//NULLPOINTER EXCEPTION

        document
            .query("#ap_password")//NULLPOINTER EXCEPTION
            .setValue(password);//NULLPOINTER EXCEPTION

        document
            .query("#signInSubmit").click();//NULLPOINTER EXCEPTION

        Thread.sleep(10000);

        page.close();

        webkit.shutdown();


    }

StackTrace:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    ... 46 more
Caused by: java.lang.NullPointerException
    at com.notificsound.amazon2.AppController.signIn(AppController.java:96)
    ... 57 more

如果我运行的代码没有初始化方法,那么它将运行良好。否则,当我按下按钮signIn方法时,它将获取NullPointerException。因此,我认为从ui4j包实例化文档对象是一个问题。

1 个答案:

答案 0 :(得分:0)

我已经尝试过您的代码,并将其更改为以下代码并进行了测试。


import io.webfolder.ui4j.api.browser.*;
import io.webfolder.ui4j.api.dom.Document;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;

public class AppController implements Initializable {

    @FXML
    private Label wronglabel;
    @FXML
    private CheckBox savein;

    @FXML
    private Button signin;

    @FXML
    private TextField email;

    @FXML
    private PasswordField pass;

    //remove @FXML from these
    private Page page;
    private Preferences preferences;
    private BrowserEngine webkit;
    private Document document;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO Auto-generated method stub
        email = new TextField();
        pass = new PasswordField();
        preferences = Preferences.userNodeForPackage(AppController.class);


        BrowserEngine webkit = BrowserFactory.getWebKit();
        PageConfiguration configuration = new PageConfiguration();
        configuration.setSelectorEngine(SelectorType.SIZZLE);
        configuration.setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");

        page = webkit.navigate("https://www.amazon.com/ap/signin?clientContext=132-0119732-2425437&openid.return_to=https%3A%2F%2Frelay.amazon.com%2F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=amzn_relay_desktop_us&openid.mode=checkid_setup&marketPlaceId=ATVPDKIKX0DER&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_relay_desktop_us&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=86400&siteState=clientContext%3D136-7029875-0274320%2CsourceUrl%3Dhttps%253A%252F%252Frelay.amazon.com%252F%2Csignature%3Dnull", configuration);
        page.show();

        //this returns null, which I think is a problem with the api
        document = page.getDocument();

        //i have also tried this and it too gives null
        page.addDocumentListener(documentLoadEvent -> {
            document = page.getDocument();
            if (document == null) System.out.println("IS NULL");
        });

        if (preferences != null) {
            if (preferences.get("username", null) != null && !preferences.get("username", null).isEmpty()) {
                email.setText(preferences.get("username", null));
                pass.setText(preferences.get("password", null));
            }
        }
    }

    @FXML
    public void signIn(ActionEvent event) throws Exception {
        String username = email.getText();
        String password = pass.getText();
        if (savein.isSelected()) {
            preferences.put("username", email.getText());
            preferences.put("password", pass.getText());
        } else {
            preferences.put("username", "");
            preferences.put("password", "");
        }


        //exception was because document had not been instantiated properly
        document.query("#ap_email")
                .setValue(username);

        document
                .query("#ap_password")
                .setValue(password);

        document
                .query("#signInSubmit").click();

        Thread.sleep(10000);

        page.close();

        webkit.shutdown();
    }
}

问题是文档对象未正确实例化。 initialize中的那个与signIn正在访问的那个不同。

编辑

即使页面已加载,函数page.getDocument()仍返回null。另外,以下代码也为null。

page.addDocumentListener(documentLoadEvent -> {
            if (documentLoadEvent.getDocument()==null ) System.out.println("IS NULL");
        });

正如我在评论中所述,这可能是API中的错误。

相关问题