ImageJ dialog widgets

Hi

I tried to do a plugin using prom-fiji version 16.0.0

Now when I used a dialog element such as

@Parameter(style="directory")
private File dir;

I get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at net.imagej.legacy.IJ1Helper.getDirectory(IJ1Helper.java:1058)
    at net.imagej.legacy.ui.LegacyUI$2.run(LegacyUI.java:291)
    at org.scijava.thread.DefaultThreadService.invoke(DefaultThreadService.java:107)
    at net.imagej.legacy.ui.LegacyUI.chooseFile(LegacyUI.java:285)
    at org.scijava.ui.DefaultUIService.chooseFile(DefaultUIService.java:329)
    at org.scijava.ui.swing.widget.SwingFileWidget.actionPerformed(SwingFileWidget.java:124)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:154)
    at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

I tried to use "style=“directory” in the imageJ-tutorials (pom-imagej, version 13.1.0, which is the same as in the pom-fiji)

Felix

1 Like

Hmm, I cannot reproduce this exception on my system.

With the widget-demo tutorial, which currently extends pom-imagej 13.1.0, I changed the file parameter to use style = "directory" and it worked as expected.

Your stack trace points to this line of code, which clearly cannot throw a NullPointerException. Can you please verify which version of imagej-legacy is being used at runtime in your code? According to pom-imagej 13.1.0, it should be 0.17.3, but somehow it is getting overridden to something else for you.

By the way: it is best practice to use the string constants when writing Java code—i.e., style = FileWidget.DIRECTORY_STYLE in this case.

1 Like

Checking the dependencies shows me 0.17.3 for the imagej-legacy.

I just cloned the latest version from


and added the tow lines from my first post. I replaced “directory” with FileWidget.DIRECTORY_STYLE
with the same results.

The only difference that comes to mind is maybe the Java version. I used version 1.7. Switching to 1.6 did the trick. Then I tried 1.8, and lucky for me this worked too (since memory management has become a bit easier and it runs a lot faster)

I know officially imageJ still runs on 1.6. But what about using scifio, opts, etc. (the new stuff) which version is recommended for developing ImageJ2 plugins?

Note that the SciJava software stack will start requiring Java 8 within the next few months.

In the meantime, you have a choice: develop against Java 6 to maintain maximum compatibility with your user base, or develop against Java 8 to have access to the latest and greatest features of Java.

1 Like

@ctrueden

I just stumbled accross the problem again. It seems, that it throws the error only if the input field is completly empty. Since IJ will recall your last input, there will alwas be something there once the plugin has run at least once (successfully), which, I guess, is the case for you.I would be curios if you can reproduce the error under these exact conditions.

To avoid erros when hitting “Browse” on an empy field, I simply to not leave it empy in the first place and initialize the directory to something like

@Parameter(style = FileWidget.DIRECTORY_STYLE)
File inputDir = new File("/");

…or just copy paste any valid path in the dialog instead of hitting the “Browse button”.

I hit the very same bug three weeks ago, and thought I had fixed it:

It should be in imagej-legacy-0.17.4.jar.

Could you check if the macro below works for you?

// @File(label = "Choose a folder", style = "directory") myDir

print(myDir);

You can also change myDir to myReallyLongNameINeverUsedBefore just to be sure previous values aren’t being persisted.

And you can write:

// @File(label = "Choose a folder", style = "directory", persist = false) myDir

To avoid the persistence mechanism being invoked in the first place.

Yes, it does. Also did set the persist=false, which works too.

So you, @ctrueden and @imagejan, were not able to reproduce the errror with a fresh plugin?

I just wrote a fresh plugin in the Script Editor with the following code:

import java.io.File;

import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;

@Plugin(type = Command.class)
public class MyCommand implements Command {

	@Parameter
	private UIService ui;

	@Parameter(persist = false, style = "directory")
	private File file;

	@Override
	public void run() {
		ui.showDialog("You chose: " + file);
	}

}

Saved and ran, and it worked just fine. Cannot reproduce. I think @imagejan is right that the bug has already been fixed.

So I narrowed it down a bit further:
I usually add a main method like the one bellow, to the commands for easy debugging.

public static void main(final String... args) throws Exception {
    final ImageJ ij = net.imagej.Main.launch(args);
    ij.command().run(DialogTest.class, true);
}

Once I compile it, I don’t have the problem anymore. So what would be the proper way to run the plugin from the IDE?

Your main method is fine for debugging from an IDE.

Adding that main method to my example above, and attempting to run from Eclipse, all works fine in my sandbox project which extends the most recent pom-fiji parent.

If you still think there is a bug in the latest version(s) of relevant components, create an MCVE and post it.