views:

981

answers:

2

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?

+2  A: 

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?

youri
The 2.5 version I am using is snapshot build - the latest stable release is still 2.4. I got the 2.5 snapshot at http://build.canoo.com/htmlunit/artifacts/ - I needed it to resolve a problem 2.4 has with jQuery. It resolved that issue, but apparently brought in new ones...
Spike Williams
What does "sees it as just HtmlInput and not an implemented HtmlInput" mean? Please clarify what that means and/or provide some reference for this.
Joachim Sauer
bad habit of mine... trying to explain in layman terms and losing meaning in the process...refference was first hit google "java final method"sorry i didnt provided it. added to the answer.
youri
+2  A: 

In fact, it's another problem.

The API is public final <I extends HtmlInput> I getInputByName(String name) throws ElementNotFoundException.

That means the method returns an instance of I (I extends HtmlInput).

When you write:

HtmlInput usernameInput = form.getInputByName("username");

-> The class I is implicitly an HtmlInput.

But when you write:

form.getInputByName("password").setValueAttribute(passWord);

-> The class I can't be found explicitly.

If you want it to work, you have to write:

form.<HtmlInput>getInputByName("password").setValueAttribute(passWord);

-> this forces I to be an HtmlInput.

Cordially, Jérôme.

Jérôme