Archive
NetBeans Platform – Duplicate pair in treePair error
I have previously written about the NetBeans Platform
I’ve been using a lot recently at work and so have found a lot of pain points when using the APIs. I’ll try to document some of the more vexing problems here for other programmers who might be facing the same problem.
One problem you will probably face while using the NetBeans Platform is an inscrutable error message when you start up your application, something to the effect of:
java.lang.IllegalStateException: Duplicate pair in treePair1: java.lang.Object pair2: java.lang.Object index1: 55 index2: 55 item1: null item2: null id1: 16309239 id2: 4ecfe790 at org.openide.util.lookup.ALPairComparator.compare(ALPairComparator.java:83) at org.openide.util.lookup.ALPairComparator.compare(ALPairComparator.java:54) at java.util.TreeMap.put(TreeMap.java:530) at java.util.TreeSet.add(TreeSet.java:238) at org.openide.util.lookup.AbstractLookup.getPairsAsLHS(AbstractLookup.java:322) at org.openide.util.lookup.MetaInfServicesLookup.beforeLookup(Met…
While it’s impossible to know from this error message, this really indicates that you had some sort of exception in the initializer of one or more of your TopComponents, leading to duplicate null entries in some internal set that NetBeans Platform maintains, leading to this error message. Fortunately, there is usually a “Previous” button on the exception window so you can see what caused the real problem.
NetBeans – Filter output
NetBeans allows you to filter the logging output of an application, if you stumble upon the feature. I normally use the Find command in the Output window to search for occurrences of a string with the Ctrl + F (Command + F on Mac) shortcut, and then go to the next matching result with the Command + G shortcut when I’m searching for something in particular (e.g. a line logging the state of a variable in the program). I found that if you press Ctrl + G instead, you can filter the output to show only lines that match a given string or regular expression. This is an extremely useful feature, and one I only found by accident. I hope this helps any NetBeans developers out there.
NetBeans Platform – how to register a class as a provider of multiple services
The issue I ran into is that the documentation is not very clear how you can register your class as providing more than one service (again, implementing more than one interface). For instance, say I have two interfaces I want a class to fulfill:
public interface Foo { public String getFoo(); } public interface Bar { public String getBar(); } public class FooBar implements Foo, Bar { public String getFoo() { return "foo"; } public String getBar() { return "bar"; } }
// This is a mistake; you can't declare two annotations like this @ServiceProvider(service=Foo.class) @ServiceProvider(service=Bar.class) public class FooBar implements Foo, Bar {
@ServiceProviders(value={ @ServiceProvider(service=Foo.class), @ServiceProvider(service=Bar.class)} )
NetBeans – Reformat text
(Sourcecode taken from this stackoverflow post)
The other time it’s useful is when working on a project where someone is using very large tab settings (e.g. 8 spaces per tab instead of 4). This can make it very difficult to read. By invoking this command, you’ll put the code into a much more readable format.
Reset windowing system in NetBeans Platform
Clean and build
Remove Windows2Local
rm -rf /path/to/Foo/build/testuserdir/config/Windows2Local
NetBeans Platform – ModuleInstall ClassNotFoundException
In the NetBeans Platform, you can create an ModuleInstall class which handles the lifecycle of your module, and provides methods you can override to handle when your module is loaded and unloaded. The standard way of creating this class is to use the wizard that NetBeans provides for this purpose, accessible by right clicking on the project and choosing New -> Other -> Module Development -> Module Installer
If you decide that you do not want to use the ModuleInstall mechanism any longer (the NetBeans Platform folks suggest you do NOT use it, as it will increase the startup time of the application), you might think you can just delete the Installer.java file that the wizard created. Unfortunately, that’s not the case. When you run the project you’ll get an exception like the following
org.netbeans.InvalidException: StandardModule:net.developmentality.moduleexample jarFile: ... java.lang.ClassNotFoundException: net.developmentality.moduleexample.Installer starting from ModuleCL@482...
The problem is that the wizard modified the manifest.mf file as well, and you need to manually clean up the file before your project will work again.
Open the manifest.mf file and you’ll see a line in the file like the following:
OpenIDE-Module-Install: net/developmentality/moduleexample/Installer.class
delete that line, and rebuild. You should be ready to go.
In general, you need to be very careful about using any of the wizards that NetBeans provides. They are extremely useful, but they end up changing XML files that you will need to manually edit later if you decide to refactor. For instance, if you create an Action instance using the New Action Wizard, the action will be registered in the layer.xml file. If you rename the action using the refactor command, the entries in the xml file are NOT modified, and you will get an exception at runtime unless you remember to edit that file.
Fortunately, the wizards are smart enough to at least tell you which files they are modifying. It’s just a matter of remembering this further down the road when you need to refactor, move, or delete files. Look for the modified files section in the wizard dialog:
Two shortcuts for NetBeans navigation
Jump to any file in any open project
⌃⇧ O
Search for string
Increase heap size for NetBeans Platform project
To increase the heap size in a NetBeans Platform project, you can edit the nbproject/platform.properties file and add the line
run.args.extra=-J-Xmx1G
, replacing the 1G piece with however much memory you’d like. (The other solutions I found did not work for me). Thanks to the NetBeans Forums for this solution.
If you’re curious about the -J piece, it’s necessary to ensure that the arguments are passed to the JVM rather than interpreted by NetBeans itself. This should work in general for whatever arguments you want to pass to the JVM upon launch of your NetBeans Platform project.
Customize code formatting in NetBeans
if (cond1) { } else { }
After unsuccessfully searching online, I eventually stumbled onto some settings that control this behavior. By default, NetBeans wants to format your if/else statements as follows:
if (cond1) { } else { // 'else' is on the same line as the closing brace }
If you want to have a new line after your closing brace, as I do, you need to change some settings.
Make sure the “else” checkbox is checked in the “new lines” section. Customize the rest of it to suit your coding style.
NetBeans Platform – how to customize the title bar and remove datestamp
Problem:
Right click on the project node, and choose Find. In the dialog box, search for {0}.
In the results, double click on the first Bundle.properties, in branding/modules…




Updated title bar
Frame f = WindowManager.getDefault().getMainWindow(); f.setTitle("My title");
These are modified instructions based off of a thread found here. See instructions on how to change the versioning number, if that’s what you desire, here.
EDIT:
If you are attempting to change the window title programmatically from an Installer instance, you’ll find that this doesn’t work. The problem is that you are changing the title too early in the lifecycle. Instead, you need to do the following:
// Only change the window title when all the UI components are fully loaded. WindowManager.getDefault().invokeWhenUIReady(new Runnable() { @Override public void run() { WindowManager.getDefault().getMainWindow().setTitle(windowTitle); } });