sto cercando di creare un programmino molto semplice tanto per prenderci la mano con le interfacce grafiche, praticamente consiste nel visualizzare 5 TextField (nome, cognome, ditta, via, telefono) e con sotto 2 pulsanti, un add e un exit, l'exit funzia bene, l'add anche se diciamo non cerco di agganciarlo ai textfield.
questa è la classe che crea i problemi:
package lavoro1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
/**
*
Title: </p>
*
Description: </p>
*
Copyright: Copyright (c) 2003</p>
*
Company: </p>
* @author unascribed
* @version 1.0
*/
public class frame extends JFrame {
private JDialog dialog;
private JButton addbutton;
private JButton exitbutton;
private JTextField nometext;
private JTextField cognometext;
private JTextField dittatext;
private JTextField viatext;
private JTextField telefonotext;
private String nome;
private String cognome;
private String ditta;
private String via;
private String telefono;
public frame() {
super ("add scheda");
JTextField nometext = new JTextField("inserisci nome");
nometext.setEditable(true);
JTextField cognometext = new JTextField("inserisci cognome");
cognometext.setEditable(true);
JTextField dittatext = new JTextField("inserisci ditta");
dittatext.setEditable(true);
JTextField viatext = new JTextField("inserisci via");
viatext.setEditable(true);
JTextField telefonotext = new JTextField("inserisci tel.");
telefonotext.setEditable(true);
JButton addbutton = new JButton("ADD");
JButton exitbutton = new JButton("EXIT");
//CREO ASCOLTATORI
ActionListener addlistener = new AddButtonListener();
ActionListener exitlistener = new ExitButtonListener();
addbutton.addActionListener(addlistener);
exitbutton.addActionListener(exitlistener);
//setto il bottono EXIT come bottone preselezionato
getRootPane().setDefaultButton(exitbutton);
//disposizione nord:
JPanel nordpanel =new JPanel();
nordpanel.setBorder(BorderFactory.createEmptyBorde r(10,4,10,4));
nordpanel.add(nometext);
nordpanel.add(cognometext);
nordpanel.add(dittatext);
nordpanel.add(viatext);
nordpanel.add(telefonotext);
//disposizione sud:
JPanel sudpanel=new JPanel();
sudpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
sudpanel.add(addbutton);
sudpanel.add(exitbutton);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.NORTH, nordpanel);
getContentPane().add(BorderLayout.SOUTH, sudpanel);
pack();
setVisible(true);
}
//ASCOLTATORE DEL PULSANTE ADD / EXIT
class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
nome= nometext.getText();
cognome=cognometext.getText();
ditta=dittatext.getText();
via=viatext.getText();
telefono= telefonotext.getText();
System.out.println(nome+" "+cognome+" "+ditta+" "+via+" "+telefono+"");
//lavoro.add(nome, cognome, ditta, via, telefono);
}
}
class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
System.exit(0);
}catch (Exception ex){}
}
}
}
nella classe AddButtonListener ho inserito dei collegmaneti alle variabili che dovrebbero contenere i valori delle JTextField, ma mi rilascia solamente errori quando premo su add. (il compilatore non da errori, gli errori si creano durante l'esecuzione)
ecco l'errore:
Exception occurred during event dispatching:
java.lang.NullPointerException
at lavoro1.frame$AddButtonListener.actionPerformed(fr ame.java:100)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1450)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(AbstractButton.java:1504)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:250)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.jav a:3715)
at java.awt.Component.processEvent(Component.java:354 4)
at java.awt.Container.processEvent(Container.java:116 4)
at java.awt.Component.dispatchEventImpl(Component.jav a:2593)
at java.awt.Container.dispatchEventImpl(Container.jav a:1213)
at java.awt.Component.dispatchEvent(Component.java:24 97)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:2125)
at java.awt.Container.dispatchEventImpl(Container.jav a:1200)
at java.awt.Window.dispatchEventImpl(Window.java:914)
at java.awt.Component.dispatchEvent(Component.java:24 97)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 339)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:85)