Calling method of another class from JME


I have two classes

class1 for Java Frame
class2 for JME canvas

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.

How to solve this problem.

Any guide pls

Could we see a small example program that exibits the problem you are describing

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 

Because you never set the variable. You have to pass an instance of your class1 to your class2 when you create it.

My guess is that my advice is probably confusing because you are still very new to Java. I recommend some basic Java tutorials before diving into JME.

Here you will get a lot of complicated answers to a really simple Java question that doesn’t really have anything to do with jMonkeyEngine itself.

1 Like

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.