Nullpointer exception from DaemonService

Hi all,

I implemented DaemonService from an example project where it’s working. After I implement it in the project I am currently working on I am met with a nullpointer exception upon running the ursim. Can anybody help me?

Here is the error:
java.lang.NullPointerException at com.ur.urcap.examples.mydaemonswing.impl.nodes.installation.MyDaemonInstallationNodeContribution$2.run(MyDaemonInstallationNodeContribution.java:81) ~[?:?] at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_242]

This is the line that is throwing the error:

	private void start_python_program() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				daemonService.getDaemon().start();   <--- the line that is causing the nullpointer 
			}
		}).start();
	}

Here is my MyDaemonDaemonService class:

public class MyDaemonDaemonService implements DaemonService {

    private DaemonContribution daemonContribution;

    public MyDaemonDaemonService() {
    }

    @Override
    public void init(DaemonContribution daemonContribution) {
        this.daemonContribution = daemonContribution;
        try {
            daemonContribution.installResource(new URL("file:com/ur/urcap/CNC2HMI/_01_App/"));
        } catch (MalformedURLException e) {	}
    }

    @Override
    public URL getExecutable() {
        try {
            return new URL("file:com/ur/urcap/CNC2HMI/_01_App/main.py"); // Python executables
        } catch (MalformedURLException e) {
            return null;
        }
    }

    public DaemonContribution getDaemon() {
        return daemonContribution;
    }

}

I am not sure what is causing the nullpointer so any help is appreciated.

Best thing you can do to help track down nullpointers is to use System.out.println() and just start printing stuff to the terminal. My guess would be if you put (before the line that errors)

System.out.println(daemonService.getDaemon())

it’s going to print “null” to the terminal. If you still get a nullpointer, that means the .getDaemon() method is being inacted on a null object, so doing

System.out.println(daemonService)

would says null. Either the instance of daemonService never got initialized (by your activator I think) or the .getDaemon() method does not return an new instance of the daemon. Not totally sure how the init() function gets run. Maybe the service does that. Otherwise, looks like your “getDaemon()” method is just going to return null, since the variable daemonContribution was only ever declared, not initialized. (Again, unless something else runs the init() function that does the initializing.)

I maanged to track it down to the activator class where there a line was missing to register the daemonservice to the bundlecontext. Thanks for your input.