1

I'm getting a null reference exception on the line which I have indicated with a comment. Does anyone have an idea as to why this is happening?

var form = Form.ActiveForm as Form1;
var doSave = MessageBox.Show("Would you like to save this measurement?",
                             "Save Measurement",
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question);

if (doSave == DialogResult.Yes) // User wants to save the current measurement.
{
    curName = ShowDialog("Measurement Name", "Save Measurement");
    // ERROR HERE **** 
    int ret = form.databaseClass.GetFirstSoundReadyTestOccurrence(form.testNumber.Text);
Prix
  • 19,417
  • 15
  • 73
  • 132
  • 3
    post the exception details, including the stack trace--otherwise people can only guess at your problem. – Peter Ritchie Jul 16 '13 at 20:58
  • I will in just a second. –  Jul 16 '13 at 20:59
  • 1
    I imagine `form` is null, so `Form.ActiveForm` is null or not an instance of `Form1`. Otherwise, `form.databaseClass` or `form.testNumber` is null. – Lee Jul 16 '13 at 20:59
  • could be `form`, could be `databaseClass`, could be `testNumber, could be `Text`, could be something in `GetFistSoundReadTestOccurance`... – Peter Ritchie Jul 16 '13 at 21:01
  • 1
    http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net - debug your code to see which object is null. – James Jul 16 '13 at 21:03
  • I just tried to cause my program to crash in the same way twice and it didn't. –  Jul 16 '13 at 21:04
  • I also noticed that the form will randomly disappear so I'm assuming it's the form. –  Jul 16 '13 at 21:06
  • 1
    If it doesn't reproduce all the time: are you sure the active form is always an instance of Form1? Otherwise `Form.ActiveForm as Form1` will return null and you'll get a NRE later on. – jods Jul 16 '13 at 21:06
  • I think that's what it is... I believe both times that it crashed it was because I minimized the form. Sorry, I'm a noob... –  Jul 16 '13 at 21:10
  • @sheaKelse There is nothing wrong with being a beginner, but I'd encourage you to read up on basic usage of the debugger. It is *meant* to help you with problems like this. In cases like this you will likely find useful breakpoints, the local variables (locals) view and exception catching (you usually get that last for free just by running a debug build through the debugger; Visual Studio has both "debug" and "start without debugging" for a reason, and most IDEs have similar options). – user Jul 16 '13 at 21:13
  • 1
    What you can learn from the comment by @jods is _never use the `as` operator for a cast if you are not going to handle explicitly the situation where the result is `null`._ Use a cast of the form `(Form1)(Form.ActiveForm)` instead (last parenthesis is not required). – Jeppe Stig Nielsen Jul 16 '13 at 21:18
  • 1
    @JeppeStigNielsen gives you a good tip. Either use `as` and check for the possible null value, or use the cast `(Form1)`. Benefit of the latter is that if your cast is invalid it will fail early with a very explicit exception. It's ok to be a beginner, we all had to learn: good luck with your project! – jods Jul 16 '13 at 21:20

1 Answers1

4

I would bet this is a case of the Form.ActiveForm not being of type Form1.

This should be easy to reproduce. Just make sure your active form is not of type Form1 before getting into this code. In other words... make sure some other form is active when this code gets run.

The edits below will fix when this occurs but you really want a more reliable way to get the form. Without knowing more about your design it's difficult to recommend a better way of getting the form.

var form = Form.ActiveForm as Form1;
if (form != null)
{
var doSave = MessageBox.Show("Would you like to save this measurement?",
                             "Save Measurement",
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question);

if (doSave == DialogResult.Yes) // User wants to save the current measurement.
{
    curName = ShowDialog("Measurement Name", "Save Measurement");
    // ERROR HERE **** 
    int ret = form.databaseClass.GetFirstSoundReadyTestOccurrence(form.testNumber.Text);
}
}
Kevin
  • 4,586
  • 23
  • 35