ERROR: System.NullReferenceException: Object reference not set to an instance of an object(MepSystemSearch)

ERROR: System.NullReferenceException: Object reference not set to an instance of an object(MepSystemSearch)

reylorente1
Collaborator Collaborator
2,505 Views
3 Replies
Message 1 of 4

ERROR: System.NullReferenceException: Object reference not set to an instance of an object(MepSystemSearch)

reylorente1
Collaborator
Collaborator

Estoy aplicando un ejemplo con MepSystemSearch de BuildingCoder,pero al 5 item me da System.NullReferenceException: Object reference not set to an instance of an object,por que?.

I am applying an example with MepSystemSearch from BuildingCoder, but at 52022-02-02 (1).png2022-02-02 (2).png item it gives me System.NullReferenceException: Object reference not set to an instance of an object, why?

 

0 Likes
Accepted solutions (2)
2,506 Views
3 Replies
Replies (3)
Message 2 of 4

RPTHOMAS108
Mentor
Mentor
Accepted solution

Not familiar with the sample but following it through logically I'd say by looking at GetNextConnectedElement::

 

 

 

public Element GetNextConnectedElement( 
      Connector pConn, 
      Element pPrevElem,
      ref List<ElementId> lVisited )
    {
      foreach( Connector pRef in pConn.AllRefs )
      {
        if( NextConnector( pRef, pPrevElem, lVisited ) )
        {
          continue;
        }
        return pRef.Owner; //Owner is null here
      }
      return null; //or you are reaching this point (NextConnector returns true for all connectors).
    }

 

 

 

So I would be investigating the reason for that and the implications of a null check in terms of avoiding adding null items to the list.

0 Likes
Message 3 of 4

jeremy_tammik
Alumni
Alumni
Accepted solution

When ever you a faced with a null pointer reference, I would first of all recommend debugging and stepping through your code until you hit the problematic place. In most cases, the cause will become immediately evident and you can easily move at least one step closer to fixing it. This is standard programming practice, cf. a search for 'fix null pointer reference .net':

 

https://duckduckgo.com/?q=fix+null+pointer+reference+.net

 

Interesting fact:

  

https://en.wikipedia.org/wiki/Null_pointer#History

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 4 of 4

elonjigar
Community Visitor
Community Visitor

An Object is an instance of a Class , it is stored some where in memory. A reference is what is used to describe the pointer to the memory location where the Object resides. The message "object reference not set to an instance of an object" means that you are referring to an object the does not exist or was deleted or cleaned up. It's usually better to avoid a NullReferenceException than to handle it after it occurs. To prevent the error, objects that could be null should be tested for null before being used.

if (mClass != null)
{
// Go ahead and use mClass
mClass.property = ...
}
else
{
// Attempting to use mClass here will result in NullReferenceException
}

A NullReferenceException typically reflects developer error and is thrown in the following scenarios:

  • Forgotten to instantiate a reference type.
  • Forgotten to dimension an array before initializing it.
  • Is thrown by a method that is passed null.
  • Get a null return value from a method, then call a method on the returned type.
  • Using an expression to retrieve a value and, although checking whether the value is null.
  • Enumerating the elements of an array that contains reference types, and attempt to process one of the elements.

 

0 Likes