Java Script runs fine in Eclipse but produces Null Pointer Exception in Fiji Script Editor

I have been developing a code that will run through a directory of images containing a specific string (DAPI, GFP). Two images with the same sub-strings will be matched to each other using these two differing sub-strings. The other sub-strings are the images name (usually what drugs were run on it) and its numerical identifier.

So far I have written a code that works perfectly in Eclipse and prints out all of the image names in the proper pairing order.

So basically the code pairs like this:

x_DAPI_0001
x_GFP_0001
x_DAPI_0002
x_GFP_0002

y_DAPI_0001
y_GFP_0001
y_DAPI_0002
y_GFP_0002

http://codebins.com/bin/4ldqp00/latest

Whenever I try to run this in FIJI it requires me to save it first before running it and then I get the following error.

Started JFileChoose_Tes.txt at Tue Dec 01 12:38:17 EST 2015
java.io.FileNotFoundException: C:\Users\Stewart Lab\Desktop\Research\ImageJ Macros\JFileChoose_Tes.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:106)
at java.io.FileReader.(FileReader.java:55)
at net.imagej.ui.swing.script.TextEditor$22.execute(TextEditor.java:2006)
at net.imagej.ui.swing.script.TextEditor$Executer$1.run(TextEditor.java:1734)

Now I am incredibly new to development with Java let alone Maven & FIJI so I have been pouring through tutorials on it. I am just hopelessly lost on what is needed by FIJI to properly run a plugin coded in JAVA.

Everything under the “doSomething()” method will eventually just be the actions I would normally do to my images, so basically it will be a macro nested inside of the plugin. I am well able to do that, and now I have coded the other side which allows me to choose a directory and pair images, it’s just putting the two together that is holding me up now.

So the first thing that’s going on is that Java scripting in Fiji does require a physical source file to compile - but it’s easy to save a file without the proper extension, and there may be a bug where characters of the name are lost.

The first thing to try would be to manually save your code in a .java file, and then File > Open it from the script editor. This should allow your script to run - but if you make changes to the source they won’t actually take effect until Fiji is restarted.

So the whole concept of “rapid prototyping” is extremely misleading if you use Java.

If you want rapid scripting I would recommend an actual scripting language - e.g. python, or ImageJ’s macro language.

If you want to use the full Java API then I recommend skipping over to the getting started with Maven section. There’s a minimal-ij1-plugin project designed to act as a starting point (and certainly one of the easiest ways to learn is to look at and modify existing code). Your workflow in this case is

  1. make changes to the Java code
  2. build the jar with Maven
  3. copy the jar into your Fiji installation
  4. (re)start Fiji
  5. run your plugin

One thing to consider is if you truly need to write a Java plugin for this functionality…? If you have working logic written in a script and just want a way of linking in file-selection logic, your script can be enhanced with File parameters… and there are examples that in use in the tutorial templates. The ImageJ macro language also has dedicated methods for this purpose.

In any case, just to reiterate - using Java in the script editor is plagued with problems right now and is not recommended. Whether you continue with a script or a full Java plugin (developed in an IDE like Eclipse) let us know if you have any more questions or issues.

2 Likes

Thank you for the amazing reply, the minimal plugin I had already seen and was saving that in case this turns out to be a more tedious option (which it seems to be).

I had no idea that developing in Java for ImageJ had so many issues and I was definitely under the impression that I could just churn out prototypes. I picked Java because I am used to the syntax but if python is that much easier to prototype with I might as well just go with that.

Really the big thing I wanted from Java was the filechooser program that I found on the web (some modifications I made of course), but I have a feeling it’s wholly possible inside of FIJI.

My main question is if I could write a script that would write the data I receive, from a measurement on an image in Fiji, into an excel sheet in a specific cell? I know this is possible with Java in fact I know how to do it that way but I have no clue if Fiji has such a function or how to do that through Python (though I’m sure I could go look it up quickly).

By the way you should mostly ignore the comments in the posted code as I did figure out everything I was talking about with the getFile() & doSomething() methods and it does print everything perfectly.

If you like Java syntax, but wish it was just a little more concise in a couple of ways, might I suggest Groovy? It is quite awesome.

1 Like

One thing to consider is if you truly need to write a Java plugin for this functionality…? If you have working logic written in a script and just want a way of linking in file-selection logic, your script can be enhanced with File parameters… and there are examples that in use in the tutorial templates. The ImageJ macro language also has dedicated methods for this purpose1.

I’ve been thinking about this a lot and whether it is necessary to even use Java.

I saw that IJM does have a “getDirectory()” but at the end of the day it seems its ability to manipulate variables is limited which is why I think Java may be the best course.

However this is what I am looking for my program to do at the end of the day:

  1. Ask for directory
  2. Search for image pair based off of unique strings (1 pair at a time)
  3. Run macro on that pair
  4. Take results and paste them in a specified location into an excel sheet (return to step 2, repeat until all images have been looped through)

Is this best done through Java, Python, or Fiji itself? It doesn’t seem possible to do this only in Fiji. I know enough of Java and Python to do either or. I’m just unsure of what the Pro/Cons to them are. Like I said my only pro with Java is I know the syntax/libraries better.

I’m hesitant to do Groovy since I want to get this project done relatively soon and I don’t really have the time to learn something entirely new.