locked
Run Time Execution Error "NullReferenceException was unhandled" RRS feed

  • Question

  • I am new to C#, & so I am in the process of teaching myself. I come from a C/C++ (& some C++/CLI) background.

    The following are code snippets showing the code where the error occurs (during debug).  There is what might look like bad code, but I’ve been trying to instantiate the “path” String in different ways to see if the error goes away – so that’s why it looks so clumsy & also why some of it might not really make sense, since this is just some dummy code, & so not the final code I am going to actually use.

    What I don’t understand is the fact that when this event gets executed, the “path” String already contains a string containing an actual path, however, it results in an error as if this “path” was actually either NULL or empty & thus resulting in the “NullReferenceException” exception being thrown & hot handled. Thanks.

     

    {

        public class Controller

        {

            public bool LogFileIsLegal(String filename)

            {

                bool isLegal = false;

                int len = filename.Length;

                if (len > 0) isLegal = true;

                return isLegal;

            }

        }

     

    }

     

    namespace WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

            public Controller pC;

     

            public Form1()

            {

                InitializeComponent();

            }

     

            private void Start_Button_Click(object sender, EventArgs e)

            {

                String path = new String(' ',512);

                path = Location_ComboBox.Text;

                String filename = "TestFile";

                if(String.IsNullOrEmpty(path) == false)

                {

                    if (pC.LogFileIsLegal(path)) <<== ERROR occurs here ==

                    {

                        Console.WriteLine("Got this far.");

                    }

                }

            }

     

    namespace Process_Y
    Friday, July 30, 2010 5:30 PM

Answers

  • I think it's complaining about pc not path.

    You have declared it but not instantiated it so need something like:

         public Controller pC = new Controller();


    Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
    • Marked as answer by JimyJames Friday, July 30, 2010 5:59 PM
    Friday, July 30, 2010 5:53 PM

All replies

  • I think it's complaining about pc not path.

    You have declared it but not instantiated it so need something like:

         public Controller pC = new Controller();


    Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
    • Marked as answer by JimyJames Friday, July 30, 2010 5:59 PM
    Friday, July 30, 2010 5:53 PM
  • Cool! I am going to have to learn how to better interpret the C# error messages. Thanks. -jj

    Friday, July 30, 2010 6:01 PM