This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 22974
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!