Search code examples
javahtmlunit

Java Object "cannot be dereferenced"


I'm using a library - HtmlUnit - and I just updated from version 2.4 to version 2.5. When I compile my code against 2.5, I'm getting a strange object "cannot be dereferenced" error message that I'm not familiar with. Moreover, I don't understand why it works when I write the code in two lines, but fails when I do it as a one-liner .

Here is the code:

//this compiles fine
HtmlInput usernameInput = form.getInputByName("username");
usernameInput.setValueAttribute(userName);

//this fails to compile
form.getInputByName("password").setValueAttribute(passWord);

This is the error message I get when I compile using ANT and Java 1.6:

[javac] E:\workspaces\europa\PortalTestSuite\src\com\carefirst\portal\test\controller\EAITest.java:32: com.gargoylesoftware.htmlunit.html.HtmlInput cannot be dereferenced
[javac]         form.getInputByName("password").setValueAttribute(passWord);
[javac]                                    ^

com.gargoylesoftware.htmlunit.html.HtmlInput cannot be dereferenced ? I've seen derefernce issues with Autoboxing, but not with objects. What does it mean in this context? And why does the code work one way and not the other?


Solution

  • The function form.getInputByName is declared final as shown in the javadoc here

    The object HtmlInput is abstract thus on runtime time it is always implemented by an object that is extending HtmlInput.

    Due to the nature of final methods (it is never overridden so there is no callstack) the error comes beceause the compiler sees it as just htmlinput and not an implemented htmlinput.(ref)(he thinks there will be no callstack) assigning it to a new htmlinput object correctly invokes the callstack and thus makes the code viable again.

    thats what i think is going on here

    though i somehow dont manage to find version 2.5 yet can you link me please where you got it?