WHY do I ran into NULL = Crash? ---> via <dynamic_cast>

WHY do I ran into NULL = Crash? ---> via <dynamic_cast>
0.0 0

#1

void mainWorld::LevelUp_checkWorlds(){

   //📣 Load Worlds
CCWorld *_mainWorld = CCSoomlaLevelUp::getInstance()->getWorld(WORLD_MAIN_ID);

//📣 Cast Levels
int _numWorlds = CCSoomlaLevelUp::getInstance()->getWorldCount(false);
/* =================================== */
/* =================================== */

//📣 Cast all Worlds in this MainWorld
for (int i=0; i < _numWorlds; i++ ) {
    
    soomla::CCWorld *world = dynamic_cast<soomla::CCWorld*>(_mainWorld->getInnerWorldAt(i));
    
    if(world->canStart()) {
        CCLOG(FILE " ");
        CCLOG(FILE "OFFEN World: %s // World Nr:%d", world->getId()->getCString(),i );
        
        // If so, draw the menu without a lock icon on the level
    }
    else {
        CCLOG(FILE "GESCHLOSSEN %s ", world->getId()->getCString() );
        // Draw the menu with a lock icon on the level
    }
    
}

/* =================================== */
/* =================================== */


CCLOG(FILE " TEST");

}


#2

If dynamic_cast is returning a null it means that the object you are trying to cast is not of the type specified in the dynamic_cast. This is basic c++, Please debug your code and check what type of objects are returned by the _mainWorld->getInnerWorldAt(i) function.


#3

Hi @vkrishna ,

so at first the FOR-LOOP is running perfectly. All my worlds are shown in the DEBUG-LOG with the right info.

After the last item, (I think - if the loop is running (if int i == _numWorlds)) the crash occurs… normaly the loop should end with its work…