views:

134

answers:

1

I have the following code wrapped by swig:

int
cluster::myController(controller*& _controller) {
   _controller = my_controller;
   return 0;
}

controller has a private constructor.

What's the correct incantation to make something like this not throw an exception?

 public static void main(String argv[]) {
    controller c = null;
    int r = dan_cluster.theCluster().myController(c);
    System.out.println(r);
    System.out.println(c.getUUID());
  }
A: 

It would be helpful if you were to post the exception you're getting, but I don't believe that what you're trying to do is possible. In C++ you can pass a pointer by reference and so make changes to a pointer variable passed to a function, but in Java there is nothing equivalent. You should check the generated wrapper, but my guess is that your C++ code is being wrapped as something like:

int myController(controller _controller) {
   _controller = my_controller;
   return 0;
}

...which clearly won't do what you want, leading to a NullPointerException when you try to getUUID().

Assuming I'm right, the best fix is for myController() to just return a controller*. If you really need the integer, consider returning a std::pair (note: wrapping any part of the stl requires some delicacy, consult the documentation).

David Seiler
Sorry, of *course* it's a null-pointer deref. I should've been more clear.I need the int because in some cases (not this method mind you) we need to pass back a failure code. std::pair it is!
Chip Christian