NilObjectException for custom data property

Hello,
I am new to xojo and mostly new to programming … therefore I have a lot of questions :slight_smile:
Reading the book “Introduction to programming with Xojo” I decided to start with a ToDo list manager for my training.
So, I created a class “Project” which has, among others, the property “DueDate” of type “Date”.
During startup of the app I generate three objects from this class, to have something to work with and save them in an array.

It looks like this (showing only one object but the code is the same for all three):

  Dim p As Project
  
  p = New Project
  p.Titel = "Impara la vela"
  p.Detail  = "Proviamo a usare un laser sul lago di Lugano"
  p.Status = True
  p.DueDate.Day = 3  <<--- here I get the "NilObjectException"
  MyProjects.Append(p)

I tried to assign a due date in all possible ways I can think of until I simplified to “Day”. Normally the property would get a date from a text field.

So, “p” is my object. “DueDate” is a property of type “Date” and the error means that the object does not exist, correct?
Since “p” does exist (p = New Project) it could have to do with “DueDate”. I learned that “Date” is also a class but I don’t instantiate it … I thought that this is done by some sort of miracle by xojo since it is part of my class :slight_smile:

And yes, I searched the forum, I googled (which leads most of the time back to forum entries) and I scanned the above mentioned book, but I was not able to find a solution.

I know, that it is my fault, but I am not abel to discover it.

Thanks in advance for your help!
Regards,
thomas

Date is also a class, so you new to create a new Date object first:

Dim d As New Date p.DueDate = d

Thanks a lot Paul!
Your answer was not only super fast but did also solve my issue.
Unfortunately I did still not fully understand it though.
What if I would like to set the total seconds for p.DueDate ?
I tried with

p.DueDate.TotalSeconds = 3552924347

and it comes back again with “NilObjectException” for the property “p.DueDate”. All other properties are correctly created (which makes me think that I haven’t understood that date thing :slight_smile:

Any ideas for this?

Thanks in advance,
thomas

If you are getting a NilObjectException then you don’t have an instance of Date to work with. An instance “initializes the class” and is created by using “New”. Instead you are just working with a Date variable that does not yet contain a value.

If you have this code:

Dim d As Date p.DueDate = d
Then you have created a Date instance (d). You are assigning the d instance to the DueDate property of p. So this code also works:

p.DueDate.TotalSeconds = 3552924347
You cannot modify DueDate until you first assign a Date instance to it, because it has not been initialized. You could also initialize it like this:

p.DueDate = New Date p.DueDate.TotalSeconds = 3552924347

The Classes topic in the User Guide might also be helpful.

Thanks again Paul!
It works as you described it, even if I do not fully understand it.
I will read a bit more by following your link to the classes.

Cheers,
thomas