views:

285

answers:

3

I have the following method in my applet:

public File[] getFiles()

Which I call from Javascript as follows:

var files = applet.getFiles();

for (var i = 0; i < files.length; i++) {
    // Do something.
}

This works in Firefox but IE gives me

'length' is null or not an object

Any ideas anyone?

A: 

You can use Firebug Lite in IE to investigate what's going on with files

First, include Firebug Lite in your file

<script type='text/javascript' 
    src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'&gt;&lt;/script&gt;

Now log it in the console with

var files = applet.getFiles();
console.log(files);
for (var i = 0; i < files.length; i++) //...

Now you can investigate what object was actually being returned by getFiles()

I give this suggestion because IE said length is null not or not an object instead of files is null or not an object, so maybe files have other methods that you can use. Use Firebug to reflect those.

kizzx2
A: 

Not sure how widely supported passing a Java array to javascript is. A workaround is to return the array as a delimited string as shown in the following example

http://www.rgagnon.com/javadetails/java-0183.html

objects
Already seen this link. Approach is not viable since getFiles returns an array of non-string objects.
Chry Cheng
I realise that, you build the string from the string attributes required from the File array, eg. the file path.you can then get the details required from the file using that path
objects
A: 

Work-around found. Instead of returning an array, return a list instead. Then iterate through it using the get and size methods. Works in both IE and Firefox.

So, the applet method should be:

public List<File> getFiles()

And to use it Javascript-side:

var files = applet.getFiles();
for (var i = 0; i < files.size(); i++) {
    // Do something with files.get(i).
}
Chry Cheng
Should have checked https://developer.mozilla.org/en/LiveConnect first. Clearly stated there that arrays are dodgy in a certain LiveConnect version.
Chry Cheng