Insubstantial / insubstantial

Swing look-and-feel library and assorted widgets
193 stars 57 forks source link

Adding any Component to Frame results in NullPointerException #17

Closed MatthiasGi closed 13 years ago

MatthiasGi commented 13 years ago

I've got this sample-code:

package de.gianfelice.debarim.gui;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel;

public class SampleApp extends JFrame {

    public SampleApp() {
        super("Sample app");
        try {
            UIManager.setLookAndFeel(new SubstanceGraphiteLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SampleApp.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            this.setLayout(new FlowLayout());
            this.add(new JButton("button"));
            this.add(new JCheckBox("check"));
            this.add(new JLabel("label"));
            this.setSize(new Dimension(250, 80));
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                SampleApp s = new SampleApp();
                s.setVisible(true);
            }
        });
    }
}

But everytime I try to run this code, I get a NullPointerException:

java.lang.NullPointerException
    at org.pushingpixels.substance.internal.ui.SubstanceButtonUI.__org__pushingpixels__substance__internal__ui__SubstanceButtonUI__installDefaults(SubstanceButtonUI.java:169)
    at org.pushingpixels.substance.internal.ui.SubstanceButtonUI.installDefaults(SubstanceButtonUI.java)
    at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:71)
    at org.pushingpixels.substance.internal.ui.SubstanceButtonUI.__org__pushingpixels__substance__internal__ui__SubstanceButtonUI__installUI(SubstanceButtonUI.java)
    at org.pushingpixels.substance.internal.ui.SubstanceButtonUI.installUI(SubstanceButtonUI.java)
    at javax.swing.JComponent.setUI(JComponent.java:662)
    at javax.swing.AbstractButton.setUI(AbstractButton.java:1782)
    at javax.swing.JButton.updateUI(JButton.java:128)
    at javax.swing.AbstractButton.init(AbstractButton.java:2149)
    at javax.swing.JButton.(JButton.java:118)
    at javax.swing.JButton.(JButton.java:91)
    at de.gianfelice.debarim.gui.SampleApp.(SampleApp.java:21)
    at de.gianfelice.debarim.gui.SampleApp$1.run(SampleApp.java:39)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Everything is running under NetBeans 7.0 and Windows 7 x64. I'm not using Maven to do this.

Maybe I'm only doing something wrong?

If I could provide any further information, please let me know.

Greetings from Germany, Matthias

shemnon commented 13 years ago

Github is no JIRA. I assumed it would post my comment when I closed it. Instead it looked like I posted it but it actually ate it. Bad UI... Bad UI!

Anyway, the issue is the timing of setting the look and feel. You need to set it prior to creating any JComponent, and certianly not inside the constructor for a JFrame. The super constructor creates some features of JFrame that are ultimately dependent on the look and feel, and changing things mid-flight is not something the code is expecting to happen.

By moving the look and feel configuration to the main method I was able to make things work. Here's what worked for me.

package de.gianfelice.debarim.gui;

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import org.pushingpixels.substance.api.skin.SubstanceGraphiteLookAndFeel;

public class SampleApp extends JFrame {

    public SampleApp() {
        super("Sample app");

        try {
            this.setLayout(new FlowLayout());
            this.add(new JButton("button"));
            this.add(new JCheckBox("check"));
            this.add(new JLabel("label"));
            this.setSize(new Dimension(250, 80));
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(new SubstanceGraphiteLookAndFeel());
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(SampleApp.class.getName()).log(Level.SEVERE, null, ex);
                }
                JFrame.setDefaultLookAndFeelDecorated(true);
                SampleApp s = new SampleApp();
                s.setVisible(true);
            }
        });
    }
}
disrvptor commented 13 years ago

FWIW, I was able to run the original code with no errors under Java 1.6.0_20.

MatthiasGi commented 13 years ago

First: Thanks to you for trying to help me, but unfortunately this does not work for me:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.pushingpixels.substance.internal.utils.SubstanceColorUtilities.getDefaultBackgroundColor(SubstanceColorUtilities.java:830)
    at org.pushingpixels.substance.internal.utils.SubstanceColorUtilities.getBackgroundFillColor(SubstanceColorUtilities.java:733)
    at org.pushingpixels.substance.internal.ui.SubstancePanelUI.__org__pushingpixels__substance__internal__ui__SubstancePanelUI__installDefaults(SubstancePanelUI.java:73)
    at org.pushingpixels.substance.internal.ui.SubstancePanelUI.installDefaults(SubstancePanelUI.java)
    at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:39)
    at org.pushingpixels.substance.internal.ui.SubstancePanelUI.__org__pushingpixels__substance__internal__ui__SubstancePanelUI__installUI(SubstancePanelUI.java)
    at org.pushingpixels.substance.internal.ui.SubstancePanelUI.installUI(SubstancePanelUI.java)
    at javax.swing.JComponent.setUI(JComponent.java:662)
    at javax.swing.JPanel.setUI(JPanel.java:136)
    at javax.swing.JPanel.updateUI(JPanel.java:109)
    at javax.swing.JPanel.(JPanel.java:69)
    at javax.swing.JPanel.(JPanel.java:92)
    at javax.swing.JPanel.(JPanel.java:100)
    at javax.swing.JRootPane.createGlassPane(JRootPane.java:528)
    at javax.swing.JRootPane.(JRootPane.java:348)
    at javax.swing.JFrame.createRootPane(JFrame.java:255)
    at javax.swing.JFrame.frameInit(JFrame.java:236)
    at javax.swing.JFrame.(JFrame.java:203)
    at de.gianfelice.debarim.gui.SampleApp.(SampleApp.java:12)
    at de.gianfelice.debarim.gui.SampleApp$1.run(SampleApp.java:38)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I just copied and pasted your code. I downloaded the jars from your website, did I something wrong?

Again thank you very much and greetings from Germany, Matthias

disrvptor commented 13 years ago

Matthias, can you provide the links where you downloaded the jars from?

MatthiasGi commented 13 years ago

Hello lunaflare,

of course I can! Here is it: http://shemnon.com/speling/2011/04/insubstantial-62-release.html

If I understood everything right, it is the private homepage of shemnon. On my Windows 7 here I don't have git, but on my GNU/Linux I do. But I don't think, that it would change anything to download them there...

Greetings from Germany and I'm really sorry for my poor English, Matthias

Edit: Well, also it would be really great, if anyone could provide the sourcecode of the last two examples of the metioned page?

disrvptor commented 13 years ago

Hmm, I still wasn't able to produce your issue using those libraries. Here's what I did:

  1. Download the substance and trident jars
  2. Copy the above code into SampleApp.java in the same directory
  3. Run javac -classpath substance-6.2.jar -d bin SampleApp.java
  4. Run java -classpath substance-6.2.jar;trident-6.2.jar;bin de.gianfelice.debarim.gui.SampleApp

I'm running 32-bit Java 1.6.0_20 on the command line of Windows 7 x64.

The Flamingo example code is at https://github.com/Insubstantial/insubstantial/blob/master/substance-flamingo/src/test/java/test/substance/ribbon/NewCheckRibbon.java

MatthiasGi commented 13 years ago

I could find the error: In my IDE, I also checked the library substance-swingx. I had another error in this project and included everything, what could work. That error could be solved.

Well, and without this library, everything works just well. Sorry for my dumb issuereport!

Thanks to all for the helping and sorry again for bothering you with this...

Greetings from Germany, Matthias

P.S.: And thanks for the link @lunaflare!

Edit: Well, after a bit testing, I must assume, that the windows are not compatible to some Windows 7 Features: Aero Snap (Moving them to one side for maximizing and organizing), also some Shortcuts (Win + Up etc) don't work. I will try it on GNOME 3 later, if anyone is intrested. I don't know how important this is.

disrvptor commented 13 years ago

No worries and glad to help.