2012-04-16 31 views
0

當我嘗試使用Visual Studio和Enterprise Architect編譯我的程序時出現此錯誤。錯誤:未將對象引用設置爲對象的實例企業架構師和視覺工作室

我正在爲Enterprise Architect編寫一個工具,而且我必須製作一個圖表,並且我仍然會出現此錯誤,我不知道該怎麼辦。

,我有問題的代碼是:

public Graph(EA.Repository repository) 
    { 

     EA.Diagram maindiagram; 
     this.modelRepository = repository; 
     maindiagram = repository.GetCurrentDiagram(); //recupero del diagramma 
     this.diagramId = maindiagram.DiagramID; //identificativo del diagramma 

     //inizializzazione nodi 
     Collection nodeCollection = maindiagram.DiagramObjects; 
     nodeList = new ArrayList(); 


     foreach (DiagramObject diagram in maindiagram.DiagramObjects) 
     { 
      diagramList.Add(diagram); 
      foreach (Element element in diagramList) 
      { 
       if (element.Type == "Class"|| element.Type == "Component"||element.Type == "Package") 
       { nodeList.Add(new Node(diagram, ref repository)); } 

      }     

     } 

     //inizializzazione archi 
     Collection linkCollection = maindiagram.DiagramLinks; 
     linkList = new ArrayList(); 

     foreach (DiagramLink edge in maindiagram.DiagramLinks) 
     { 
      edgeList.Add(edge); 
      foreach(Connector connector in edgeList) 
       if (connector.Type == "Association" || connector.Type == "Aggregation" || connector.Type == "Compose" || connector.Type == "Dependency" 
        || connector.Type == "Generalization" || connector.Type == "Realization") 
       { linkList.Add(new Link (edge, ref repository));} 
     } 

請幫助,如果你知道怎麼辦。

非常感謝!

+0

哪條線出現錯誤? – 2012-04-16 11:53:17

+0

問題在於maindiagram的分配,並說它導致null。我不明白它如何可以爲空。另一個是我爲同樣原因假設的第一個foreach。我必須解決maindiagram問題,但我不知道如何,我想GetCurrentDiagram()可以幫助您獲得您在企業架構師中開放的關係圖。 – Defi 2012-04-16 18:26:44

回答

0

repository可能是null - 你應該做的參數參數null檢查,以確定是否可以繼續或取消;也maindiagram可能是null(我們都知道,如果repository是什麼東西,然後GetCurrentDiagram可能返回null

這些事情都在,可能會導致您的問題的方式訪問。

diagramList不存在在該方法的範圍內,假設它有一個更寬鬆的範圍:這也可以是什麼都沒有,但你可以調用Add,你也可以像遍歷它一樣,並且不檢查element是否爲null,嘗試訪問屬性。

總之,在您發佈的代碼中可能會出現這種情況。您應該更具體地說明錯誤實際發生的位置,但答案將是相同的:something is nothing

+0

我知道一些東西是空的,我如何解決一個,我得到其他錯誤,總是相同的。我得到關於它接縫的「maindiagram」的錯誤,但我不明白爲什麼,以及「maindiagram.DiagramObject」中的foreach。你知道爲什麼我的currentdiagram()不起作用,不給這個主圖的價值嗎? – Defi 2012-04-16 12:46:52

相關問題