This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
I am getting an object reference not set even though I am creating a new instance of a list any ideas as to why.
List<QueryCritera> whereClause = new List<QueryCritera>();
whereClause=viewConfig.WhereClause;
foreach (QueryCritera condishion in whereClause)
{
string filedname = condishion.fieldName;
string fieldValue = condishion.Rightvalue;
string operation = condishion.Operation;
}
Your ViewConfig must be null
if(viewConfig != null)
{
whereClause=viewConfig.WhereClause;
}
Your problem is something of the below:
the viewConfig is null
the viewConfig.WhereClause is null.
the viewConfig.WhereClause is not null but contains null values.
In order you find out what of the above is true and act correspondingly, you should debug your code.
though I am creating a new instance of a list
That's true, you create an empty list of QueryCritera objects. However, later you assign to the variable that holds this list, whereClause, the viewConfig.WhereClause, for which something of the things that mentioned above is true and causes the problem.
Related
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 months ago.
I have below code.
var cfg = _cfgUtil.GetCCfg(addr.Cnt, li.txDte);
if (cfg != null && cfg.Isabc)
{
await <some operation>
}
it is failing in second line
if (cfg != null && cfg.Isabc)
what Im not getting is , how to bypass this NUll reference check
exact error Im getting is :
System.NullReferenceException: 'Object reference not set to an
instance of an object.'
I would say your Isabc is what has the null value.
That would need checking also, cfg.Isabc != null
Alternatively, if you are using a newish version of C# the folowing would work, without needing separate checks.
// Option A:
if(cfg != null && cfg.Isabc != null)
{
//...
}
// Option B:
if(cfg?.Isabc is not null)
{
//...
}
This all assumes Isabc is of type bool?, since normally a bool wouldn't be null and other types wouldn't work that way in the if statement.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
How can I handle this null exception?
I have a button that dequeues the data from my static variable class
public ActionResult BtnNext()
{
System.Threading.Thread.Sleep(1000);
var first = MyQueue.todayQueue.Dequeue();
MyQueue.todayQueue.Count();
ViewBag.QueueItem = first;
return View();
}
And here is my view, this shows the data that I dequeued.
#{
var item = (Rosh.QueueMe.Web.Models.MyQueue)ViewBag.QueueItem;
}
#{
if (item != null)
{
//Help here.
}
}
<p>#item.QueueNumber</p>
I still don't know how to handle when my viewbag is still empty / null.
Right now I get this error:
Object reference not set to an instance of an object. / item was null.
The code won't proceed to my index page since it is null.
You either need to check for null for ViewBag.QueueItem as well or use as for reference casting which takes care in case of ViewBag.QueueItem is not being set or is null. So you can adjust it like:
#{
var item = ViewBag.QueueItem as Rosh.QueueMe.Web.Models.MyQueue;
}
now the item will be set to null in case of ViewBag.QueueItem is null
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
Here is my code:
int loanAppFundId = 0;
var loanAppDb = db.LoanApplications.FirstOrDefault(s => s.LoanId == vm.LoanId);
if (loanAppDb != null)
{
if (vm.FundID != null && loanAppDb.FundID !=0)
{
if (loanAppDb.FundID != vm.FundID)
{
string oldFund = "", newFund = "";
loanAppFundId = loanAppDb.FundID;
var oFund = db.Funds.Where(s => s.FundID == loanAppFundId);
}
}
}
In the last line where I'm trying to retrieve fund based on the loanAppFundId is where the exception happens, if I set there 0 or some other variable it proceeds without any issue but whenever I try to set some variable I'm getting the error.
On purpose I initialized variable loanAppFundId with value 0, but again I'm getting error that the reference object is not set to an instance of an object.
If I set some dummy value in the db.Funds.Where query I'm able to get the loanAppDb.FundID value without any issue.
What am I doing wrong? I have multiple checks for the value if it's null or no.
Edit
I am not sure how this one is duplicate since I'm asking with real example, not a generic question.
Obviously even if loanAppDb is not null but loanAppDb.FundID is null. To avoid it you can check loanAppDb.FundID for null too:
if (vm.FundID != null && loanAppDb.FundID!= null && loanAppDb.FundID !=0)
{
//your magic
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
So my code basically sets up a linked list. Each ticket object either stores the reference to another ticket object, or null. The .getNext() method gets the reference to the next object in the list. current is the object representing the start of the list and the while loops go through the list changing current until the conditions change. Finally, it sets current to the Ticket that was passed as a parameter.
public void AddLowPTicket(Ticket ti) // doesnt check if front == null because AddTicket already does
{
Ticket current = front;
while(current.getPrio() == Priority.High && current != null) // cycles/skips through the list as long as Priority == High.
{
current = current.getNext();
}
current.Print(); // *THIS WORKS*
while(current != null && current.getPrio() == Priority.Low) // *NullReferenceException: Obj ref not set to an instance of an obj.*
{
current = current.getNext();
}
current = ti;
}
This is the Print method for the Ticket object. It prints the local variables just fine, which means they aren't null.
public void Print()
{
Console.WriteLine("{0}\nPriority:{1}", m_description, m_prio == Priority.High ? "High" : "Low");
}
Why does it crash if current is not null, and none of its variables are either.
Hi guys thanks very much for your time! I guess it didn't build properly (somehow?). After adding then removing some lines then rebuilding/running it it ran properly! Sorry, and thanks again!
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I'm a bit confused here (undoubtedly due to being new to C# and entity framework but that's besides the point) I want to check for a situation where my query to the database returns no data and upon that happening set some values to variables on my page Below is the code I have up right now which is throwing an error telling me that I need to check for null on the very line where I am checking for null (if the UserName value is null the record was not returned as it is a required column). So what am I missing on how to go about checking for null return here?
using (CInTracDBEntities Context = new CInTracDBEntities())
{
var CInTracUsers = Context.CInTracUsers.Where(a => a.Login == HttpContext.Current.Request.LogonUserIdentity.Name).Select(x => new { x.Login, x.UserName, x.Status, x.StatusDate, x.ReviewDate }).FirstOrDefault();
if (CInTracUsers.UserName == null)
Thanks,
Ken....
ask for the whole object:
if (CInTracUsers == null)
because if CInTracUsers is null you'll get an exception when calling any of his proprties