We help IT Professionals succeed at work.

nullreferenceexception was unhandled by user code

paulwhelan
paulwhelan asked
on
801 Views
Last Modified: 2008-01-09
hi

i do

if ((_oDateTimeDataType.InitialValue.ToString() == "") || (_oDateTimeDataType.InitialValue == null))
           
I get a crash and it says
Object reference not set to an instance of an object.

When I hover over InitialValue (at both points in my code) its set to null.

Thanks
Paul
Comment
Watch Question

Commented:
Since an object is null - it means that the instanse is not created, thereof you can't refer to its methods or properties.

For example, if you have

String myName;

Console.Write(myName.ToString()) //will throw an exception

However,

String myName = "";

Console.Write(myName.ToString()) //will NOT throw an exception

Commented:
What you need to do is the following:

if(_oDateTimeDataType.InitialValue != null){
  //invoke methods
}else{
 //treat the case when its empty
}

Author

Commented:
Heres the full code which I thought made sense.

Thanks
Paul


if ( (_oDateTimeDataType.InitialValue.ToString() == "") || (_oDateTimeDataType.InitialValue == null) )
            //if the initial value is blank in the xml
            {
                _oBlankCheckBox.Checked = true;
                _oDateTimeCheckBox.Checked = false;
                _oDateTimeTextBox.Enabled = false;
            }
            else
            {
                _oBlankCheckBox.Checked = false;
                _oDateTimeCheckBox.Checked = true;
                _oDateTimeTextBox.Enabled = true;

                //only do this here as if done above will create an initial value when its not in the XML
                _oInitialValue = Convert.ToDateTime(_oDateTimeDataType.InitialValue);
                //is in format dd/mm/yyyy hh:mm:ss in XML, load as dd/mm/yyyy
                _oDateTimeTextBox.Text = _oInitialValue.ToShortDateString();

            }
@sbereli: Just a note: Your first example (with "String myName;") can't be compiled! The compiler is good enough to see those simple errors. If You extend Your sample to "String myName = null;" it would compile and crash... even if it is obvious for humans to see this error.

@paulwhelan: The problem is the order of Your comparisons in the if-clause (theoretically there should not be one). To be on the 100% save side use the example from sbereli's second response.

On the other hand most would write the test like "if ((myString != null) && (myString != string.Empty)) { /* valid string */ } else { /* invalid string */ }".
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.