• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Initializing Fields/Objects

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you initialzie fields in a class when you declare them, what actually hapens and when?
This compiles, but does not run, no matter it seems, what line or order you initialize 'insets' in the init method. See error message right below the code. I put a second example of *almost* the same code. This difference is that 'theInsets' is initialized with an initializer when declared. What is agrevating, is that this not only compiles but actually runs! Why? What is going on that we don't see? Anybody?
</pre>
<pre>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyInsetsButton extends JApplet
{
JButton theButton;
Container theContainer;
Insets theInsets;
//= new Insets(1,1,1,1);

public void init()
{
theContainer = this.getContentPane();
theInsets = new Insets(1,1,1,1);
//Button
theButton = new JButton("Press Me");
theButton.addActionListener(new Switcher());
theContainer.add(theButton);
}

public Insets getInsets()
{
return theInsets;
}

public class Switcher implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
invalidate();
validate();
}
}
}
//the end

Error Messages:
java.lang.NullPointerException:
at java.awt.BorderLayout.layoutContainer(BorderLayout.java:608)
at java.awt.Container.layout(Container.java:494)
at java.awt.Container.doLayout(Container.java:484)
at java.awt.Container.validateTree(Container.java:553)
at java.awt.Container.validateTree(Container.java:560)
at java.awt.Container.validate(Container.java:535)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:488)
at sun.applet.AppletPanel.run(AppletPanel.java:282)
at java.lang.Thread.run(Thread.java:479)
java.lang.NullPointerException:
at java.awt.BorderLayout.layoutContainer(BorderLayout.java:608)
at java.awt.Container.layout(Container.java:494)
at java.awt.Container.doLayout(Container.java:484)
at java.awt.Container.validateTree(Container.java:553)
at java.awt.Container.validateTree(Container.java:560)
at java.awt.Container.validateTree(Container.java:560)
at java.awt.Container.validate(Container.java:535)
at sun.applet.AppletViewer$1$AppletEventListener.appletStateChanged(AppletViewer.java:226)
at sun.applet.AppletPanel.dispatchAppletEvent(AppletPanel.java:229)
at sun.applet.AppletPanel.appletResize(AppletPanel.java:616)
at java.applet.Applet.resize(Applet.java:149)
at java.applet.Applet.resize(Applet.java:160)
at sun.applet.AppletPanel.run(AppletPanel.java:331)
at java.lang.Thread.run(Thread.java:479)
Process Exit...
</pre>
For example this fairly simple example compiles and runs fine:

<pre>
public class MyInsetsButton extends JApplet
{
JButton theButton;
Container theContainer;
Insets theInsets = new Insets(1,1,1,1);

public void init()
{
theContainer = this.getContentPane();

//Button
theButton = new JButton("Press Me");
theButton.addActionListener(new Switcher());
//Container
}
public Insets getInsets()
{
return theInsets;
}
public class Switcher implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
invalidate();
validate();
}
}
}
//the end
</pre>

 
Trailboss
Posts: 23216
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what the answer to your question is, but check out this line
theInsets = new Insets(theInsets.top + 2
It looks like you are using theInsets before you have created it.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic