foreach iterator null when looping through collection [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 2 years ago.
I am using Visual Studio 2017 with the ASP.NET Core 2.1 Framework. When I try and loop through a static object I have in my controller, the iterator seems to be null.
The static object I am calling definitely has a collection seen below:
However When I try and see what the details are of the iterator I get a null reference exception even through the iterator should have a value:
What would be the cause of this null reference?
EDIT: Thanks to the answer I was able to debug what the issue was.
In the code where I am setting the bool wasFound I am doing a where query against a list of objects where the name of the incoming connection is in the list. In this case, the connection name was null and so it would fail.
Obviously the null reference error was a bit vague as to what was causing the error.

The problem is not with i itself, most probably the incoming instance is null, you can use ?. operator in this case:
i?.incoming?.connection

Related

Getting a NullReferenceException while script is functioning and following traditional debugging methods aren't effective [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I'm trying to do a little bit of cleaning up and I seem to be getting the follow
NullReferenceException: Object reference not set to an instance of an
object DestroyCollision.OnCollisionEnter2D (UnityEngine.Collision2D
collision) (at Assets/Scripts/DestroyCollision.cs:66)
Now though, even though the entire script works perfectly (there's literally no bugs or anything) and the object reference is set correctly (since the object in question gets its active state set to false), I am stumped as to why there are any issues. The following code is the line in question:
if (collision.gameObject.tag == "Shield")
{
GameObject.FindGameObjectWithTag("Shield").SetActive(false);
Destroy(this.gameObject);
}
The shield object is properly tagged, and as I said, setactive(false) gets applied. Line 66 is the gameobject.find... of that line of code there.
Why am I getting the error and how to fix it?
Are you sure you mean to have Destroy(this.gameObject); instead of Destroy(collision.gameObject);?
We know collision.gameObject is not null there, but you provide no info on what this.gameObject is.

Check null within chain of object accessing [duplicate]

This question already has answers here:
C# null check chain in method call
(4 answers)
Closed 3 years ago.
How Can I check null for each object I am using in below chain?
forensicId = Message.Events.SMS.SMS_Mappings.FirstOrDefault().Bug.ForensicId;
More details: I want to access ForensicId from (tables/Proxies loaded by entity framework) BUG which is part of a SMS_Mappings and SMS_Mappings are again part of some table.
Is there any way where I can check if Message is not null or of events are not null and SMS is not null and so on within a single line.
Try this forensicId = Message?.Events?.SMS?.SMS_Mappings?.FirstOrDefault()?.Bug?.ForensicId; It returns null if any object in chain is null or ForensicId value if everything is ok. There is a nice article about such scenarios

ASP.NET MVC correct way to use values that may or may not have the value of null? [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
What is the correct way to use values that may or may not have the value of null?
I have this piece of code that sends an email to a manager if the employee who is taking a holiday request has a manager.
if(holidayRequestForm.Employee.Site.SiteManagerEmail != null)
{
SendMailToManager();
}
However this is causing a NullReferenceException.
What would be the correct way to implement calling the SendMailToManager() if that Employee has one without causing a NullReferenceException.
Is it bad practise to use possible null values this way?
Take a look at Null-conditional operator ?. in the docs in order to understand how you can rewrite your if statement (avoiding NullReference exceptions).
if(holdayRequestForm?.Employee?.Site?.SiteManagerEmail != null)
{
SendMailToManager();
}
I don't find it a bad practice. I do not think you have any other possibilities in the described situation.

C# Android Unhandled Exception Error System.NullReferenceException [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
I am trying to get Database Data to insert into List View
Error Code:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object. occurred
UPDATE 1:
Either shMsg or sqldb is null.
Put a breakpoint on line shMsg = FindViewById<TextView> (Resource.Id.shMsg);Then check if your resource is filled, and if you step 1 execution further, if the variable shMsg has a value.
Then proceed stepping until you realise which object has a null value.
The reason you get this exception is because you're trying to get a property of something that doesn't exist, because it's null.
If the sqldb variable is null, make sure you don't dispose of the connection to the database prematurely, which is a common mistake.

Is Nullification Advisable? [duplicate]

This question already has answers here:
Setting Objects to Null/Nothing after use in .NET
(15 answers)
Closed 8 years ago.
I don't mean to start a[nother] civil war over this issue, but should nullification be enforced or not?
While trying to fix a bug (that was fixed elsewise, as can be seen here), I added code like this in a couple of places to immediately and explicitly set some objects to null:
List<String> XMLFiles = CCRUtils.GetXMLFiles(fileType, #"\");
foreach (string fullXMLFilePath in XMLFiles)
{
RESTfulMethods.SendXMLFile(fullXMLFilePath, uri, 500);
}
XMLFiles = null;
Now that I know that did not solve the problem I was having, should I just leave it, or remove this type of code?
should nullification be enforced or not?
In general, it should not. There are very few circumstances where there is a reason to set a variable to null. As soon as the scope in which the variable is declared ends, the reference to the object will no longer be held.
Setting a variable to null after use is (almost) always just a misunderstanding of how the GC works, and serves no purpose.

Categories