Как создать интерфейсы в контроллере Javafx из других пакетов? - PullRequest
0 голосов
/ 05 июня 2019

Я реализую приложение JavaFX с помощью веб-сканера ui4j.Я получаю исключение нулевого указателя в классе контроллера.Я не мог понять, как создавать экземпляры объектов, которые являются интерфейсными классами в методе инициализации.

Я уже прочитал документацию, не нашел ничего похожего.Может, кто-нибудь предоставит мне некоторые преподаватели, это очень ценится.

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

Если я запускаю код без метода инициализации, он работает хорошо.В противном случае, когда я нажимаю кнопку входа, он получает исключение NullPointerException.Поэтому я считаю, что проблема заключается в создании экземпляров объектов документа из пакета ui4j.

1 Ответ

0 голосов
/ 05 июня 2019

Я попробовал ваш код и изменил его на следующий фрагмент кода и протестировал его.


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.

EDIT

Функция page.getDocument() возвращает ноль, даже если страница загружена. Кроме того, приведенный ниже код также дает ноль.

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

Как я уже говорил в своем комментарии, это может быть ошибка в API.

...