Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:47:27+00:00 2026-06-01T18:47:27+00:00

I recently had a coding bug where under certain conditions a variable wasn’t being

  • 0

I recently had a coding bug where under certain conditions a variable wasn’t being initialized and I was getting a NullReferenceException . This took a while to debug as I had to find the bits of data that would generate this to recreate it the error as the exception doesn’t give the variable name.

Obviously I could check every variable before use and throw an informative exception but is there a better (read less coding) way of doing this? Another thought I had was shipping with the pdb files so that the error information would contain the code line that caused the error. How do other people avoid / handle this problem?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:47:28+00:00Added an answer on June 1, 2026 at 6:47 pm

    Firstly: don’t do too much in a single statement. If you have huge numbers of dereferencing operations in one line, it’s going to be much harder to find the culprit. The Law of Demeter helps with this too – if you’ve got something like order.SalesClerk.Manager.Address.Street.Length then you’ve got a lot of options to wade through when you get an exception. (I’m not dogmatic about the Law of Demeter, but everything in moderation…)

    Secondly: prefer casting over using as, unless it’s valid for the object to be a different type, which normally involves a null check immediately afterwards. So here:

    // What if foo is actually a Control, but we expect it to be String?
    string text = foo as string;
    // Several lines later
    int length = text.Length; // Bang!
    

    Here we’d get a NullReferenceException and eventually trace it back to text being null – but then you wouldn’t know whether that’s because foo was null, or because it was an unexpected type. If it should really, really be a string, then cast instead:

    string text = (string) foo;
    

    Now you’ll be able to tell the difference between the two scenarios.

    Thirdly: as others have said, validate your data – typically arguments to public and potentially internal APIs. I do this in enough places in Noda Time that I’ve got a utility class to help me declutter the check. So for example (from Period):

    internal LocalInstant AddTo(LocalInstant localInstant,
                                CalendarSystem calendar, int scalar)
    {
        Preconditions.CheckNotNull(calendar, "calendar");
        ...
    }
    

    You should document what can and can’t be null, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had to take a quick look at Adobe InDesign server. In this
This task has already been asked/answered, but I recently had a job interview that
I had a frustrating problem recently that boiled down to a very simple coding
I've recently asked and had answered this question and it's stopped the issue of
I recently had this problem about scrolling and focus changing. I solved it by
I had recently participated in coding competion sponsored by an company and there was
I recently had this idea to create a C# Windows Form App that enables
I recently ran into an embarrassing situation with some Flash coding where I had
I recently had help getting the correct Date format on my jQuery / tablesorter
I recently had a bug in Firefox due to me choosing the wrong script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.