🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #653

Re: null pointer while changing L & F

#653
From: GEORGE
Date: Tue, 19 Apr 2005 01:23
188 lines
7878 bytes
I am posting below the code for my "app":  just a single runnable JDialog 
subclass.  There are three buttons Metal,  Motif, and Window .  I find that 
if I change  the L&F directly in the action class for these buttons (see the 
commented out lines in the action handlers) then there is no problem. 
However if I use the action handlers to set a Preferences object and make 
the JDialog class a PreferenceChangeListener then I get a null pointer 
exception.  The trouble is that each time I run the program , the NPE 
doesn't happen on the first button click.  Sometimes its the second, 
sometimes the third click, etc.


The Code
---------------------------------------------------------------------------------------------------------------------------------------------------------
package configuration;

import javax.swing.*;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.prefs.*;
import com.georgex.util.LookAndFeelHandler;

public class OptionsDialog1 extends JDialog
  implements PreferenceChangeListener
{
 public OptionsDialog1() throws Exception {
    init();
 }

 public static void main(String[] args) {
  try {
   OptionsDialog1 optionsdialog1 = new OptionsDialog1();
   optionsdialog1.setVisible(true);
  } catch (Exception ex) {
      ex.printStackTrace();
    }
 }

  private  void init() throws Exception {
    userPreferences = Preferences.userRoot();
    this.setTitle("Configuration Options");
    this.setModal(true);
    this.setSize(500,500);

    scrollPane1.setViewportView(tree);
    scrollPane2.setViewportView(panel);
    splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setDividerLocation(150);
    splitPane.setLeftComponent(scrollPane1);
    splitPane.setRightComponent(scrollPane2);
    splitPane.setOneTouchExpandable(true);
    userPreferences.addPreferenceChangeListener(this);

    this.getContentPane().setLayout(new GridBagLayout());
    this.getContentPane().add(splitPane, new 
GridBagConstraints(0,0,1,1,1,1,GridBagConstraints.CENTER, 
GridBagConstraints.BOTH, new Insets(20,10,0,10),0,0));
    this.getContentPane().add(metalButton, new 
GridBagConstraints(0,1,1,1,1,0,GridBagConstraints.EAST, 
GridBagConstraints.NONE, new Insets(20,0,20,50),0,0));
    this.getContentPane().add(motifButton, new 
GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.EAST, 
GridBagConstraints.NONE, new Insets(20,10,20,0),0,0));
    this.getContentPane().add(windowsButton, new 
GridBagConstraints(2,1,1,1,0,0,GridBagConstraints.EAST, 
GridBagConstraints.NONE, new Insets(20,10,20,0),0,0));

    metalButton.addActionListener(new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
//     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
//     SwingUtilities.updateComponentTreeUI(this);
          userPreferences.putInt("LOOK_AND_FEEL", METAL);
    } catch (Exception ex) {}
      }
    });
    motifButton.addActionListener(new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
//     UIManager.setLookAndFeel(
//      "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//    SwingUtilities.updateComponentTreeUI(this);

          userPreferences.putInt("LOOK_AND_FEEL", MOTIF);
         }catch (Exception ex) {}
      }
    });

    windowsButton.addActionListener(new AbstractAction() {
      public void actionPerformed(ActionEvent e) {
        try {
// 
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//        SwingUtilities.updateComponentTreeUI(this);
          userPreferences.putInt("LOOK_AND_FEEL", WINDOWS);
      } catch (Exception ex) {}
      }
    });

  }

    public void preferenceChange(PreferenceChangeEvent e) {
     // System.out.println(e.getKey());
      try {
      int laf = userPreferences.getInt("LOOK_AND_FEEL", 
LookAndFeelHandler.METAL);
      switch (laf) {
       case LookAndFeelHandler.MOTIF:
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
         SwingUtilities.updateComponentTreeUI(this);
         return;
       case LookAndFeelHandler.WINDOWS:
         UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
         SwingUtilities.updateComponentTreeUI(this);
         return;
       default:
         UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
         SwingUtilities.updateComponentTreeUI(this);
        return;
      }
      } catch (Exception ex) {}
    }

  private void setLAF() {
    SwingUtilities.updateComponentTreeUI(this);

  }

  JSplitPane splitPane = new JSplitPane();
  JScrollPane scrollPane1 = new JScrollPane();
  JScrollPane scrollPane2 = new JScrollPane();
  JTree tree = new JTree();
  JPanel panel = new JPanel();
  JButton motifButton = new JButton("Motif");
  JButton metalButton = new JButton("Metal");
  JButton windowsButton = new JButton("Windows");
  Preferences userPreferences;
  public static final int METAL = 0;
  public static final int MOTIF = 1;
  public static final int WINDOWS = 2;
}


"Lori M Olson [TeamB]" <javadragon@techie.com> wrote in message 
news:426131bc$1@newsgroups.borland.com...
> GEORGE wrote:
>
>> I am using the code below to change the look and feel of my app using 3 
>> radio buttons for Metal, Motif and Windows.
>>
>> The L & F changes alright.  But I get a null pointer exception along the 
>> way.  The stack trace is also shown below but I am unable to make much of 
>> it.  Any help would be appreciated.
>>
>> TIA
>> George
>>
>
> What leads you to believe that piece of code is responsible?  It's not 
> part of the stack trace?  Is it that the problem goes away if you comment 
> out that code or something like that?
>
> I suspect that your code is simply exposing an different, underlying 
> problem.  Reading stack traces of Swing bugs is usually a thankless task. 
> I really hate finding NPE's in the JDK.  If you run in the debugger, you 
> can click on those line numbers and go directly to the line on which the 
> NPE happened.  Looks like something needed a preferred size to be set, and 
> it was not.  But that's just a wild guess.
>
> -- 
>
> Regards,
>
> Lori Olson [TeamB]
>
> ------------
>
> Save yourself, and everyone else, some time and search the
> newsgroups and the FAQ-O-Matic before posting your next
> question.
>
> Google Advanced Newsgroup Search
>     http://www.google.ca/advanced_group_search
> Other Newsgroup Searches:
>     http://www.borland.com/newsgroups/ngsearch.html
> Joi Ellis's FAQ-O-Matic:
>     http://www.visi.com/~gyles19/fom-serve/cache/1.html 
Technical Details
Message-ID: <42645d86$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!diablo.voicenet.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4260659e@newsgroups.borland.com> <426131bc$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,d97b42cc813c1e29
X-Google-Attributes: gid111d45,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "GEORGE" <geo@nowhere.net>
Newsgroups: borland.public.jbuilder.java.swing
References: <4260659e@newsgroups.borland.com> <426131bc$1@newsgroups.borland.com>
Subject: Re: null pointer while changing L & F
Date: Mon, 18 Apr 2005 21:23:10 -0400
Lines: 190
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-RFC2646: Format=Flowed; Response
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
NNTP-Posting-Host: 70.21.199.188
Message-ID: <42645d86$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1113873798 70.21.199.188 (18 Apr 2005 18:23:18 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!diablo.voicenet.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail