1

I am trying to execute Javascript function called "returnAllLinkTexts()" on the DOM html page loaded via my Java application. Below line is executed by a Swing Buton.

myscript = browser.executeJavascript("returnAllLinkTexts()").toString(); //Line 407

Once in a while I get the following exception. The Java application does not terminate or crash.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException com.demo.Main$BigButtonListener.actionPerformed(Main.java:407)

I have tried the following to keep retrying about 20 times but it doesn't even reach this point. Exception is raised immediately @ 407.

int st = 0;
while (myscript == null){
 myscript = browser.executeJavascript("gogo()").toString();                              if (myscript != null) break;
 if (shit == 20) break;
 sht++;
}

UPDATE:

This is the Javascript function returnAllLinkTexts();

function returnAllLinkTexts(){  
var mydata = new Array();

$('a', document).each(function() {
    mydata.push($(this).text()); 
});

return mydata;
}
KJW
  • 15,035
  • 47
  • 137
  • 243

3 Answers3

1

You're calling toString() on a potentially null object.

-> Firstly, I would surround with a try-catch block

But the question is why is the return value a possible-null? Check your return value in the function..

Nur

nurnachman
  • 4,468
  • 2
  • 37
  • 40
  • I have provided the function above....but I don't see why it would return null. There are links on the page with 100% certainty. – KJW Jan 19 '11 at 09:12
1

The only thing I can think of why returnAllLinkTexts is breaking (thus you get null) is when it's called before jQuery was loaded.

If possible, try calling browser.executeJavascript after the page finished loading otherwise check for null as others already suggested and you can keep trying to invoke it (using timer for example) until it's not null.

Edit: since you're already using the return value as string, you can return string to begin with, for example:

return mydata.join(",");

Will return the links text separated with comma.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • This is in the right direction but jQuery is loaded already. This Java exception only seems to occur when returnAllLinkTexts is used....if it's just a simple text, then no Java NullPointerExceptions.... I've tried to invoke it 100 loops which took about a minute, but it's still null....then other times it's working fine....totally frustrated with this right now. – KJW Jan 19 '11 at 12:17
  • @Kim weird indeed, see my edit though - if you'll return ordinary string from within the JS function it might solve the problem. – Shadow The GPT Wizard Jan 19 '11 at 12:58
  • I think this is the problem because when I remove the jQuery functions and methods from returnAllLinkTexts();, everything seems kosher. when I add any jQuery function, trouble brews – KJW Jan 22 '11 at 08:10
0

Don't unconditionally call toString() on a return value which may be null. Always check first for null!

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347