Simple If/Else Logic which is throwing odd error

Yesterday - I added some functionality to some code which added the
street address, city, state and zip to my users vCalendar which works great
however I get an error:  

NullReferenceException: Object reference not set to an instance of an object.

if the vCalendar doesn't have the street address, city, state and zip filled in - therefore
what should the logic be so that this works whether the information is their or not?

Below I tried to do this with a if/else block figuring that if the street address is filled in
all the other is as well and then display it in the vCalendar - otherwise just display the Description

Due to the odd error I also included how I have the variables set up and what I have each
variable set to:


            string strStreet = "";
            string strCity = "";
            string strState = "";
            string strPostalCode = "";


            strLocation = objEvent.Location;
            strStreet = objEvent.Street;
            strCity = objEvent.City;
            strState = objEvent.State.Abbreviation;
            strPostalCode = objEvent.PostalCode;


if(strStreet != "")
                  {
                        
                        Response.Write("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + strDescription + "=0A" + strStreet + "=0A" + strCity + " " + strState + " " + strPostalCode + "\n");
                        
                  }
                  else
                  {
                        Response.Write("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + strDescription + "\n");
                  }
dylanoneAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mrichmonConnect With a Mentor Commented:
Response.Write("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + strDescription + "=0A" + (strStreet != null ? strStreet + "=0A" : "") + (strCity != null ? strCity + "=0A" : "")+ " " + (strState != null ? strState + "=0A" : "")+ " " + (strPostalCode != null ? strPostalCode + "=0A" : "")+ "\n");

Should only add in the ones that are present
0
 
mrichmonCommented:
If you need something as a blank indicator for ones that are not present adjust the poriton after each : where I have included an empty string
0
 
Bob LearnedCommented:
1) What is objEvent defined as?  

2) How are the properties set?

Bob
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dylanoneAuthor Commented:
Interesting after adding that line of code which I though would work fine - I still get an  error (NullReferenceException: Object reference not set to an instance of an object) even if the street, city, state, zip are null - If I add a state for instance then it works but I can't always say users will eneter in a state.

objEvent is defined as the following:

base.Event objEvent = srv.EventSrv.GetEvent

the properties get set via a control:

<cusc:ConnectDataString Runat="server" ID="CustomLocationControl" DataField="Location" FilterParagraphs="false" /><br />


It's not as if this code doesn't work - it's code that has been working for over a year I'm just adding in some information to make it more user friendly and the error I'm getting isn't very
friendly!



0
 
Bob LearnedCommented:
1) This is ASP.NET

2) .NET version?  2.0?

3)  What is srv?

Bob
0
 
dylanoneAuthor Commented:
1) It is ASP.Net with C# code behinds

2) .Net ver 1.1

3) srv is a tool of classes which are called on the top of the page like such:

using srv = CustomLogic.Connect.Services;

0
 
dstanley9Connect With a Mentor Commented:
You're accessing the State as:

objEvent.State.Abbreviation


if objEvent.State is null it will throw a Null Reference exception.

try

          strState = (objEvent.State== null) ? "" : objEvent.State.Abbreviation;
0
 
Bob LearnedCommented:
1) What is the happening when you get this exception?

2) Are you posting back?

3) There is not enough information here to explain why this exception is happening, but there are a lot of possibilities.

Bob
0
 
dylanoneAuthor Commented:
When I get the exception - I'm clicking a link which is suppose to open up a vCalendar window so that a user can add an event to their calendar -

Interesting now that I look at this more the information that get's populated into the vCalendar for street, city, state, zip is on the previous page and not on the page with the link which adds it to the calendar.

This may explain why when the street, city, state and zip are not filled in - I'm getting an error even with the check for the null as mrichmon posted

I am not posting back

0
 
dylanoneAuthor Commented:
Thanks everyone - finally got this one working for all the desired scenerios
0
All Courses

From novice to tech pro — start learning today.