1

Getting NullReferenceException on Microsoft.windows.controls.MessageBox when i click the arrow(up or down) in the vertical scrollbar

List<string> errors = new List<string>();

errors = selectedJob.ValidationErrors;
if (errors != null && errors.Count() > 0)
{
    var msg = string.Join(Environment.NewLine,  errors);                                
    MessageBoxResult result =  Microsoft.Windows.Controls.MessageBox.Show(msg, title, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
}

Any suggesstions would be helpful, I can't seem to figure out whats wrong here. Thanks in advance

Hafeez S.
  • 11
  • 1
  • which line do you hit nullref error on ? its a very generic error hence provide as much information as possible. thanks. – Ravi Sankar Raju May 25 '16 at 22:29
  • Is this inside the event method for the up/down errors? And what line of your code is it throwing an exception? And where are you creating/initializing `title`? – Hill May 25 '16 at 22:29
  • MessageBoxResult result = Microsoft.Windows.Controls.MessageBox.Show(msg, title, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } – Hafeez S. May 25 '16 at 22:30
  • string title = string.Format("{0} {1}", "Missing fields in Job - ", _jobRepository.selectedJob.jobData.JobFileIO.JobName); – Hafeez S. May 25 '16 at 22:31
  • MessageBox.show is what is throwing the message – Hafeez S. May 25 '16 at 22:31
  • could you pease post the full message of the exception? seems to be a verry strange case... – whymatter May 25 '16 at 23:23
  • System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Windows.Controls.MessageBox.Button_Click(Object sender, RoutedEventArgs e) – Hafeez S. May 26 '16 at 14:20
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Craig W. May 26 '16 at 16:28

1 Answers1

1

You are creating a new empty list, but then assigning that object to selectedJob.ValidationErrors. Did you mean to use errors.AddRange(selectedJob.ValidationErrors); ?

(btw, you should use errors.Any() instead of errors.Count() > 0)

And does using title ?? String.Empty not throw the exception?

Protiguous
  • 89
  • 2
  • 9
  • I tried errors.AddRange(selectedJob.ValidationErrors) but that didn't fix the issue. Also "title" is not String.empty, it is getting filled in with some static text. See my comment above for details on the "title" – Hafeez S. May 26 '16 at 19:07