Identify String got from <AppSettings> in web.config is Null

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear All,

I am facing a problem,
I would like to do something after getting a value of a key from AppSettings
in web.config file.
I'd also like to do something if there is no any key in the AppSettings.
I know that if there is no any key in the web.config, the string will be null.
So it will throw exception "null pointer reference"

I would like to know how to code the program to do something while it throw
exception.

Thanks
 
If I understand you correctly, which I am not sure,

try {
String maybeThere = ConfigurationSettings.AppSettings["AreYouThere"];
if (maybeThere != null) {
//Well the string is not null, why dont I use it.
}
else {
//String is null. Dont use it.
}
}
catch (Exception e) {
for any exceptions..
}


The null exception occurs only if you blindly attempt to use a reference
without checking its validity. So, you can avoid it by just checking for not
being null.
 
Back
Top