JDK-6793340 : Regression: Component.isValid() is called during component initialization
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6u10,6u12
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2009-01-13
  • Updated: 2011-01-19
  • Resolved: 2009-01-14
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
Here's the thread:
  http://forums.java.net/jive/post!reply.jspa?messageID=325828

Exception with this test:
#>/cygdrive/m/re/1.6.0_12/promoted/all/b01/binaries/windows-i586/bin/java TestJPanel
isValid
Exception in thread "main" java.lang.NullPointerException
        at TestJPanel.isValid(TestJPanel.java:18)
        at java.awt.Component.invalidateIfValid(Component.java:2707)
        at java.awt.Component.setLocale(Component.java:1777)
        at javax.swing.JComponent.<init>(JComponent.java:595)
        at javax.swing.JPanel.<init>(JPanel.java:65)
        at javax.swing.JPanel.<init>(JPanel.java:92)
        at javax.swing.JPanel.<init>(JPanel.java:100)
        at TestJPanel.<init>(TestJPanel.java:7)
        at TestJPanel.main(TestJPanel.java:22)

Comments
EVALUATION The stack trace suggests that an overidden setLocale() methods might cause the same problem. Many other public methods may in fact be invoked from constructors. Actually Java specification does not guarantee that public methods will never be invoked from within the constructor. Since the public methods may be overidden, the developer who does so must take some measures to check if the necessary fileds have already been initialized (like checking if (field != null) { ... }). Therefore, this is not a defect in Java.
14-01-2009

EVALUATION The change was introduced with this delta in Component.java D 1.437 08/10/31 18:17:15 ap203012 881 880 00376/00016/09399 MRs: COMMENTS: Fixed 4811096: Allow mixing of heavy and lightweight components The problem is that isValid can be overridden and may be relying on some data initialized in the subclass' constructor. Whether it's a bug or not - not clear, I don't see a contract saying that these methods can't be called during construction, although generally it's not nice.
13-01-2009