0

I put some properties in the App.xaml.cs file which I am using to store data and populate textboxes as I navigate through my application:

    public String appRXName { set; get; }
    public String appRXNumber { set; get; }

Originally I had a pivot control that called different pages to gather data, but then I moved that pivot control item off to its own page that still calls other pages to collect data. Now when I run the application I get an error.

Basically it was working when I had it inside the original Pivot control. Once I moved it to a separate page (pivot page calles it) then I started to get this error:

System.ArgumentNullException was unhandled Message=Value can not be null. Parameter name: Text

No matter what page I hit always the second item in the list displays the error.

txtRxNotes.Text = (Application.Current as App).appDosageNotes;
txtQuantity.Text = (Application.Current as App).appQuantity.ToString();

I found something online about a RootVisual but I'm not sure if that is what I looking at or not. Does anyone have any ideas?

Derek Lakin
  • 16,179
  • 36
  • 51
webdad3
  • 8,893
  • 30
  • 121
  • 223
  • Before the exception is thrown, what are the values of **appDosageNotes** and **appQuantity**? Are either of them null? – Derek Lakin Feb 15 '11 at 12:40
  • Don't use `(Application.Current as App).Foo`, use `((App)Application.Current).Foo` instead. In first case you get null reference exception if `Application.Current` is not `App` - which is wrong, because it is actually just invalid type. Use `as` operator only when you need to know if you can cast to given type and then use the casted object. Also are you sure txtRxNotes/txtQuantity is not null? – Lukáš Novotný Feb 15 '11 at 12:44
  • @Derek - They might both be null. This is happening when I first use this screen to start adding data. Do I need to wrap a check around each property to check if it is NULL? Is there a way to do more of a global check? – webdad3 Feb 15 '11 at 13:08
  • Why is this happening after I moved my code from the mainpage (pivot) to a separate page being called by my pivot? – webdad3 Feb 15 '11 at 13:12

1 Answers1

2

The ArgumentNullException is being thrown because the value that you are trying to set for the Text property is null, which you cannot do; the Text property is not a nullable type.

Without knowing how and when these application-level properties are being set it's difficult to provide a good explanation of why the behavior is different since your refactor, but you could either:

  • Put a null check in the code that accesses these application-level properties.
  • Initialise the application-level properties to string.Empty in the application constructor.
Derek Lakin
  • 16,179
  • 36
  • 51
  • I wrapped a null check around everything and that seemed to work... However, I like the string.Empty fix when I initialize the application-level properties. I'll try that too. Thanks for your help! – webdad3 Feb 15 '11 at 13:38