Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

can anybody help me to convert this objective-c code to android java.

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (_mouseJoint != NULL) return;

    UITouch *myTouch = [touches anyObject];

    CGPoint location = [myTouch locationInView:[myTouch view]];

    location = [[CCDirector sharedDirector] convertToGL:location];

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

    if (_paddleFixture->TestPoint(locationWorld)) {

        b2MouseJointDef md;
        md.bodyA = _groundBody;
        md.bodyB = _paddleBody; = locationWorld;
        md.collideConnected = true;
        md.maxForce = 1000.0f * _paddleBody->GetMass();

        _mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);


I tried below is my code but getting exception. java.lang.nullpointer . Is anything wrong ?

public boolean ccTouchesBegan(MotionEvent event) {

         if (_mouseJoint != null){ 
             return true;


         CGPoint convertedLocation = CCDirector.sharedDirector().convertToGL(CGPoint.make(event.getX(), event.getY()));
         Vector2 locationWorld = new Vector2(convertedLocation.x/PTM_RATIO, convertedLocation.y/PTM_RATIO);

         // return CCTouchDispatcher.kEventHandled;


             MouseJointDef md = new MouseJointDef();
             md.bodyA = _body;
             md.bodyB = _paddleBody;
             md.collideConnected = true;
             md.maxForce = (float)(1000.0f * _paddleBody.getMass());
             _mouseJoint =  (MouseJoint) _world.createJoint(md);
     //return CCTouchDispatcher.kEventHandled;

        return true;
share|improve this question
up vote 0 down vote accepted

The key difference between obj-c and pretty much any other language is that you don't have to check for null (or nil in obj-c). Basically you need to see what line your exception is occurring on, and wrap that with a null check.

share|improve this answer
thanks for your reply i did that but it is still giving exception. – jubin Aug 5 '11 at 8:18
@Tetrad That's a really interesting comment, I've really done the bulk of my coding in Obj-C so I'm curious whether you're only referring to properties and message passing, or if there's some deeper fundamental difference I'm not aware of? – jakev Aug 6 '11 at 5:13
@NS6String, I'm mainly referring to the fact that you can send a message to nil and it'll just fail silently instead of either crashing or throwing an exception. So a straight code conversion would have to account for that behavior. – Tetrad Aug 6 '11 at 5:48
actually when i convert this code iphone to java i got the exception GLthread 8 java.lang.NullPointerException. This exception i got. – jubin Aug 9 '11 at 1:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.