Zerotoinfinity
Zerotoinfinity

Reputation: 6540

Object reference null for the same statement in asp.net

I am facing a strange problem while running my application.

I have a class [Say studentInfo], for which I am declaring a instance above the page load [i.e. of page scope]

StudentInfo sInfo;

on Page_load I am calling this instance to call functions of that class and everything is working fine:

ex: string studentName = sInfo.GetStudentId(studentId);

But when I am writing the same above code in the different function on the same page, I am getting this error:

Object reference can not be set to null

private infoList GetInfo()
{
 int studentId = // some logic;
 string studentName = sInfo.GetStudentId(studentId);
}

Upvotes: 0

Views: 92

Answers (1)

The Scrum Meister
The Scrum Meister

Reputation: 30111

You are declaring the variable, but not creating it

try StudentInfo sInfo = new StudentInfo ();

Upvotes: 2

Related Questions