Book a Demo

Author Topic: Error: Object reference not set to an instance of  (Read 7670 times)

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Error: Object reference not set to an instance of
« on: April 16, 2012, 07:50:56 pm »
Hi!

i get this error when i try to compile my program using Visual Studio and Enterprise Architect.

I'm writing a tool for Enterprise Architect, and i have to make a graph, and i continue to get this error, i don't know what to do.

The code that i have problem with is:

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));}
            }
Please help if you know how.

Thank you a lot!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Error: Object reference not set to an instance
« Reply #1 on: April 16, 2012, 10:15:52 pm »
You could start by telling us which object is NULL

Geert

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Error: Object reference not set to an instance
« Reply #2 on: April 16, 2012, 10:22:00 pm »
uhh sorry,
well it shows me for:
  • this.diagramId = maindiagram.DiagramID;
  •   diagramList.Add(diagram);

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Error: Object reference not set to an instance
« Reply #3 on: April 16, 2012, 10:24:53 pm »
omg

Ok, hover your mouse over the...

pff, nevermind..

Geert

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Error: Object reference not set to an instance
« Reply #4 on: April 16, 2012, 10:32:24 pm »
why this kind of answer?
sorry if is a stupid question for you, but i'm trying to learn something, if you don't want to help, don't bother to answer, thank you a lot for your time.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Error: Object reference not set to an instance
« Reply #5 on: April 17, 2012, 01:06:42 am »
I'm no Cxx-hacker but what I see from your program is that you should re-start with some very basics.

q.

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +55/-3
    • View Profile
Re: Error: Object reference not set to an instance
« Reply #6 on: April 17, 2012, 09:02:01 am »
You need to check the return value from GetCurrentDiagram() before using it.
The Sparx Team
[email protected]

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: Error: Object reference not set to an instance
« Reply #7 on: April 17, 2012, 09:21:29 am »
The diagramID will be null if there is no diagram open when your add-in runs.

Open a diagram, then run your add-in.

... also as KP suggested, test for null before using the returned value.


maindiagram = repository.GetCurrentDiagram(); //recupero del diagramma
if (mainDiagram != null) {
     this.diagramId = maindiagram.DiagramID; //identificativo del diagramma
}

« Last Edit: April 17, 2012, 09:22:58 am by philchudley »
Models are great!
Correct models are even greater!

defi

  • EA User
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Re: Error: Object reference not set to an instance
« Reply #8 on: April 17, 2012, 09:22:28 pm »
Ok thank you, i wrote and if condition before every value that accure that error and now i don't get any error, but eather an result. I'll keep working on it.

Thank you a lot for your help.

 :)