This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
What I am trying to do Is, find an object inside a list
Log q = logs.Find(x => (x.dnsq.queryID.Contains(queryid)));
However when I do it, I get this error
x.dnsq.queryID = 'x.dnsq.queryID' threw an exception of type
'System.NullReferenceException
The Log class looks like this
class Log : LogCtrl
{
public int id { set; get; }
public string source { set; get; }
public string destination { set; get; }
public string protocol { set; get; }
public double time { set; get; }
public string info { set; get; }
public DNSresponse dnsr { set; get; }
public DNSquery dnsq { set; get; }
}
It sounds like either x or x.dnsq or x.dnsq.queryId is null for at least one item in logs. You can check for this in your statement by doing the following:
Log q = logs.Find(x => (x != null && x.dnsq != null && x.dnsq.queryID != null && x.dnsq.queryID.Contains(queryid)));
Related
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 27 days ago.
I got a class who looks like this.
public class PostUndKey
{
public string Key { get; set; }
public List<int> Id { get; set; }
public List<string> von_datum { get; set; }
public List<string> bis_datum { get; set; }
}
In my Code i use something like this.
PostUndKey x = new PostUndKey();
var z = 42;
x.Id.Add(z);
And i always get the Null Reference Exception.
Can someone explain this to me pls i dont get it.
Thanks
You need to create an instance of List<int> and assign it to Id property. List<T> is a reference type and default value for reference type is null. For example:
PostUndKey x = new PostUndKey();
x.Id = new List<int>();
var z = 42;
x.Id.Add(z);
Or initialize Id for PostUndKey instance creation:
public class PostUndKey
{
public string Key { get; set; }
public List<int> Id { get; set; } = new List<int>();
public List<string> von_datum { get; set; }
public List<string> bis_datum { get; set; }
}
Read more:
Auto-Implemented Properties
Reference types
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed last month.
Here I have this class which is basically a data structure:
public class Config
{
public uint? Release { get; set; }
public string InstallPath { get; set; }
public uint? CheckEveryXMinutes { get; set; }
public override string ToString()
{
return $"(Release: {Release}, InstallPath: {InstallPath}, CheckEveryXMinutes {CheckEveryXMinutes})";
}
}
And I want to use this structure as property of another class like this:
public class ConfigRegistry
{
public Config Result { get; set; }
public ConfigRegistry (string registryKey)
{
Result.Release = 0;
Console.WriteLine("Read Release from registry: {0}", Result.Release);
}
}
And I'm getting an error at "Result.Release = 0" line:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
What am I doing wrong here?
I needed to add
Result = new Config();
This question already has answers here:
The model item passed into the dictionary is of type .. but this dictionary requires a model item of type
(7 answers)
Closed 3 years ago.
The details are being fetched in the controller correctly but value passed in the view is incorrect. Therefore I am getting the following error:
The model item passed into the dictionary is of type 'System.Int64', but this >dictionary requires a model item of type >'estatebranch.ViewModels.RentRegisterViewModel'
My controller has following action method
public ActionResult RentRegister(int propertyId)
{
try
{
using (estatebranchEntities db = new estatebranchEntities())
{
RentRegisterViewModel objRentRegister = new RentRegisterViewModel();
objRentRegister.balance = db.PropertyDetails.Where(m => m.propertyid == propertyId).Select(m => m.balance).SingleOrDefault();
return View(objRentRegister.balance);
}
}
catch (Exception)
{
throw;
}
}
My View model is as follow:
public class RentRegisterViewModel
{
public int recordid { get; set; }
public int propertyid { get; set; }
public int month { get; set; }
public int year { get; set; }
public long amountpaid { get; set; }
public System.DateTime paymentdate { get; set; }
public long interest { get; set; }
public Nullable<long> balance { get; set; }
public virtual PropertyDetail PropertyDetail { get; set; }
}
try changing this line:
return View(objRentRegister);
This question already has answers here:
What is the best way to give a C# auto-property an initial value?
(23 answers)
Closed 5 years ago.
What i'm trying to do is create a new model that will have certain features:
public class GenericGoal
{
public int _id { get; set; }
public List<String> Type_of_Goal { get; set; }
public DateTime Date { get; set; }
}
my quick and small question would be, how would I prepopulate the Type_of_goal field?
You could use a constructor for this; could look like this example.
public class GenericGoal
{
public int _id { get; set; }
public List<String> Type_of_Goal { get; set; }
public DateTime Date { get; set; }
public GenericGoal()
{
Type_of_Goal = new List<String>();
}
}
I recommend the initialisation in the constructor because in my opinion it's cleaner and better readable.
You could give the property 'Type_of_Goal' an initial value, if you don't want to have to initialize it every time, or initialize it when no value is set to it.
public class GenericGoal
{
public int _id { get; set; }
public List<String> Type_of_Goal { get; set; } = new List<String>();
public DateTime Date { get; set; }
}
If it is 'NullReferenceException' you are concerned about, check for null's before accessing the 'Type_of_Goal' property, as follow. Note the ? - Known as Null Propagation Operator
genericGoalInstance.Type_of_Goal?.Add("Good Goal");
This question already has answers here:
"Specified cast is not valid" error in C# windows forms program
(9 answers)
Closed 6 years ago.
I got the following code:
var model = new List<LogModels>();
while (reader.Read())
{
var logContent = new LogModels(
(int)reader["id"],
(string)reader["response"],
(string)reader["language"],
(string)reader["country"],
(string)reader["location"],
(string)reader["os"],
(string)reader["browser"],
(string)reader["type"],
(DateTime)reader["date"]
);
model.Add(logContent);
}
But I get the following error:
An exception of type 'System.InvalidCastException' occurred in Speed Services.dll but was not handled in user code
Additional information: Specified cast is not valid.
Image: https://i.gyazo.com/adf04b8e271bb53c93303d5774a48f80.png
My model (LogModels) looks like this:
public int id { get; set; }
public string response { get; set; }
public string language { get; set; }
public string country { get; set; }
public string location { get; set; }
public string os { get; set; }
public string browser { get; set; }
public string type { get; set; }
public DateTime date { get; set; }
Does someone know what the issue could be since I can't seem to find the problem?
var model = new List<LogModels>();
while (reader.Read())
{
var logContent = new LogModels(
Convert.ToInt32(reader["id"].Tostring()),
(string)reader["response"],
(string)reader["language"],
(string)reader["country"],
(string)reader["location"],
(string)reader["os"],
(string)reader["browser"],
(string)reader["type"],
Convert.ToDateTime(reader["date"].Tostring())
);
model.Add(logContent);
}
casting problem because of integer or datetime value.
(int)reader["Id"] instead of Convert.ToInt32(reader["Id"].Tostring())
(DateTime)reader["date"] instead of Convert.ToDateTime(reader["date"].Tostring())