NilObjectException on file copy routine

i am getting a NilObjectException on a file to file copy
I am reading all the files in a given directory, then attempting to copy them to another destination.
my routine is “copyFiles” … the exception is taking place in the routine at the CopyFileTo line.

please advise.

 for i = 0 to Ubound(fileArr) - 1
//   this is the call to the copyFiles
    rtn = copyFiles(dirPath + "/" + fileArr(i), AppDataPath, fileArr(i))
  next i

[code]sub CopyFiles(fromfile as string, toDir As String, fileName As String)
Dim FromFileFolder As FolderItem = GetFolderItem(fromfile, FolderItem.PathTypeShell)
Dim ToFileFoler As FolderItem = GetFolderItem(toDir, FolderItem.PathTypeShell)
Dim adFolder As FolderItem = SpecialFolder.ApplicationData
Dim rtn As String
Dim ft as new filetype

ft.name = fileName

// this line is getting the Exception !!!
FromFileFolder.CopyFileTo(GetFolderItem(toDir).child(ft.name))

End Sub
[/code]

GetFolderItem can fail if path is wrong.
And please don’t do the math on paths as “/” is not always the right delimiter. Please use .child() function.

FolderItem.PathTypeShell may not be the right one. Probably Native type.

You should put stuff in temp variables, so you can change for nil.

Hi Christian,

im working on MAC OS

The Shell Type was wrong, i was getting a “” in the path.
I have changed that to PathTypeNative I still get the exception.

i have checked the from and to paths and they look correct.
fromDir /Volumes/8GB/stxDB/
toDir /Users/davidcullins/Library/Application Support/PassSaver/Data

I am using the Child in the CopyFileTo call. I do not know where else i would use a Child …

anyway I still get the exception.

http://documentation.xojo.com/index.php/Volume

i see what your saying Tim. about Volume(i)

I am selecting the FromDir via a Dialog Folder Box,
that passes the /Volumes/8GB/stxDB to the CopyFileTo routine…

Could it be that the Dialog box is gathering the wrong path?

maybe if you guys saw the exception error you could better diagnosis the issue

STACK
MainWin.copyToFile

Variables
Globals
adFolder FolderItem
Exception NilObjectionException
fileName xxxxxxxx
fromfile /Volumes/8GB/stxDB/xxxxxxxx
FromFileFolder FolderItem
Return
rtn
toDir /Users/davidcullins/Library/Application Support/PassSaver/Data
ToFileFolder FolderItem

I don’t really know what you’re doing, but I’d say the next step is to stop mucking around with path strings and GetFolderItem. Use only the FolderItem methods, Open/Save Dialogs, and SpecialFolder until you stop getting errors.

There is example code in the documentation for copying an entire folder and it’s contents.

I found Michael B’s code to copy an entire folder
but suppose you did not want to overwrite files or prompt the user to overwrite …

However this read the contents of a source folder, then copies the content and the folder its self "/folder
I only want to copy the contents … not the actual folder.

dim source as folderitem = SelectFolder
  if source <> nil and source.Exists then
    dim destination as FolderItem = SpecialFolder.Documents
    source.CopyFileTo(destination)
  end if

does this help?
Supply a folder item each for the folder on the usb drive, and the target folder

[code]sub CopyFolderCONTENTS(fromFOLDER as FOLDERITEM, toFOLDER As FOLDERITEM)
dim x as integer
if fromFolder.directory and toFolder.directory then

dim fromfile as foldertitem
dim tofile as folderitem
Dim count As Integer = fromFolder.Count
For i As Integer = 1 To count
fromfile = desktopFolder.Item(i)
fromfile.CopyFileTo(tofolder.child(fromfile.name))
Next

end if

End Sub[/code]

in the new framework, you might use this code in the middle instead

For Each f As FolderItem In fromFolder.Children f.copyfileto(tofolder.child(f.name)) Next

yes much much Better Jeff.
however suppose you wanted to only copy certain files not all files
such as files that end in .ini .dat .exe .txt ect …

if instr(fromfile.name, ".EXE") > 0 then //do the copy end if

Even better thank you !!!