I'm trying to create a simple generic function:
public T GetPost<T>(HttpListenerRequest request) where T : new()
{
Stream body = request.InputStream;
Encoding encoding = request.ContentEncoding;
StreamReader reader = new StreamReader(body, encoding);
string data = reader.ReadToEnd();
body.Close();
reader.Close();
// NullRefferenceException on this line:
typeof(T).GetField("Name").SetValue(null, "djasldj");
return //yet to come
}
Strangely the line with typeof(T)
return this error:
Object reference not set to an instance of an object.
What is a NullReferenceException, and how do I fix it?
Also how can I return the constructed T class?
This is how I call the function:
string data = GetPost<User>(ctx.Request);
And this is the User
class:
public static string Name { get; set; }
public string Password { get; set; }