Erro ao executar TextFieldFrame [RESOLVIDO]

2 respostas
hackum

Olá a todos!

Veja o código:

//TextFieldFrame.java
//Demonstrating the class JTextField

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
public class TextFieldFrame extends JFrame {
	
	private JTextField textField1; //field of text with size set
	private JTextField textField2; //field of text built with text
	private JTextField textField3; //field of text with text and size
	private JPasswordField passwordField; //field of password with text
	
	//builder TextFieldFrame add JTextFields the JFrame
	public TextFieldFrame()
	{
		super("Testing JTextField and JPasswordField");
		setLayout(new FlowLayout()); //sets the layout of frame
		
		//builds textField with 10 columns
		textField1 = new JTextField(10);
		add(textField2); //add tectField1 to JFrame
		
		//builds field with the text default and 21 columns
		textField3 = new JTextField("Uneditable text field",21);
		textField3.setEditable(false); //off the edition
		add(textField3); //add textField3 to JFrame
		
		//builds passwordField with the text default
		passwordField = new JPasswordField("Hidden text");
		add(passwordField); //add passwordField to JFrame
		
		//handles of event registers
		TextFieldHandler handler = new TextFieldHandler();
		textField1.addActionListener(handler);
		textField2.addActionListener(handler);
		textField3.addActionListener(handler);
		passwordField.addActionListener(handler);
		
		
	}//end construction
	
	//class internal private for treatment of event
	private class TextFieldHandler implements ActionListener
	{
		//process events of field of text
		public void actionPerformed(ActionEvent event)
		{
			String string = ""; //states string the be displayed
			
			//user pressed Enter on JTextField textField1
			if(event.getSource() == textField1)
				string = String.format("textField1: %s",event.getActionCommand());
			//user pressed Enter on JTextField textField2
			else if (event.getSource() == textField2)
				string = String.format("textField2: %s",event.getActionCommand());
			//user pressed Enter on JTextField textField3
			else if (event.getSource() == textField3)
				string = String.format("textField3: %s",event.getActionCommand());
			
			//user pressed Enter on JTextField passwordField
			else if (event.getSource() == passwordField)
				string = String.format("passwordField: %s",new String (passwordField.getPassword()));
			
			//display the content of JTextField
			JOptionPane.showMessageDialog(null, string);

	
			
	}
		
		
	}
	

}

//TextFieldTest.java
//Testing TextFieldFrame.

import javax.swing.JFrame;
public class TextFieldTest {
	
	public static void main(String args[])
	{
		
		TextFieldFrame textFieldFrame = new TextFieldFrame();
		textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		textFieldFrame.setSize(325,100); //sets the size of frame
		textFieldFrame.setVisible(true); //display the frame
		
	}

}

Veja o error ao executar:

Exception in thread "main" java.lang.NullPointerException
	at java.awt.Container.addImpl(Container.java:1066)
	at java.awt.Container.add(Container.java:974)
	at javax.swing.JFrame.addImpl(JFrame.java:556)
	at java.awt.Container.add(Container.java:377)
	at TextFieldFrame.<init>(TextFieldFrame.java:26)
	at TextFieldTest.main(TextFieldTest.java:10)

O que está acontecendo ? Não há nenhum error aparente no código. Mas ao executar dá erro.

2 Respostas

Ivan_Alves

Você está tentando adicionar um objeto não instanciado no seu JFrame veja na linha 25 e 26 está assim

//builds textField with 10 columns textField1 = new JTextField(10); add(textField2); //add tectField1 to JFrame

você instancia o textField1 e adiciona o textField2 que não foi instanciado

flw

hackum

Olá Ivan!
Muito obrigado por ter me ajudado!
Consigui resolver o problema!
Um grande abraço!

Criado 17 de julho de 2011
Ultima resposta 18 de jul. de 2011
Respostas 2
Participantes 2