Problema com ActionListener [RESOLVIDO]

6 respostas
G
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Janela extends JFrame implements ActionListener{

	private static final long serialVersionUID = 3482477293683784221L;
	
	JLabel label;
	JTextField textfield;
	JButton button;
	JPanel panel; 
	
	public Janela() {
		
		this.setSize(350,100);
		JLabel label = new JLabel("Seu nome:");
		JTextField textField = new JTextField(20);
		JButton button = new JButton("Ok");
		button.addActionListener(this);
		JPanel panel = new JPanel();
		panel.add(label);
		panel.add(textField);
		panel.add(button);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.add(panel);
		
	}

	public void actionPerformed(ActionEvent e) {
		panel.setBackground(Color.BLACK);
	}
}

******************************************Ao clicar no botão retorna

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Janela.actionPerformed(Janela.java:40)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

alguma idéia?

6 Respostas

W

Ola,
Voce ta criando instancias dos objetos duas vezes, primeiro fora do construtor Janela e depois dentro do Janela.

JLabel label; 
JTextField textfield; 
JButton button; 
JPanel panel; // *** definiu panel, sem inicializar. ***

e depois (dentro do construtor Janela)

JLabel label = new JLabel("Seu nome:"); 
JTextField textField = new JTextField(20); 
JButton button = new JButton("Ok"); 
button.addActionListener(this); 
JPanel panel = new JPanel(); // definiu outra instancia e inicializou
panel.add(label); 
panel.add(textField); 
panel.add(button);

Provavelmente o panel que eh referenciado dentro do event handler eh o panel global, ou seja, o que vc definiu fora do metodo Janela
que eh nulo.

public void actionPerformed(ActionEvent e) { 
     panel.setBackground(Color.BLACK);  // esta eh a instancia que vc definiu fora do construtor e nao inicializou!!!
}

//Daniel

ViniGodoy

Quando for postar códigos, por favor, use a tag code:
http://www.guj.com.br/java/50115-voce-e-novo-no-guj-vai-criar-um-topico-e-colar-seu-codigo-fonte-leia-aqui-antes-por-favor

ViniGodoy
Seguindo as dicas do Daniel:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Janela extends JFrame implements ActionListener{

	private static final long serialVersionUID = 3482477293683784221L;
	
	JLabel label;
	JTextField textfield;
	JButton button;
	JPanel panel; 
	
	public Janela() {
		
		this.setSize(350,100);
		label = new JLabel("Seu nome:");
		textField = new JTextField(20);
		button = new JButton("Ok");
		button.addActionListener(this);
		panel = new JPanel();
		panel.add(label);
		panel.add(textField);
		panel.add(button);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.add(panel);
		
	}

	public void actionPerformed(ActionEvent e) {
		panel.setBackground(Color.BLACK);
	}
}
G
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Janela extends JFrame implements ActionListener{

	private static final long serialVersionUID = 3482477293683784221L;
	
	public Janela() {
		
		this.setSize(350,100);
		JLabel label = new JLabel("Seu nome:");
		JTextField textField = new JTextField(20);
		JButton button = new JButton("Ok");
		button.addActionListener(this);
		JPanel panel = new JPanel();
		panel.add(label);
		panel.add(textField);
		panel.add(button);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.add(panel);
	}

	public void actionPerformed(ActionEvent e) {
		
		this.panel.setBackground(Color.BLACK);
	}
}

Agora:

panel cannot be resolved or is not a field Janela.java /Interface/src line 33 Java Problem

ViniGodoy

Você não pode criar uma variável local e tentar acessa-la em outro método. Você tentou rodar o código que postei?

O conceito de escopo é bastante básico, você deveria conhece-lo bem antes de começar com interface gráfica.

G

Ah entendi, não tinha entendido antes! Obrigado. Agora funcionou.

Criado 2 de novembro de 2011
Ultima resposta 2 de nov. de 2011
Respostas 6
Participantes 3