StateValue not Initializing

Topics: Web Client Software Factory
May 30, 2007 at 6:40 PM
I am working through the March excercises and such.

At this point, I am on exercise 5 and when I run the application, I am getting a nullreferenceexception on the following code:

public virtual void AddTransfer(Transfer transfer)
{
_transfers.Value.Add(transfer);
}

In debugging, the _transfers.Value is Null. I was under the assumption that the StateValue would be initialized at this point.

Am I missing something somewhere? I have verified that all the code has been put in correctly, etc.

Thanks for any leads/info....

May 30, 2007 at 7:58 PM
Is your StateValue _transfers parameter private or public? The objectbuilder requires this to be public through a public setter property or a public parameter.
May 30, 2007 at 8:02 PM
It's public as defined below:

public StateValue<List<Transfer>> _transfers;


BennyXNO wrote:
Is your StateValue _transfers parameter private or public? The objectbuilder requires this to be public through a public setter property or a public parameter.

May 30, 2007 at 8:53 PM
I just built a QueryStringValue<T> Class that is like the StateValue<T> Class for the WCSFContrib Project, so I am a bit too intimate on how this class works.

A few things.

1) A Session Cache key comes into play here. Unless you specify an attribute on the variable of say SessionStateKey"Transfers" or whatever, the key for storing the value in SessionState is a combination of the class name and field name. This means this value can only be populated and retrieved from the same class. Adding the attribute allows the value to be truly global to the session and can be populated and retrieved from anywhere.

2) Null may mean the value is not in the cache or not being accessed using the right key ( see 1 above ). It doesn't necessarily mean that ObjectBuilder is not doing its thing. By default the variable is set to default(T) where T is its type. Only if it is found in the cache does the value get populated.

3) The class containing the public variable has to be created using ObjectBuilder, which I assume is the case but wanted to mention it.

I haven't looked at the March Exercises, but it may call for a SessionStateKey Attribute on the public field or perhaps it just hasn't been populated yet.

Regards,

Dave

_____________________________

David Hayden
Microsoft MVP C#
May 31, 2007 at 1:09 PM
Here is the code for the EFTController:

namespace GlobalBank.EFT
{
public class EFTController
{
private ITransferService _transferService;
private INavigationService _navigationService;

public StateValue<List<Transfer>> _transfers;

public EFTController()
{
}

InjectionConstructor
public EFTController(ServiceDependencyITransferService transferService,
ServiceDependencyINavigationService navigationService)
{
_transferService = transferService;
_navigationService = navigationService;
}

public virtual DateTime GetLastTransferDate()
{
return _transferService.GetLastTransferDate();
}

public virtual void AddTransfer(Transfer transfer)
{
_transfers.Value.Add(transfer);
}

public virtual List<Transfer> GetTransfers()
{
return _transfers.Value;
}

public virtual void CreateNewTransfers()
{
_navigationService.Navigate("NewTransfersView.aspx");
}

public virtual void ReviseTransfers()
{
_navigationService.Navigate("NewTransferView.aspx");
}

public virtual void ProcessTransfers()
{
transferService.ProcessTransfers(transfers.Value.ToArray());
_navigationService.Navigate("SummaryView.aspx");
}

public virtual void SubmitTransfers()
{
_navigationService.Navigate("ReviewTransfersView.aspx");
}
}
}

The _transfers is being set properly but the Value is always null and so the .Add fails. In looking over the docs from the March Exercises, nothing appears to be amiss. So needless to say, I'm at a bit of a loss. It doesn't appear that a sessionkey is the issue but setting up the sessionkey on transfers allow the code to work either. A null reference exception on .Value is still ocurring.

Pat.


DavidHayden wrote:
I just built a QueryStringValue<T> Class that is like the StateValue<T> Class for the WCSFContrib Project, so I am a bit too intimate on how this class works.

A few things.

1) A Session Cache key comes into play here. Unless you specify an attribute on the variable of say SessionStateKey"Transfers" or whatever, the key for storing the value in SessionState is a combination of the class name and field name. This means this value can only be populated and retrieved from the same class. Adding the attribute allows the value to be truly global to the session and can be populated and retrieved from anywhere.

2) Null may mean the value is not in the cache or not being accessed using the right key ( see 1 above ). It doesn't necessarily mean that ObjectBuilder is not doing its thing. By default the variable is set to default(T) where T is its type. Only if it is found in the cache does the value get populated.

3) The class containing the public variable has to be created using ObjectBuilder, which I assume is the case but wanted to mention it.

I haven't looked at the March Exercises, but it may call for a SessionStateKey Attribute on the public field or perhaps it just hasn't been populated yet.

Regards,

Dave

_____________________________

David Hayden
Microsoft MVP C#

May 31, 2007 at 2:25 PM
I found the problem.


Seems a bit of code was left out of the docs. The .Value property of the _Transfers was not being initialized properly when accessed. I knew it had to be something simple but just couldn't seem to find it.

Thanks!