---
title: "NullReferenceException on String Property"  
description: "NullReferenceException on String Property"  
author: "Anonymous User"  
published: 2013-09-21  
updated: 2013-09-21  
canonical: https://www.mindstick.com/forum/1468/nullreferenceexception-on-string-property  
category: "wpf"  
tags: ["NullReferenceException", "wpf", "mindstick", "String Property", "exception", "nullpointer exception"]  
reading_time: 2 minutes  

---

# NullReferenceException on String Property

In my program I allow the user to add to a [Data Collection](https://answers.mindstick.com/qa/105129/how-to-use-mobile-devices-for-data-collection-in-business) bound to a TreeView. When the user creates the new node they are allowed to assign it a name. The user writes the nodes name in a textBox located in a pop up window. The text from the text box is bound to a [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) in a [ViewModel](https://www.mindstick.com/forum/157215/what-is-viewmodel-in-mvc).

However, I can't seem to get the property or the string in the ViewModel to read the value that it is bound to. It just [shows up](https://answers.mindstick.com/qa/50405/apple-iphone-x-shows-up-liquid-detected-error-when-charging) in the debugger as null, which is when I get the exception. I've created other [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) exactly like this, but for some reason this one doesn't want to work.

## XAML:

<TextBox Text="{Binding TransName}" MaxHeight="20" MinHeight="20" Height="20" Margin="142,24,12,40" Name="textBox1" TextChanged="textBox1_TextChanged"></TextBox>

**In the [View Model](https://www.mindstick.com/forum/160237/explain-razor-view-model-binding-how-does-it-work-and-why-is-it-important):**

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) string _nodeName;

//Property for Node Name

public string NodeName

{

get { return _nodeName; }

set

{

_nodeName = value;

NotifyPropertyChange(() => NodeName);

}

}

Still in the View Model, this is the line in which the [Exception occurs](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs)

//hasSpace is a boolean

hasSpace = _nodeName.Contains(" ");

## Replies

### Reply by Sumit Kesarwani

Hi Pravesh,\

One possible solution is to use the property instead of the backing field. This will control what kind of output can be returned and eliminate the NullReferenceException.

Change the property to return empty by default.

```
public string NodeName{     get { return _nodeName ?? string.Empty; }     set     {         _nodeName =value;        
NotifyPropertyChange(() => NodeName);     }}
```

Then change the ViewModel to use the property:

hasSpace = NodeName.Contains(" ");


---

Original Source: https://www.mindstick.com/forum/1468/nullreferenceexception-on-string-property

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
