In class1, I am be able create object of class2 which starts showing " JME drawing cubes". However I want to call back method of class1 from JME running object after 2 minute. it shows java.lang.Nullpointer exception
However i could able access all data of class1 within class2.
The BaseAppStates can provide you with a callback from jMonkeyEngine thread, but you are doing swing stuff, swing runs on its awt thread and so calling it from jMonkeyEngine thread might have unexpected behaviours…
To provide a callback on the swing thread use this for async callbacks:
SwingUtilities.invokeLater(() -> {
// Do async swing or awt stuff on the awt event dispatching thread
});
Use this for sync callbacks:
SwingUtilities.invokeAndWait(() -> {
// Do sync stuff
});
Beginner tip: Set all possible fields (=class- and instance variables) final and don’t let anything uninitialized, which is also for final fields not allowed. The classes will become a lot more stable if you can tell that anything has an allowed value. Also, when you write to a field from an argument in a method (constructors are also methods), always test them if the have a valid argument, unlike it can really have any possible value like a boolean. When you write an invalid argument to a field and a later method call throws an exception (or more worse: doesn’t throw an exception but do anything wrong), it is not really possible to track down the source of this invalid argument.
Also, before you post, simplify your code to the most simple example. Often, you find the mistake by yourself and if not, post the example and the Exception inclusive StackTrace, if any.