Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Null reference error occurs on reading value from setting file.

C#
<setting name="Last_Selected_Vodafone" serializeas="String">
              <value />
          </setting>


i have this property in setting file . when i try to access this in c#

C#
Properties.Settings.Default.Last_Selected_Vodafone;


i am getting null reference error.. that could be because default value is null and its a datetime property......

how can i set
C#
datetime variable = datetime.now ?

any idea
Posted
Updated 9-Mar-14 21:17pm
v2
Comments
JoCodes 10-Mar-14 3:27am    
What you mean by setting file here?
Raman Midha 10-Mar-14 3:48am    
joCodes setting file here means?

1 solution

Please try is as below.

NOTE: Please do necessary casting if it requires.
SQL
DateTime variable = Properties.Settings.Default.Last_Selected_Vodafone ?? DateTime.Now;

?? Operator

LIVE DEMO : .NET FIDDLE
 
Share this answer
 
v5
Comments
Raman Midha 10-Mar-14 3:26am    
Sorry Sampath, we can not use ?? with datetime . we will have to make nullable type .

i tried to use ?? with datetime in this way

DateTime ? LastSelect=Properties.Settings.Default.Last_Selected_Vodafone;


i am getting Object reference not set to an instance of an object ( NUll reference error)
Sampath Lokuge 10-Mar-14 3:36am    
You're wrong.Please check my LIVE DEMO.You can use ?? with DateTime.
Raman Midha 10-Mar-14 3:39am    
how can we set null value in not nullable types do you think its possible
Sampath Lokuge 10-Mar-14 3:42am    
You're not setting null for the 'variable' know ? If your 'Last_Selected_Vodafone' is null then you set the DateTime.Now for the 'variable'.Which is not null DateTime Variable.Am I right ?Please check and play with the LIVE DEMO.
Raman Midha 10-Mar-14 3:47am    
yes you are right ..but the thing is as i try to access Properties.Settings.Default.Last_Selected_Vodafone;

i get object refernce error .. it means property does not return null value.. its throwing an exception .. so we can not check its null value like this..

i added solution below....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900