Windows 11 Xojo 2024R4.1
I have a container with property of changes as dictionary.
I try to add a key pair with this code in the saveap method within the container.
changes.value("Saved") = "The invoice was saved."
But it errors on that line with nilobjectexception. What am I doing wrong?
Thanks
Do you have this code somewhere?
changes = new dictionary
I do not. I did not see that anywhere in the Help but I will give it a try.
Thanks
You can put the code I wrote just before your changes.value… code.
Before you can add items to your dictionary you need to make an instance of it with New.
From the documentation:
The most common type of exception that occurs is a NilObjectException. This exception occurs when you attempt to use an object but don’t have an instance of it.
Ok, So that works. But when I try to read the values I get a nilobjectexception. Do I need NEW again before I read the values?
I guess it depends on your program and where do you create the instance for the dictionary.
If you are reading values (to create the dictionary) you need the dictionary to have an instance.
Please read on the concept of Scope. Variables cease to exist when their scope ends.
I have changes as dictionary as a property in the container. I then create a key value pair when a change is made to any field or if the save button is pressed. I then want to read those values from the dictionary and save them to a log text file. The log file can be written to at the end of the save method.
Here’s a quick demo project that shows how to retain the dictionary:
dict.xojo_xml_project
Writing the dictionary out should just be iterating the Keys. Give a shout if you get stuck.
It is working now. Thanks for the help.
Oh, and if this a settings file of some kind, you can make the Read/Write super easy by using JSONItem instead of Dictionary. It works almost exactly the same, but you can quickly write out the item with .ToString and read the string in using .Load or the Constructor.
To be fair, you can do the same thing with Dictionary but by using JSONItem your code becomes self documenting because you’ll know exactly what format is being read and written.