Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3881

Re: Avoiding NPEs caused

From "Mark Space" <mark.space@THRWHITE.remove-dii-this>
Subject Re: Avoiding NPEs caused
Message-ID <SXslk.34771$ZE5.1939@nlpi061.nbdc.sbc.com> (permalink)
Newsgroups comp.lang.java.gui
References <13afa58b-569c-4b70-80a5-d5c44182efb7@m73g2000hsh.googlegroups.com>
Date 2011-04-27 15:47 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.programmer
Royan wrote:

> abstract class AbstractModel {
>     public AbstractModel() {
> 	indirectCall();
>     }
> 
>     private void indirectCall() {
> 	setSomeValue(new Integer(1));
>     }
> 
>     public void setSomeValue(Integer value) {
>         firePropertyChange("someProperty", null, value);
>     }

As Lew said, the chained call from the constructor is the problem. Never 
ever do this.  Calling any "foriegn" method too (like "indirectCall()" ) 
is broken because you don't know what they will call.  If they call a 
public, overriden method (which happens here), then you could be in a 
lot of trouble.

Joshua Bloch describes almost the exact code you have with a big "Don't 
Do This!" sign next to it.  It's Item 17: Design and Document for 
Inheritance or Else Prohibit It, in Effective Java 2nd edition.

A couple of classic solutions:  use composition instead of inheritance. 
  Use a static factory (maybe with a Strategy Pattern to "plug in" the 
exact Model you want).

Composition would use something like the Decorator Pattern.  Use a 
concrete class, DefaultModel, instead of an abstract class 
AbstractModel, and wrap the new class around the default one.

But we kinda don't have enough info to help you out of this, it's very 
much a design issue.  "indirectCall" is the problem, you can NEVER do 
this and expect it to work well.  It must go.  What are you really 
trying to do here?

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Avoiding NPEs caused "Royan" <royan@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
  Re: Avoiding NPEs caused "Mark Space" <mark.space@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
    Re: Avoiding NPEs caused "Royan" <royan@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000
      Re: Avoiding NPEs caused "Mark Space" <mark.space@THRWHITE.remove-dii-this> - 2011-04-27 15:47 +0000

csiph-web