Board index » jbuilder » null pointer while changing L & F

null pointer while changing L & F


2005-04-16 09:08:45 AM
jbuilder1
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
------------------------------------------------------------------------------------------------
MY CODE
int laf = userPreferences.getInt("LOOK_AND_FEEL",
LookAndFeelPanel.METAL);
switch (laf) {
case LookAndFeelPanel.MOTIF:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
// this.pack();
return;
case LookAndFeelPanel.WINDOWS:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
// this.pack();
return;
default:
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
// this.pack();
----------------------------------------------------------------------------------------------
STACK TRACE
Exception occurred during event dispatching:
java.lang.NullPointerException
at
javax.swing.plaf.basic.BasicSplitPaneUI.getPreferredSize(BasicSplitPaneUI.java:1065)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1582)
at java.awt.GridBagLayout.GetLayoutInfo(GridBagLayout.java:911)
at java.awt.GridBagLayout.getLayoutInfo(GridBagLayout.java:816)
at java.awt.GridBagLayout.preferredLayoutSize(GridBagLayout.java:636)
at java.awt.Container.preferredSize(Container.java:1558)
at java.awt.Container.getPreferredSize(Container.java:1543)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1584)
at java.awt.GridBagLayout.GetLayoutInfo(GridBagLayout.java:911)
at java.awt.GridBagLayout.getLayoutInfo(GridBagLayout.java:816)
at java.awt.GridBagLayout.ArrangeGrid(GridBagLayout.java:1412)
at java.awt.GridBagLayout.arrangeGrid(GridBagLayout.java:1372)
at java.awt.GridBagLayout.layoutContainer(GridBagLayout.java:712)
at java.awt.Container.layout(Container.java:1401)
at java.awt.Container.doLayout(Container.java:1390)
at java.awt.Container.validateTree(Container.java:1473)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validateTree(Container.java:1480)
at java.awt.Container.validate(Container.java:1448)
at
javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:379)
at
javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:113)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:153)
at java.awt.Dialog$1.run(Dialog.java:515)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:234)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
 
 

Re:null pointer while changing L & F

GEORGE wrote:
Quote
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
de{*word*81}, 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
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 

Re:null pointer while changing L & F

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]" < XXXX@XXXXX.COM >wrote in message
Quote
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 de{*word*81}, 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
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 

{smallsort}

Re:null pointer while changing L & F

I am posting a runnable version of my "app". There are 3 buttons (for
Motif, Metal and Window) in the JDialog subclass. If I change the L&F
"directly" in the action handler for the buttons there is no problem.
However if I just set a Preferences object in the action handler with the
chosen look and feel and make the JDialog subclass a
PreferenceChangeListener (as shown below) then I get an NPE: and not always
on the first button click.
I would like to get it to work the second way because in my real app, I'd
like to be able to inform all the other containers of the L&F change by
making all of them implement PreferenceChangeListener. If there is a better
way to accomplish this, please let me know.
TIA
George
------------------------------------------------------------------------------------------------------------
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.*;
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");
// setLAF();
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");
userPreferences.putInt("LOOK_AND_FEEL", MOTIF);
setLAF();
}catch (Exception ex) {}
}
});
windowsButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
//
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// setLAF();
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;
}
----- Original Message -----
From: "Lori M Olson [TeamB]" < XXXX@XXXXX.COM >
Newsgroups: borland.public.jbuilder.java.swing
Sent: Saturday, April 16, 2005 11:39 AM
Subject: Re: null pointer while changing L & F
Quote
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 de{*word*81}, 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
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache
"Lori M Olson [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
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 de{*word*81}, 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
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 

Re:null pointer while changing L & F

I am posting a runnable version of my "app". There are 3 buttons (for
Motif, Metal and Window) in the JDialog subclass. If I change the L&F
"directly" in the action handler for the buttons there is no problem.
However if I just set a Preferences object in the action handler with the
chosen look and feel and make the JDialog subclass a
PreferenceChangeListener (as shown below) then I get an NPE: and not always
on the first button click.
I would like to get it to work the second way because in my real app, I'd
like to be able to inform all the other containers of the L&F change by
making all of them implement PreferenceChangeListener. If there is a better
way to accomplish this, please let me know.
TIA
George
----------------------------------------------------------------------------------------------------------------
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;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author
* @version 1.0
*/
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");
// setLAF();
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");
userPreferences.putInt("LOOK_AND_FEEL", MOTIF);
setLAF();
}catch (Exception ex) {}
}
});
windowsButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
//
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// setLAF();
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", METAL);
switch (laf) {
case MOTIF:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
return;
case 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]" < XXXX@XXXXX.COM >wrote in message
Quote
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 de{*word*81}, 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
www.google.ca/advanced_group_search
Other Newsgroup Searches:
www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
www.visi.com/~gyles19/fom-serve/cache/1.html
 

Re:null pointer while changing L & F

apologies for posting this thrice...I thought the deletes worked when I
posted this at home. But now I'm viewing this from work and I still see the
original two posts...Pl ignore.
"GEORGE" < XXXX@XXXXX.COM >wrote in message
Quote
I am posting a runnable version of my "app". There are 3 buttons (for
Motif, Metal and Window) in the JDialog subclass. If I change the L&F
"directly" in the action handler for the buttons there is no problem.
However if I just set a Preferences object in the action handler with the
chosen look and feel and make the JDialog subclass a
PreferenceChangeListener (as shown below) then I get an NPE: and not
always
on the first button click.

I would like to get it to work the second way because in my real app, I'd
like to be able to inform all the other containers of the L&F change by
making all of them implement PreferenceChangeListener. If there is a
better
way to accomplish this, please let me know.

TIA
George
--------------------------------------------------------------------------
--------------------------------------
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;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author
* @version 1.0
*/
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");
// setLAF();
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");
userPreferences.putInt("LOOK_AND_FEEL", MOTIF);
setLAF();
}catch (Exception ex) {}
}
});

windowsButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
try {
//

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel
");
Quote
// setLAF();
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", METAL);
switch (laf) {
case MOTIF:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
return;
case WINDOWS:

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel
");
Quote
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]" < XXXX@XXXXX.COM >wrote in message
news:426131bc$ XXXX@XXXXX.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 de{*word*81}, 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
>www.google.ca/advanced_group_search
>Other Newsgroup Searches:
>www.borland.com/newsgroups/ngsearch.html
>Joi Ellis's FAQ-O-Matic:
>www.visi.com/~gyles19/fom-serve/cache/1.html