C# Null Reference error when testing for Null??

Hi,
I have this piece of code:

                if (message.ReceivedBy.Address == null)
                {
                    m.ReceivedBy ="-";
                }
                else
                {
                    m.ReceivedBy = message.ReceivedBy.Address; 
                }

Open in new window


Yet I get a Null Reference error when I do the test:

if (message.ReceivedBy.Address == null)

Eh?

This is the error message:

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=DataCollector
  StackTrace:
       at DataCollector.frmDataCollector.GetMailDataFromInbox() in d:\Dev\Kohola\DataCollector\DataCollector\frmDataCollector.cs:line 308
       at DataCollector.frmDataCollector.cmdStart_Click(Object sender, EventArgs e) in d:\Dev\Kohola\DataCollector\DataCollector\frmDataCollector.cs:line 142
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at DataCollector.Program.Main() in d:\Dev\Kohola\DataCollector\DataCollector\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

Any help with what is causing this error and how to fix it would be greatfully accepted.

Siv
SivAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SivAuthor Commented:
Please ignore this question I have twigged what is wrong, it's down to the fact that I have tested for

message.ReceivedBy.Address

when I should have tested for just:

message.ReceivedBy

As it's ReceivedBy that's null and when the compiler evaluates message.ReceivedBy.Address it can't get to Address because message.ReceivedBy is null.

Staring at it for too long and couldn't see teh wood for the trees!

Siv
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
And it would also fail with that error if message was null
0
SivAuthor Commented:
Good point!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.