1

It's after a few days of unsuccessful research that I come back to you. I have a WPF application in dotnet 4.5, and the application settings stored as UserSettings, one parameter is of DateTime type (the expiry date of the token), and for each application launch I compare this parameter with DateTime.Now (to see if the token has expired or I can use it). That's works perfectly in Win 10 even in some Win7, because now i install it in another Win 7 machine, it generates NullReferenceException (for the DateTime Parameter : token_expire_time)

I wanted to do a method that returns a boolean, to see if this parameter was initialized (DateTime dt = new DateTime()) or not, but I can not find how to do it.

Thanks by advance.

public string get_token()
{   
    int result_time = DateTime.Compare(DateTime.Now, Properties.Settings.Default.token_expire_time); // NullReferenceException
    // Check if token is not expired
    if (result_time >= 0)
    {
        // Token expired
        // so get another token
    }
    // return the token setting stored (string)
    return Properties.Settings.Default.token;
}

My question is not a duplicate of What is a NullReferenceException, and how do I fix it?: 1-problem in C# not VisualBasic 2- Problem in Properties.Settings.settings not in declaration of variable in the code

J-Go
  • 115
  • 1
  • 1
  • 8
  • well it is obvious that `Properties.Settings.Default.token_expire_time` is null – Ashkan Mobayen Khiabani Jun 07 '18 at 08:05
  • maybe try adding a null check condition in your code `if(Properties.Settings.Default.token_expire_time == null){ Properties.Settings.Default.token_expire_time = DateTime.Now;}` – vCillusion Jun 07 '18 at 08:09
  • @Ashkan + vCillusion : but if I'm not mistaken the DateTime type is a value type so not nullable. When i launch the application, I put a breakpoint in get_token method, i look that the property has the value {01.01.0001 00:00:00} so not null – J-Go Jun 07 '18 at 08:28
  • C# has Nullable Value types (`DateTime?`). are those 2 different variables `token_expire_time` and the last line `token`? – Typist Jun 07 '18 at 09:24
  • @Typist yes `token_expire_time` is a DateTime and the `token` is a String. for the type `(DateTime?)` exact, but the `token_expire-time` is created in Settings, there is not this type – J-Go Jun 07 '18 at 09:35
  • @J-Go, I tried a sample by creating a settings file with `token_expire_time` as `DateTime` type variable. When I left it blank and tried to read it in my program, I got `System.NullReferenceException`. I was wondering how come you are getting it as DateTime.MinValue (01.01.0001 00:00:00) – Typist Jun 07 '18 at 09:45
  • @Typist The problem that in debug mode (visual studio 2017) i never have been this exception, but when i installed the application in some other systems (win 7), it works in one and thrown exception in another (event observer -> Error -> .Net Runtime -> NullReferenceException in Properties.Settings.Default.get_token_expire_time). The MinValue that i had in the lik [link](https://imgur.com/a/1mDINWt) – J-Go Jun 07 '18 at 10:17
  • Because i can't edit my oun answer, i put it as comment : To resolve my problem, in property settings table, i set as default value a date (exp : 01/01/2018), and it works. Thanks all for your answer – J-Go Jun 07 '18 at 13:44

0 Answers0