Box2d MouseJoint not created properly

I am new to this platform. I am right now working on cocos2dx with box2d and spriteHelper. I have created physics objects in spriteHelper and can able to simulate it in same. But When I try to do this through coding, mouse joint is not created and giving me error.

Here is my code:

 std::string Nest::initTest()  
{
    this->setTouchEnabled(true);

    CCSize s = CCDirector::sharedDirector()->getWinSize();

#if 1
    // Use batch node. Faster
    //when using batches - load a batch node using the generated image
    batchNodeParent = CCSpriteBatchNode::create("Letters_nest.png", 100);
    this->addChild(batchNodeParent, 1);
#endif

    CCSpriteFrameCache::sharedSpriteFrameCache()->removeUnusedSpriteFrames();

    //load into the sprite frame cache the plist generated by SH
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Letters_nest.plist");

    this->initPhysics();

    GHDebugDrawLayer* debugDraw = GHDebugDrawLayer::createDebugDrawLayerWithWorld(world);
    this->addChild(debugDraw, 1000);

   GHDirector::sharedDirector()->setPhysicalWorld(world);
GHDirector::sharedDirector()->setPointToMeterRatio(PTM_RATIO);

  CCSprite *bg = CCSprite ::create("bg.png");//createWithSpriteFrameName("bg.png");//
    bg->setAnchorPoint(ccp(0,1));
    bg->setPosition(VisibleRect::leftTop());
    this->addChild(bg, 0);



    GHSprite* nestImage = GHSprite::createWithSpriteFrameName("nest.png");
    GHSprite* egg1 = GHSprite::createWithSpriteFrameName("egg.png");
    GHSprite* egg2 = GHSprite::createWithSpriteFrameName("egg.png");

    nestImage->setPosition(ccp(s.width/2,s.height/2));
    egg1->setPosition(ccp(VisibleRect::center().x-50,VisibleRect::center().y+70));
    egg2->setPosition(ccp(VisibleRect::center().x+50,VisibleRect::center().y+70));

        if(batchNodeParent != NULL){//if we use batch nodes we must add the sprite to its batch parent
            batchNodeParent->addChild(nestImage);
            batchNodeParent->addChild(egg1);
            batchNodeParent->addChild(egg2);

        }
        else{//if we dont use batch nodes then we must add the sprite to a normal node - e.g the layer or another node
            this->addChild(nestImage);
            this->addChild(egg1);
            this->addChild(egg2);

        }
    this->scheduleUpdate();

    return "";
}

void Nest::beganTouchAtPosition(cocos2d::CCPoint p){
   // CCPoint location = CCDirector::sharedDirector()->convertToGL(p);
    CCPoint location = p;

    b2Vec2 v = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);

    for (b2Body *b = world->GetBodyList(); b; b = b->GetNext()) {

        b2Fixture *f = b->GetFixtureList(); // get the first fixture
        CCSprite *sprite =(CCSprite *) b->GetUserData();
        if(sprite != NULL)
        {
                CCLog("sprite not NUll %d",sprite->getTag());
          
               ** if(f -> TestPoint(v))   /// This line not executing** 
            {
                CCLog("You touched a body %d",sprite->getTag());

                b2MouseJointDef md;
                md.bodyA = groundBody;
                md.bodyB = b;
                md.target = v;
                md.collideConnected = true;
                md.maxForce = 1000.0f * b->GetMass();
                mouseJoint = (b2MouseJoint *)world->CreateJoint(&md);
                b->SetAwake(true);
            }
        }
    }

}
void Nest::moveObjectAtPosition(cocos2d::CCPoint p){

    if (!mouseJoint) return;

    CCPoint location = p;
        b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
      **  mouseJoint->SetTarget(locationWorld);  // Error line exc_Bad_acess**
   
}

void Nest::endTouchAtPosition(CCPoint p)
{

    if (mouseJoint ) {
        world->DestroyJoint(mouseJoint);
        mouseJoint = NULL;
        boolMouseJoint = false;
    }  
}

void Nest::initPhysics()
{
    CCSize s = CCDirector::sharedDirector()->getWinSize();

    b2Vec2 gravity;
    gravity.Set(0.0f, -10.0f);
    world = new b2World(gravity);

    // Do we want to let bodies sleep?
    world->SetAllowSleeping(true);

    world->SetContinuousPhysics(true);
     b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0, 0); // bottom-left corner
    groundBody = world->CreateBody(&groundBodyDef);

    b2EdgeShape groundBox;

    // bottom

    groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // left
    groundBox.Set(b2Vec2(0,s.height/PTM_RATIO), b2Vec2(0,0));
    groundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO), b2Vec2(s.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);
}

void Nest::update(float dt)
{   
    int32 velocityIterations = 8;
    int32 positionIterations = 1;

    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {
        if (b->GetUserData() != NULL) {
            //Synchronize the AtlasSprites position and rotation with the corresponding body
            CCSprite* myActor = (CCSprite*)b->GetUserData();
            myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
            myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );

     }   }  
}

I have also attached screenshot for the error message.

Please help.


2.png (341.9 KB)

Same problem here.
It seems that you can not access to the body properties in all the code…
I’m searching a solution too.
Someone can give me a clue?

Thanks.