Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Beans Bound Properties 1

Status
Not open for further replies.

squidster

Technical User
Oct 13, 2002
55
GB
HI,

I'm very new to java and have been learning about bound properties but have got stuck trying out a test program which keeps throwing Null Pointer Exceptions.

I have mamnaged to bind the text property but have problems with the foreground and font properties, I think this may be something to do with overiding methods incorrectly and have tried alternatives but still no luck.

The bean code with the properties is:-
[tt]
import java.awt.*;
import javax.swing.JPanel;
import java.beans.*;
import java.awt.font.*;
import java.awt.color.*;

public class GreenBox extends JPanel {

public GreenBox() {
setBackground(Color.white);
setForeground(Color.black);
setText("Help meeeeeeee!");
}

//Instance variables
private String text;
private java.awt.Font font;
private java.awt.Color foreground;

/*Create a property-change listener to manage the list of listeners

private transient PropertyChangeSupport changes =
new PropertyChangeSupport(this);

/*Create add/remove methods for the property-change listener

public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
super.addPropertyChangeListener(l);
changes.addPropertyChangeListener(l);
}
public synchronized void removePropertyChangeListener(PropertyChangeListener
l) {
super.removePropertyChangeListener(l);
changes.removePropertyChangeListener(l);
}

/**
* setText that notifies the listeners of changes
* via firePropertyChange() method
*/
public void setText(String text) {
String oldText = this.text;
this.text = text;
changes.firePropertyChange("text", oldText, text);
repaint();
}
//getText method
public String getText() {
return text;
}

/* public void setFont(java.awt.Font font) {
java.awt.Font oldFont = this.font;
this.font = font;
changes.firePropertyChange("font", oldFont, font);
repaint();
}
public java.awt.Font getFont() {
return font;
}

// Alternative Overide set Font in super class
public void setFont(java.awt.Font font) {
java.awt.Font oldFont = getFont();
super.setFont(font);
changes.firePropertyChange("font", oldFont, font);
}
public java.awt.Font getFont() {
return super.getFont();
}


public void setForeground(java.awt.Color foreground) {
java.awt.Color oldForeground = this.foreground;
this.foreground = foreground;
changes.firePropertyChange("foreground", oldForeground, foreground);
repaint();
}
public java.awt.Color getForeground() {
return foreground;
}*/
}
[/tt]
I've commented out the bits that are causing problems.

Any help would be appreciated as I'm going slowly mad trying to figure this out.

If you would like the code for the test harness please let me know.
thanks
 
I suspect that the problem is that

private String text;
private java.awt.Font font;
private java.awt.Color foreground;

are not initialized to begin with. In your constructor, you are calling setForeground which sets oldForeground to this.foreground. Since this.foreground was never initialized, it's null and now so is oldForeground. You pass oldForeground to the firePropertyChange method which I'll bet is what's throwing the null pointer exception.

Try initializing them to bogus values like this:

private String text = "";
private java.awt.Font font = super.getFont();
private java.awt.Color foreground = Color.white;

Let us know what happens... "When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Thanks idarke, have tried your suggestion but still get the same error.

The excat error produced is:-
[tt]
java.lang.NullPointerException
at exercise1.GreenBox.setFont(GreenBox.java:70)
at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:90)
at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:51)
at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:42)
at javax.swing.JComponent.setUI(JComponent.java:322)
at javax.swing.JPanel.updateUI(JPanel.java:107)
at javax.swing.JPanel.<init>(JPanel.java:67)
at javax.swing.JPanel.<init>(JPanel.java:97)
at exercise1.GreenBox.<init>(GreenBox.java:22)
at exercise1.MenuTester.<init>(MenuTester.java:37)
at exercise1.MenuTest.<init>(MenuTest.java:20)
at exercise1.MenuTest.main(MenuTest.java:49)
[/tt]
I'm using JBuilder which when you select the error produced takes you to the line where it occurs, this initially points to:-
[tt]
change.firePropertyChange(&quot;font&quot;, oldFont, font);
[/tt]
in the setFont method.
 
It's not clear which setFont method you're trying to use, so I picked the first one. I put some debug messages in the SetFont method like so:

public void setFont(java.awt.Font font)
{
System.err.println(&quot;font=&quot; + font);
System.err.println(&quot;this.font=&quot; + this.font);
System.err.println(&quot;changes=&quot; + changes);

java.awt.Font oldFont = this.font;
this.font = font;
changes.firePropertyChange(&quot;font&quot;, oldFont, font);
repaint();
}

I got this output when I tried to create a GreenBox:

font=javax.swing.plaf.FontUIResource[family=dialog,name=Dialog,style=plain,size=12]

this.font=null

changes=null

I put this in a debugger and stepped through it and found that both getFont and setFont are being called before your constructor is even called.

I've never extended a JPanel before, so this puzzles me. But for the short term, you can merely assume that those methods are going to be called before the object is created and put in some checks to handle it, like this:

public void setFont(java.awt.Font font)
{
if (font == null || this.font == null || changes == null) return;

java.awt.Font oldFont = this.font;
this.font = font;
changes.firePropertyChange(&quot;font&quot;, oldFont, font);
repaint();
}
&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Sorry my code is a bit confusing, I was swapping between the two setFont methods to see if overriding was the problem.

I've done as suggested which allows the greenBox instance to be created in my test program :)

The only trouble now is that this program needs to call the greenBox.setFont method and passes a font value to it so debug out put now contains:-
[tt]
font=java.awt.Font[family=serif.bolditalic,name=Serif,style=bolditalic,size=12]

this.font=null

changes=java.beans.PropertyChangeSupport@25844f
[/tt]
which means that I can't do the remaining body of the method which is meant to make a copy of the current font (oldFont) and then set this.font to the new font (font) being passed, and then fire these cahnge values to the listeners. (I think??)

This is all kind of confusing as I can't see why the setText method didn't have the same problems.

Thanks again for your help, any other ideas?
 
Well, feel free to try removing the check for oldfont==null. I'm sure the fact that changes was null was what was causing your problem. The firePropertyChange method may not be bothered by a null argument after all.

SetText wasn't being called until your constructor was running - by that time changes was no longer null.

This is a good opportunity to get into JBuilders debugging abilities and step through your program. Good luck! &quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Yep its the changes=Null thats the problem.

I will give the debugging a go as no doubt I will encounter the odd prblem in the future!!

Thanks very much for your help

[thumbsUp2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top