2011-01-10 16 views
2

XElementsをカテゴリリストにステッピングして再帰的に追加しようとしています。カテゴリーの最初のリストザッツC#ヌル参照を返す再帰メソッド。オブジェクト参照がオブジェクトのインスタンスに設定されていません。 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; 
    } 

よしので、問題が来ますメソッドを2回目にforeach。 int parentID = Int32.Parse(cat.Attribute("CatID").Value);でnullreferenceExceptionを返す - "オブジェクト参照がオブジェクトのインスタンスに設定されていません"

まだJavaから来るC#に慣れているので、やさしくしてください。私はその眩しいエラーは確かだが、私はなぜきれいな理由を見ていない。

< < < < < < < < < >>>>>>>>>>>>>> )( 新しい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; 
    } 

のように見えますしかし、私はまだヌルを打つことに終わります。

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; 
      } 
     } 
    } 
+0

可能な限り、実際のデータは少なくとも3つのレベルのカテゴリに進むことができます。私が気づいたのは、私が最初にXElementがその下の属性を持つカテゴリ要素であることを初めて知ったのですが、2回目にはcategory要素とともにサブ要素を追加しました。 。だから私はちょうどsubbedカテゴリの要素を渡すか、catから最初に実行されたプル属性のいくつかの種類を設定して、2番目とすべての後続の実行は子孫属性を引き出すと推測しています。しかし、私はそれが速く混乱すると思う。 – Fozz

+0

http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net –

答えて

0

これはよく見えません。あなたのテーブルには何も入っていないものがあると確信していますか?そうであれば、categoryIdのすべての操作を行っているので、それらを選択するべきではありません。私はChrisが言及したように、別の親を持たない親カテゴリにいるのでこの例外が発生していると思います。 if条件を設定し、親カテゴリの場合は解析しないでください。このようなもの:

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; 
    } 
+0

if文が再帰を中断していないのですか?少なくとも私はJavaで何が起きているのかかなり確信しています(私はそれをテストしなければならないでしょう - そして私はC#がJavaのものではないことを知っています。 – Fozz

0

"猫" で指定したノードには属性 "CATID" はありません。たぶんあなたは階層の一番上を打ったでしょうか?

0

問題は、cat.Attribute("CatID")がnullです。 nullでcat.Attribute("CatID").Valueを実行していると、そのエラーが発生します。

ヌルでない場合は、次のようにチェックすることができます。それとも別の質問がありましたか?

関連する問題