Strange NilObjectException?

Hello,
I am a bit confused here as to what is going on?

I have the following code (in a method), which SUCCESSFULLY creates a new text file, and then puts the name into a listbox row:

[code]// DEFINE THE NAME OF THE NEW NOTE
NoteName = NoteNameWindow.Namefield.text

// DEFINE THE PATH
dim appSupportFolder as FolderItem = SpecialFolder.ApplicationData.child(kAppSupportFolderName)

// CHECK IF THE FOLDER EXISTS
if not appSupportFolder.exists then

// IF NOT, THEN CREATE IT
appSupportFolder.CreateAsFolder

end

// DEFINE THE Notes FOLDER
NotesFolder = appSupportFolder.child(“Notes”)

// IF THE Notes FOLDER DOESN’T EXIST - CREATE IT
if not NotesFolder.exists then
NotesFolder.CreateAsFolder
end

// DEFINE THE newItem VARIABLE AS A FOLDERITEM
Dim newItem as FolderItem = NotesFolder.child(NoteName)

// SET THE FILENAME AS THE ROWTAG
NotesWindow.notesList.RowTag(NotesWindow.notesList.LastIndex) = NoteName

// DEFINE tos VARIABLE AS A NEW TEXT OUTPUT STREAM
dim tos as TextOutputStream

// CREATE THE NEW FILE
tos = TextOutputStream.Create(newItem)

// CLOSE THE FILE
tos.Close

// SET FOCUS TO THE NOTES LISTBOX
NotesWindow.notesList.SetFocus

// ADD NEW LISTBOX ROW CONTAINING THE NEW NAME
NotesWindow.notesList.AddRow(NoteName)[/code]

In the listbox Change event - it opens the text file (associated to that row’s Rowtag), into a text area:

[code]// DEFINE THE VARIABLE tis AS THE TEXT INPUT STREAM
dim tis as TextInputStream

// OPEN THE FILE CORRESPONDING TO THE LISTBOX SELECTION --------- CAUSES THE ERROR
tis = TextInputStream.Open(me.RowTag(me.ListIndex))

// READ THE TEXT FILE’S CONTENT INTO THE noteView AREA
noteView.text = tis.ReadAll

// CLOSE THE FILE
tis.Close[/code]

This all works fine - EXCEPT when I first create the text file, and then try to click on its name in the listbox.
I then get informed of an NilObjectException, and that tis = nil.

This is the line of code I get returned to in the debugger:

// OPEN THE FILE CORRESPONDING TO THE LISTBOX SELECTION --------- CAUSES THE ERROR tis = TextInputStream.Open(me.RowTag(me.ListIndex))

I can’t understand how tis = nil???
This listbox’s Change event works perfectly every other time - and only causes the error when I first create the new file???

Could someone please advise me - I have now spent over a day trying to work out this problem alone :frowning:

Thank you all in advance.

I think the problem is that noteName only holds the text of the file name and not a folder item. So, when you access the row tag of the list box you are not giving it a qualified folder item for the open file command.

I would check the value coming back from the row tag call immediately following the creation of the file. It also strikes me that the file holds no data when you have created it. It is possible that this could be causing some issues but I am not sure about that.

Simon - I don’t think it could be that, because it works, at all other times?

For example - if I create a new note, then close and re-open the window (without clicking on the listbox), I can then click on the file name in the listbox and it opens the file successfully.

This problem only happens if I click on the listbox row immediately after I create the file???
It seems I have to close the window. As soon as I do that - the listbox Change event works as expected??

Did that make sense?

Take the statement

tis = TextInputStream.Open(me.RowTag(me.ListIndex))

apart and debug step by step:

Dim index As Integer = Me.ListIndex // might by -1 Dim fi As FolderItem = Me.RowTag(index) // RowTag(index) might by Nil tis = TextInputStream(fi)

[quote=112032:@Richard Summers]// OPEN THE FILE CORRESPONDING TO THE LISTBOX SELECTION --------- CAUSES THE ERROR
tis = TextInputStream.Open(me.RowTag(me.ListIndex))
[/quote]
If this line is causing the error, then the way to determine the cause is to break it apart so you can see each component in the debugger.

First, save Me.ListIndex into a variable. You’ll want to ensure it’s >= 0 before you try to use it with RowTag:

Dim row As Integer = Me.ListIndex
Then you’ll want to assign the RowTag to a string, so you can see it:

If row >= 0 Then Dim file As String = Me.RowTag(row) End If

But TextInputStream.Open takes a FolderItem, not a filename string so you’d have to get a FolderItem from the name, which I don’t see your code doing. But you might be better off just storing the actual FolderItem in the RowTag rather than the name. If you did that then your code would be simpler and perhaps more of what you expect:

If row >= 0 Then Dim file As FolderItem = FolderItem(Me.RowTag(row)) Dim input As TextInputStream = TextInputStream.Open(file) End If

Eli - not quite sure what you mean??

Are you saying that:

  1. There could be no listbox row selected after creating the file.

  2. The row tag might be empty.

Have I understood correctly?
Or did you mean something completely different? :slight_smile:

Paul,
I changed the listbox’s Change event to the code below:

[code] // IF NO LISTBOX SELECTION
if me.ListIndex < 0 then

// SET THE noteView AREA TO READ ONLY
noteView.ReadOnly = true

// BLANK OUT THE noteView AREA
noteView.text = ""



// OTHERWISE - THERE IS A SELECTION

else

Dim file As FolderItem = FolderItem(Me.RowTag(row))
Dim tis As TextInputStream = TextInputStream.Open(file)

// SET THE READ-ONLY FLAG TO FALSE
noteView.ReadOnly = false

// READ THE TEXT FILE'S CONTENT INTO THE noteView AREA
noteView.text = tis.ReadAll

// CLOSE THE FILE
tis.Close

// SET THE VARIABLE lastSelectedRow AS THE SELECTED ROW
lastSelectedRow = me.ListIndex

End If[/code]

Is this what you meant?
If it was - the code produces a This Item Does Not Exist error for b[/b] in the following line:

Dim file As FolderItem = FolderItem(Me.RowTag(row))

[quote=112081:@Richard Summers]If it was - the code produces a This Item Does Not Exist error for (row) the following line:

Dim file As FolderItem = FolderItem(Me.RowTag(row))
[/quote]
Why do you expect it to exist? You haven’t declared a row variable anywhere.

If there is no selection the listbox.listindex = -1.

Bob - changed the first line of code to this:

// IF NO LISTBOX SELECTION if me.ListIndex = -1 then

Thanks.

[quote=112081:@Richard Summers]Is this what you meant?
If it was - the code produces a This Item Does Not Exist error for (row) in the following line:

Dim file As FolderItem = FolderItem(Me.RowTag(row))[/quote]

You’re using Row improperly. You need to use Listindex to figure out which row is selected.

Okay. So here’s what I would do:

dim f as folderitem = me.rowtag(me.listindex) if f = nil or f.exists = false then //no folderitem in row tag! break end

Arghhhhhhhhh :frowning:

This is my listbox’s change event code:

[code] // IF NO LISTBOX SELECTION
if me.ListIndex = -1 then

// SET THE noteView AREA TO READ ONLY
noteView.ReadOnly = true

// BLANK OUT THE noteView AREA
noteView.text = ""



// OTHERWISE - THERE IS A SELECTION

else

Dim file As FolderItem = FolderItem(Me.RowTag(me.ListIndex))
Dim tis As TextInputStream = TextInputStream.Open(file)

// SET THE READ-ONLY FLAG TO FALSE
noteView.ReadOnly = false

// READ THE TEXT FILE'S CONTENT INTO THE noteView AREA
noteView.text = tis.ReadAll

// CLOSE THE FILE
tis.Close

End If[/code]

This give me an error - tis = nil, file = nil???

Show us your code for loading the listbox next.

For what it’s worth, you should use the defensive programming that I used. Folderitems can move/change/be deleted so you should always check for nil and that it exists.

[code]// IF NO LISTBOX SELECTION
if me.ListIndex = -1 then

// SET THE noteView AREA TO READ ONLY
noteView.ReadOnly = true

// BLANK OUT THE noteView AREA
noteView.text = ""



// OTHERWISE - THERE IS A SELECTION

else

Dim file As FolderItem = Me.RowTag(me.ListIndex)
if file = nil or f.exists = false then
  //file does not exist!
   break
end
Dim tis As TextInputStream = TextInputStream.Open(file)

// SET THE READ-ONLY FLAG TO FALSE
noteView.ReadOnly = false

// READ THE TEXT FILE'S CONTENT INTO THE noteView AREA
noteView.text = tis.ReadAll

// CLOSE THE FILE
tis.Close

End If[/code]

The line
Dim file As FolderItem = FolderItem(Me.RowTag(me.ListIndex))

Try
Dim file as Folderitem = me.RowTag(me.Listindex)

Bob,
I used the code you just modified in my listbox, change event.

Here is the code to open the listbox:

[code] // DEFINE THE SUPPORT FOLDER PATH
dim appSupportFolder as FolderItem = SpecialFolder.ApplicationData.child(kAppSupportFolderName)

// CHECK IF THE SUPPORT FOLDER EXISTS
if not appSupportFolder.exists then

// IF NOT, THEN CREATE IT
appSupportFolder.CreateAsFolder

end

// DEFINE THE NOTES FOLDER
NotesFolder = appSupportFolder.child(“Notes”)

// IF THE NOTES FOLDER DOESN’T EXIST - CREATE IT
if not NotesFolder.exists then
NotesFolder.CreateAsFolder
end

// LOAD ALL FILES INTO THE LISTBOX
dim notesCount as integer = NotesFolder.Count
for i as integer = 1 to notesCount

// CHECK AND IF NECESSARY - IGNORE .DS_Store
if NotesFolder.item(i).name <> ".DS_Store" then
  notesList.AddRow(NotesFolder.item(i).name)
  
  // ROWTAGS HOLD THE FOLDERITEM
  notesList.RowTag(noteslist.LastIndex) = NotesFolder.item(i)
end

next[/code]

Kenneth - tried that - to no avail.

// OTHERWISE - THERE IS A SELECTION
else

Dim file As FolderItem = Me.RowTag(me.ListIndex)

if file = nil or f.exists = false then
//file does not exist!
break
end
Dim tis As TextInputStream = TextInputStream.Open(file)

// SET THE READ-ONLY FLAG TO FALSE
noteView.ReadOnly = false

// READ THE TEXT FILE'S CONTENT INTO THE noteView AREA
noteView.text = tis.ReadAll

// CLOSE THE FILE
tis.Close

Richard, when I changed the following it works for me
if file = nil or file.exists = false then

Kenneth - it definitely doesn’t for me. Here is my code:

[code] // OTHERWISE - THERE IS A SELECTION
else

// DEFINE THE VARIABLE file AS TEH SELECTED FILE
Dim file As FolderItem = Me.RowTag(me.ListIndex)

// IF THE FILE DOES NOT EXIST
if file = nil or file.exists = false then
  
  // DISPLAY AN ERROR MSGBOX
  MsgBox("No File Exists")
  break
  
end

// DEFINE tis AS THE SELECTED TEXT FILE
Dim tis As TextInputStream = TextInputStream.Open(file)

// SET THE READ-ONLY FLAG TO FALSE
noteView.ReadOnly = false

// READ THE TEXT FILE'S CONTENT INTO THE noteView AREA
noteView.text = tis.ReadAll

// CLOSE THE FILE
tis.Close

End If[/code]

This causes the error MsgBox to appear - indicating that f=nil or f exists = false.
The problem is that the file DOES exist - because I can see it in the folder???

This is strange…I copied your code into mine and it’s working for me…

Windows or MAC…I’m on Mac…I can test it on Windows if needed…