Skip to main content

Initialization before constructor necessary?

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
CSS
Offline
Joined: 2013-05-26

Hi eyerybody,

if I initialize a member in the constructor, I get a NullPointerException when I click the button. If I initialize it before the constructor as default value, everything works fine. How come? See comments 1), 2) and 3) below. Does Swing require an initialization as default value? I'm greateful for help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MainClass {

public static void main(String[] args) {
new ApplicationClass();
}
}

class ApplicationClass{
int var;
MyPanel plotRegion; // 1) causes problems
// MyPanel plotRegion = new MyPanel(); // 2) works

// built GUI
ApplicationClass(){
System.out.println("Entering constructor of ApplicationClass");

var = 0; // flag: 0 => don't paint, 1 => paint cross
MyPanel plotRegion = new MyPanel(); // 3) causes problems

JButton button = new JButton("Cross");
MyListener listener = new MyListener();
button.addActionListener(listener);

JFrame frame = new JFrame("Painter");
frame.setLayout(new FlowLayout());
frame.add(plotRegion);
frame.add(button);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

System.out.println("Leaving constructor of ApplicationClass");
}

// inner class of ApplicationClass
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
var = 1; // flag: 1 => paint cross
plotRegion.repaint();
}
}

// inner class of ApplicationClass
class MyPanel extends JPanel{

MyPanel(){
super();
System.out.println("Leaving constructor of MyPanel");
}

public void paintComponent(Graphics g){

g.translate(150,150); // Ursprung Umsetzen
g.setColor(Color.black);
switch(var){
// paint nothing
case 0: break;
// paint cross
case 1: g.drawLine(-100,-100,100,100);
g.drawLine(100, -100, -100, 100);
break;
}
}

// Methode liefert Minimale Größe von Malfläche
public Dimension getMinimumSize(){
return new Dimension(300,300);
}

public Dimension getPreferredSize(){
return getMinimumSize();
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
anthony_p
Offline
Joined: 2006-07-24

Hi,

There's a major and very serious problem with your code - it violates the threading contract of Swing by accessing its APIs on a user thread. I suggest you to read this:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

and fix your code accordingly. After that, if the problem persists, please post a fixed code here and we'll see what might be wrong with it.

PS. Please wrap the source code with code tags (see the toolbar above the text area where you're typing your messages to - the "<>" button.)

PPS. Are you only learning Swing, or is it some legacy code you're dealing with? If you're just starting to learn, please check out JavaFX first and consider using it for your GUI instead of Swing because the latter is a legacy GUI toolkit.

--
best regards,
Anthony