Get type of null reference object for Object reference not set to an instance of an object
Tag : chash , By : Arun Thakkar
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Um... Because it's null. In C#, a reference type is a pointer to something. A null pointer isn't pointing to anything. You are asking, "What type of thing would this point to if it was pointing to something". That's sort of like getting a blank sheet of paper, and asking, "What would this say if it had something written on it?" MyClass myobj = null;
int hash = myobj.GetHashCode();
SomeFunc(MyClass myparam)
{
if (myparam == null)
throw new ArgumentNullException("myparam must be a MyClass object");
}
SomeFunc(MyClass myparam)
{
if (myparam == null)
throw new ArgumentNullException("myparam", typeof(MyClass));
}
public class MyArgumentNullException : ArgumentNullException
{
public MyArgumentNullException(string name, Type type)
:base(name + "must be a " + type.Name + " object");
}
|
C# Recursive method returning null reference, object reference not set to an instance of an object. XElement Objects
Date : March 29 2020, 07:55 AM
To fix this issue That doesn't look good. Are you sure that there are things that are not in any category in your table? If so you shouldn't be selecting them since you are doing all the operation on the categoryId. I guess as Chris mentioned, you are getting this exception because you are on a parent category which doesn't have another parent. You need to put an if condition and not parse if it is a parent category. Something like this: private XElement FillSubCatagories(XElement cat) {
IEnumerable<XElement> list = cat.Descendants();
int catTypeID = c.Attribute("CatTypeID") == null ? 1 : Int32.Parse(cat.Attribute("CatTypeID").Value);
foreach (XElement c in list) {
if(!String.IsNullOrEmpty(cat.Attribute("CatID").Value))
{
int parentID = Int32.Parse(cat.Attribute("CatID").Value);
XElement sub = new XElement("sub",
from s in db.Categories
where s.CatTypeID.Equals(catTypeID) && s.ParentID.Equals(parentID)
select new XElement("Category",
s.CatID == null ? null : new XAttribute("CatID", s.CatID),
s.ParentID == null ? null : new XAttribute("ParentID", s.ParentID),
s.CatTitle == null ? null : new XAttribute("CatTitle", s.CatTitle),
s.CatTypeID == null ? null : new XAttribute("CatTypeID", s.CatTypeID),
s.shortDesc == null ? null : new XAttribute("shortDesc", s.shortDesc),
s.longDesc == null ? null : new XAttribute("longDesc", s.longDesc),
s.CatImage == null ? null : new XAttribute("CatImage", s.CatImage)));
c.Add(sub);
FillSubCatagories(c);
}
}
return cat;
}
|
Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instea
Tag : chash , By : user152319
Date : March 29 2020, 07:55 AM
wish of those help When I used the following code in C#... int totalValue = 0;
int total = 0;
totalValue = int.Parse("2".ToString()) * int.Parse("2".ToString());
string s = "Yes";
totalValue += s.Equals("Yes",StringComparison.CurrentCultureIgnoreCase) ? 80 : 0;
|
Object reference not set to an instance of an object error attempting to find an object recursively using LINQ
Tag : chash , By : Janne Laine
Date : March 29 2020, 07:55 AM
hop of those help? You can use either of the following methods to traverse the tree structure recursively: public static IEnumerable<T> Traverse<T>(IEnumerable<T> source, Func<T, IEnumerable<T>> childSelector)
{
var queue = new Queue<T>(source);
while (queue.Any())
{
var item = queue.Dequeue();
yield return item;
foreach (var child in childSelector(item))
{
queue.Enqueue(child);
}
}
}
public static IEnumerable<T> Traverse<T>(T root, Func<T, IEnumerable<T>> childSelector)
{
return Traverse(new[] { root }, childSelector);
}
Category root = new Category();
var searchResult = Traverse(root, item => item.Categories)
.Where(category => category.Name == "testValue")
.FirstOrDefault();
Traverse(root, item => item.Categories ?? Enumerable.Empty<Category>())
|
Test assert fail: Null reference exception. Object Reference not set to an Instance of an object
Tag : chash , By : Eugenio
Date : March 29 2020, 07:55 AM
Hope this helps In your test method, you are not setting a value for LoginView.Menu, which you are doing in Show_Login(). Therefore, when you call Compare_Login() from the test, Menu is null.
|