Skip to main content

NPE using NimbusLookAndFeel

7 replies [Last post]
saija
Offline
Joined: 2006-04-17

Hi
I was trying various look and feels and testing the Nimbus l&f get mt an npe, here's the code i was using:

UIManager.setLookAndFeel(new NimbusLookAndFeel());

i'm using using NetBeans Build 201005060200(developer version) and the build 92 of the jdk 7, and now, here's the stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:796)
at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:281)
at sun.swing.SwingUtilities2.getFontMetrics(SwingUtilities2.java:1001)
at javax.swing.JComponent.getFontMetrics(JComponent.java:1624)
at sun.swing.MenuItemLayoutHelper.reset(MenuItemLayoutHelper.java:125)
at javax.swing.plaf.synth.SynthMenuItemLayoutHelper.(SynthMenuItemLayoutHelper.java:119)
at javax.swing.plaf.synth.SynthGraphicsUtils.paint(SynthGraphicsUtils.java:498)
at javax.swing.plaf.synth.SynthMenuUI.paint(SynthMenuUI.java:283)
at javax.swing.plaf.synth.SynthMenuUI.update(SynthMenuUI.java:248)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5194)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5142)
at javax.swing.JComponent.paintImmediately(JComponent.java:4952)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:255)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:170)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:155)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:136)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
saija
Offline
Joined: 2006-04-17

Looking at my code i just found the cause:

In the Main class i set the Nimbus l&f, then i show the main window gui, there i have a menu, there a menu item shows a JFileChooser, the method that launch that JFileChooser set a new l&f, the Plastic3DLookAndFeel, commenting out this new l&f setting the error doesn't appear and the application runs just fine.

looking at this error i just wonder: cannot i set various l&f in the same application?

Add the code of some example i did to test this issue:

[code]
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
*
* @author julborga
*/
public class Main {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
crearGui();
}

});
}

private static void crearGui() {

try {

for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}

BadNimbusApp mainGUI = new BadNimbusApp();

} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Some bad thing happened : \n" + ex.
getMessage(), "Upss !", JOptionPane.ERROR_MESSAGE);
}
}
}
[/code]

[code]
import com.jgoodies.looks.plastic.Plastic3DLookAndFeel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

/**
*
* @author julborga
*
Creado el 20/05/2010 a las 10:13:58 AM
*/
public class BadNimbusApp extends JFrame implements ActionListener {

public BadNimbusApp() {
super("Testing Nimbus in JDK 7 B92");
addComponents();
setSize(new Dimension(300, 250));
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

private void addComponents() {
JPanel panel1 = new JPanel(new GridLayout(2, 1));
JPanel panel2 = new JPanel(new GridLayout(1, 2));

JTextArea text1 = new JTextArea(50, 100);
JButton ok = new JButton("ok");
ok.setActionCommand("ok");
ok.addActionListener(this);

JButton cancel = new JButton("cancel");
cancel.setActionCommand("cancel");
cancel.addActionListener(this);

panel1.add(new JScrollPane(text1));
panel1.add(panel2);
panel2.add(ok);
panel2.add(cancel);

getContentPane().add(panel1);

JMenuBar menuBar = new JMenuBar();
JMenu optionsMenu = new JMenu("Options");
JMenuItem okItem = new JMenuItem("ok");
okItem.addActionListener(this);
okItem.setActionCommand("ok");
optionsMenu.add(okItem);
menuBar.add(optionsMenu);

setJMenuBar(menuBar);

}

public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();

switch (command) {
case "ok":
getFile();
break;
case "cancel":
setVisible(false);
dispose();
break;
}
}

private File getFile() {
try {
UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
} catch (Exception e) {
}

JFileChooser chooser = new JFileChooser("C:\\");

int returnValue = chooser.showOpenDialog(null);

String fileName;
File someFile = null;

if (returnValue == JFileChooser.APPROVE_OPTION) {
fileName = chooser.getSelectedFile().getAbsolutePath();
someFile = new File(fileName);
}

return someFile;
}

}
[/code]

Message was edited by: saija

anthony_p
Offline
Joined: 2006-07-24

> looking at this error i just wonder: cannot i set
> various l&f in the same application?

I don't think this is a good idea. The L&F is a global property. Even though you want to apply it to the file chooser only, components in other frames may be repainted simultaneously while the file chooser is open. Since they think they still use the old L&F, a problem may occur since you've already set a new global L&F.

I suggest to use just one L&F in your application.

saija
Offline
Joined: 2006-04-17

Ok, thanks for the advice, i just used 2 l&f because i copied & pasted the code for the display of the JFilechooser from some other class. Btw using the jdk6 also raises this NPE...

Thank you for your time.

saija
Offline
Joined: 2006-04-17

also i just want to add, using the method described in "Enabling the Nimbus Look and Feel": http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/nimbus.html

also raises an NPE

walterln
Offline
Joined: 2007-04-17

Are you setting the L&F and creating you GUI on the EDT? It think Nimbus is more sensitive to that. Does it happen with JDK6?

saija
Offline
Joined: 2006-04-17

Hi walterln

I'm creating my gui outside of the edt(i think...), here's the code i'm using:

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

/**
* Muestra la venana de Gui principal de la aplicacion
* English: Shows the main window gui
*/
@Override
public void run() {
//english: createGUI
crearGui();
}

});

}

/**
* Inicializa el Look&Feel y crea la ventana principal de la gui de la
* aplicacion
* English: Initialize the L&F and creates the main window gui
*/
private static void crearGui() {
Plastic3DLookAndFeel vl_objLookAndFeel = null;

try {
vl_objLookAndFeel = new Plastic3DLookAndFeel();

//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}

//here i instatiate some JFrame derived class...
clsSOmeClass gui = new clsSOmeClass();

} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "error....", "Upss !", JOptionPane.ERROR_MESSAGE);
}
}

About the jdk 6, right now i don't have it installed, let me download and install that and i'll give some feedback...

anthony_p
Offline
Joined: 2006-07-24

Hi,

(please use [ code ] and [ /code ] tags w/o spaces, lowercase to post code)

When you're doing invokeLater(), you're actually transferring the Runnable to execute on the EDT, so your current code looks fine. However, please make sure that all the code in your application accesses Swing component from the EDT only (i.e. either right from the event handlers, or by using the invokeLater/invokeAndWait() from other threads if any).

Also, could you please get rid of this Plastic3DLookAndFeel thing? Just curious if that could cause some collisions with Nimbus.

Also, trying out jdk7 b94 might be worth it since there have been some Nimbus related fixes recently.

Please report the results of your experiments afterwards. If it still fails, please provide us with a *small* and complete piece of code (including import statements, class declaration, etc. - the complete source .java file) that reproduces the problem. We'll see if we can reproduce it here and file a bug if necessary.

--
best regards,
Anthony