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>