Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code:
C#
CardexController controller = new CardexController();
controller.Session.Add("cmstr", id);
controller.Index();

I encounter the System.NullReferenceException error on the line:
C#
controller.Session.Add("cmstr", id);


What should I do while id has value and it is not empty?

What I have tried:

Session sounds like an interface so I cannot make a new instance of it. And also I cannot add a value to it.
Posted
Updated 25-Jun-16 0:30am
v2

1 solution

Session property of you CardexController instance is null, undoubtably.
It's time for you to put a breakpoint on the line which initializes the controller, and press F5.

The fact that the Session property expects some instance of an object which implements a given interface (I just extrapolate from what you wrote, as I do not have the formal definition of the class), does not mean that you do not have to provide it with a valid instance.
 
Share this answer
 
Comments
bahman.01 26-Jun-16 2:11am    
I cannot understand what you mean by "Session property expects some instance of an object which implements a given interface"
phil.o 26-Jun-16 4:28am    
Which part of the sentence do you have trouble with?
bahman.01 26-Jun-16 2:15am    
Here is the real code in the first controlller:
public void checkItem(string id)
{
if (Session["cmstr"] == null)
Session.Add("cmstr", id);
else
Session["cmstr"] = id;

CardexController controller = new CardexController();
controller.cmstr = id;
controller.Session.Add("cmstr", id);
controller.Index();
}

and this is the code in the second controller named "CardexController" :
public ActionResult Index()
{
var item = Session["cmstr"];
if (item == null)
return PartialView("~/Views/RcvAccount/RcvAccountCustomer/_RcvAccountCustomerPartial.cshtml");
else
return PartialView("~/Views/RcvAccount/RcvAccountCardex/_RcvAccountCardexPartial.cshtml");
}

What should I write in the below function?
public CardexController()
{

}
phil.o 26-Jun-16 4:38am    
Please, avoid code in comments; that's hardly readable. You should use the green "Improve question" button instead, and place the code in there with correct formatting.
From what I see, the problem may be that you create a new CardexController in the checkItem method. Since you assign it to a local variable, this variable becomes null at the end of the method. So, basically your code just creates a new CardexController everytime the method is executed, throwing it away at the end.
Could it be possible that you already have a CardexController instance somewhere else, that you should use instead?
And public CardexController() { ... } is not a function declaration, this is a class definition. I don't know if this mistake is due to a bad wording, or to an incomplete knowledge. I have the feeling that there are some key concepts of OOP programming in general, and MVC specifically, that you still have to learn and understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900