Skip to content

HtmlPage.setFocusedEvent() can end up in NPE #217

Description

@twendelmuth

So I've been trying for the last few days to write a test to reproduce the error I've sometimes observed. However I was unable to get there with vanilla javascript :-/

Therefore I'm posting first here the observation:

https://github.com/HtmlUnit/htmlunit/blob/master/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java#L2513

if (elementWithFocus_ != null) {
            if (getWebClient().isJavaScriptEnabled()) {
                final Object o = getScriptableObject();
                if (o instanceof HTMLDocument) {
                    final Object e = elementWithFocus_.getScriptableObject();
                    if (e instanceof HTMLElement) {
                        ((HTMLDocument) o).setActiveElement((HTMLElement) e);
                    }
                }
            }

            elementWithFocus_.focus();
            elementWithFocus_.fireEvent(Event.TYPE_FOCUS);

            if (hasFeature(EVENT_FOCUS_FOCUS_IN_BLUR_OUT)) {
                elementWithFocus_.fireEvent(Event.TYPE_FOCUS_IN);
            }
        }

At line 2528 (https://github.com/HtmlUnit/htmlunit/blob/master/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java#L2528) elementWithFocus_ could end up being null due to something that has been triggered in the fireEvent(Event.TYPE_FOCUS) method.

A couple of lines above the element is transfered to oldFocusedElement doing similar things (and that's also how I've fixed it in my setup when encountering the issue with jQuery + bunch of different frameworks).
Happy to follow up any ideas on how to get this reproduceable with vanilla JS :-)

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions