1

Hey guys, I'm trying to get the name of every files from a specific folder into an array, but I get this error and I can't find why... this may be a stupid question but whatever.

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Here's my code:

import flash.filesystem.File;

function getFileList(directory:String):Array
{
var folder:File = new File(directory);
var files:Array = folder.getDirectoryListing();
var fileList:Array;

for(var i = 0; i < files.length -1; i++)
{
var path:String = files[i].nativePath;
var split:Array = path.split(File.separator);
fileList[i] = (split[split.length -1]);
}
return fileList;
}

var list:Array = getFileList("E://Whatever//Whatever");

2 Answers 2

2

You forgot to initialize the fileList array and hence it is null when you call fileList[i] = (split[split.length -1]); in the loop.

Change

var fileList:Array;

to

var fileList:Array = [];
Sign up to request clarification or add additional context in comments.

1 Comment

Valid remark also in terms of using literals for initialisation that are way faster than using a constructor, see tekkie.flashbit.net/flash/as/… for a benchmark.
0

I'd be willing to bet it's not finding the path you're entering, so you can't get a directory listing for it.

Try putting some traces in and see if that's where it gets stuck.

1 Comment

Thanks alot for your answer man, but that wasn't it. I just changed that line: var fileList:Array; for that line: var fileList:Array = new Array(); and it worked. But anyway thanks again man, your answer made me see where the problem was. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.