ROSJAVA preconditions exceptions in updateLoop() method outside of main
I'm modifying the ROSjava publisher example to run within some other code I've written. The other code I've written has its own updateLoop method that is called by main and runs continuously until the program is exited. I am doing calculations within this updateLoop and would like to publish the results.
I can run the example publishing code just fine if it is in main before I make the call to the updateLoop() method, however if the publishing code is inside the updateLoop(), the program crashes when it attempts the first loop. The crash occurs when checking the preconditions. Here's the exact output:
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main] java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:187) at ROS.Talker2.simpleUpdate(Unknown Source)
I modified the code to make the NodeConditions and some other stuff global so I can pass it to the updateLoop without doing to explicitly. The code below (minus the extraneous code of my program) is what produced the error above.
public class Talker2 implements NodeMain {
private Node node;
private NodeConfiguration globalConfiguration;
private int seq=0;
org.ros.message.std_msgs.String str = new org.ros.message.std_msgs.String();
Publisher<org.ros.message.std_msgs.String> publisher;
@Override
public void main(NodeConfiguration configuration) {
globalConfiguration = configuration;
Talker 2 app = new Talker2();
app.updateLoop();
}// end Main
// ... other methods in here ...
public void updateLoop()
{
/* my calcs are done before the publish here */
Preconditions.checkState(node == null);
Preconditions.checkNotNull(globalConfiguration);
try {
node = new DefaultNodeFactory().newNode("talker", globalConfiguration);
publisher = node.newPublisher("chatter", "std_msgs/String");
int seq = 0;
{
org.ros.message.std_msgs.String str = new org.ros.message.std_msgs.String();
str.data = "INSIDE LOOP "+ seq++;
System.out.println("\n\n INSIDE LOOP FLAGEN!!!"+ seq++);
publisher.publish(str);
//Thread.sleep(1000);
}
} catch (Exception e) {
if (node != null) {
node.getLog().fatal(e);
} else {
e.printStackTrace();
}
}
}//end updateLoop();
}/ end Talker2 class
How can I publish the data from within my updateLoop()? Also, I plan to have several publishers in this loop in the future. Is there anything I should look out for with that?