C# 递归方法返回空引用,未将对象引用设置为对象的实例。 XElement 对象
尝试逐步通过类别列表递归添加 XElement。
XElement dataResponse = new XElement("Categories",
from c in db.Categories
where c.CatTypeID.Equals(catTypeID) && c.ParentID.Equals(null)
select new XElement("Category",
c.CatID == null ? null : new XAttribute("CatID", c.CatID),
c.ParentID == null ? null : new XAttribute("ParentID", c.ParentID),
c.CatTitle == null ? null : new XAttribute("CatTitle", c.CatTitle),
c.CatTypeID == null ? null : new XAttribute("CatTypeID", c.CatTypeID),
c.shortDesc == null ? null : new XAttribute("shortDesc", c.shortDesc),
c.longDesc == null ? null : new XAttribute("longDesc", c.longDesc),
c.CatImage == null ? null : new XAttribute("CatImage", c.CatImage)));
internalData = FillSubCatagories(dataResponse).ToString();
这是第一个类别列表,现在我想递归地提取所有子类别并将它们嵌套在我的 Xelements FillSubCatagories() 方法中:
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) {
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;
}
好吧,当我第二次通过该方法运行 foreach 时,问题就出现了。 intparentID = Int32.Parse(cat.Attribute("CatID").Value);
返回一个 nullreferenceException - “对象引用未设置为对象的实例”
仍然习惯来自 c# java,所以要温柔。我确信这是一个明显的错误,但我还没有看到明确的原因。
<<<<<<<<<>>>>>>>>>>>>>> 编辑 new FillSubCategories() 看起来像这样,
private XElement FillSubCatagories(XElement cat) {
IEnumerable<XElement> list = cat.Descendants();
int catTypeID = cat.Attribute("CatTypeID") == null ? 1 : Int32.Parse(cat.Attribute("CatTypeID").Value);
foreach (XElement c in list) {
int parentID = Int32.Parse(c.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);
if (sub.Descendants() != null) {
FillSubCatagories(sub);
}
}
return cat;
}
这让我走得更远,但我仍然最终达到了空值。
编辑工作方法
private void FillSubCategories(XElement cat) {
IEnumerable<XElement> list = cat.Descendants();
foreach (XElement c in list) {
try {
int catTypeID = Int32.Parse(c.Attribute("CatTypeID").Value);
int parentID = Int32.Parse(c.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)));
try {
string i = sub.Element("Category").Value;
c.Add(sub);
FillSubCategories(sub);
} catch (Exception) {
continue;
}
} catch (Exception) {
continue;
}
}
}
Attempting to recursively add XElements stepping through a category list.
XElement dataResponse = new XElement("Categories",
from c in db.Categories
where c.CatTypeID.Equals(catTypeID) && c.ParentID.Equals(null)
select new XElement("Category",
c.CatID == null ? null : new XAttribute("CatID", c.CatID),
c.ParentID == null ? null : new XAttribute("ParentID", c.ParentID),
c.CatTitle == null ? null : new XAttribute("CatTitle", c.CatTitle),
c.CatTypeID == null ? null : new XAttribute("CatTypeID", c.CatTypeID),
c.shortDesc == null ? null : new XAttribute("shortDesc", c.shortDesc),
c.longDesc == null ? null : new XAttribute("longDesc", c.longDesc),
c.CatImage == null ? null : new XAttribute("CatImage", c.CatImage)));
internalData = FillSubCatagories(dataResponse).ToString();
Thats the first list of categories now i want to recursively pull all sub categories and nest them in my Xelements FillSubCatagories() method:
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) {
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;
}
Alright so the problem comes when I run through the foreach the second time through the method. On int parentID = Int32.Parse(cat.Attribute("CatID").Value);
returns a nullreferenceException - "Object reference not set to an instance of an object"
Still getting used to c# coming from java, so be gentle. I'm sure its a glaring error but I haven't seen a clean reason why.
<<<<<<<<<>>>>>>>>>>>>>>
EDITED
new FillSubCategories() looks like this
private XElement FillSubCatagories(XElement cat) {
IEnumerable<XElement> list = cat.Descendants();
int catTypeID = cat.Attribute("CatTypeID") == null ? 1 : Int32.Parse(cat.Attribute("CatTypeID").Value);
foreach (XElement c in list) {
int parentID = Int32.Parse(c.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);
if (sub.Descendants() != null) {
FillSubCatagories(sub);
}
}
return cat;
}
this got me a lot further but I still end up hitting null.
EDIT WORKING METHOD
private void FillSubCategories(XElement cat) {
IEnumerable<XElement> list = cat.Descendants();
foreach (XElement c in list) {
try {
int catTypeID = Int32.Parse(c.Attribute("CatTypeID").Value);
int parentID = Int32.Parse(c.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)));
try {
string i = sub.Element("Category").Value;
c.Add(sub);
FillSubCategories(sub);
} catch (Exception) {
continue;
}
} catch (Exception) {
continue;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那看起来不太好。您确定有些东西不属于您表格中的任何类别吗?如果是这样,您不应该选择它们,因为您正在对 CategoryId 执行所有操作。我想正如克里斯提到的,您之所以会遇到此异常,是因为您所在的父类别没有其他父类别。您需要添加一个 if 条件,并且不解析它是否是父类别。像这样的事情:
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:
“cat”指定的节点上没有属性“CatId”。也许你已经达到了最高层?
There is no attribute "CatId" on the node specified by "cat". Maybe you've hit the top of the hierarchy?
问题是
cat.Attribute("CatID")
为 null。您在 null 上执行cat.Attribute("CatID").Value
会出现该错误。您可以放入一个检查,说明如果它不为空,则继续。或者还有其他问题吗?
The problem is that
cat.Attribute("CatID")
is null. You are doing acat.Attribute("CatID").Value
on a null gives you that error.You could put in a check saying that if it is not null then proceed. Or was there another question?