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
Related
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
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I have a program in which I keep getting the NullReferenceException error. Here is the code that is causing the error:
string description = "";
if (string.IsNullOrEmpty(eventItem.Description.ToString()))
{
description = "No description available.";
}
else
{
description = eventItem.Description.ToString();
}
I have looked through this post (What is a NullReferenceException and how do I fix it?), and I have tried several of the solutions (I'm afraid I simply don't understand all of them enough to try them), but I just can't figure out why this is happening. In my understanding, this error occurs because the string is in fact null.
There are events on my Google Calendar that have no description entered, so the description is null, but shouldn't the code I have check for that, and handle it? Or, is the problem that eventItem.Description.ToString() cannot be null when I call the IsNullOrEmpty method? I have also tried changing the if statement to this:
if (eventItem.Description.ToString() == null)
...but I still get the NRE error. I have tried rewriting my code so many different ways, but nothing has worked. I'm at the end of my rope!
Your eventItem itself may be null. Do something along the lines
if ( eventItem!= null && eventItem.Description != null && eventItem.Description.ToString() == null)
Updated after DStanley pointed out in the comment that .ToString() == null comparison would be unnecessary.
if ( eventItem!= null && eventItem.Description != null)
{
}
Just to help you understand a little better... NullReferenceException basically means you are trying to use some object without instantiating it. One of the simplest way to prevent is to add a null check if you are not sure whether the object is not null. And when I say adding a null check, it simply means comparing to null in the if block before accessing any property of the object.
if( objectName != null)
{
//then do something on the object
}
shouldn't the code I have check for that, and handle it?
Not if item or item.Description is null. If item is null then the call to .Description will throw a null reference exception, and if item.Description is null then the call to ToString will throw a null reference exception. There's no "magic" that lets you call ToString on a null reference.
Note that if item.Description is already a string then there's no need to call ToString(). just do:
if (string.IsNullOrEmpty(eventItem.Description))
{
description = "No description available.";
}
else
{
description = eventItem.Description;
}
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.