Code:package gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class javaFP extends JApplet implements ActionListener{
JButton b;
JButton b1;
JButton b2;
JTextField field;
public String text;
public void init(){
try{
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
swingGUI();
}
});
}
catch(Exception e){
System.out.println(e);
}
}
public void swingGUI(){
/*b= new JButton("Button 1");
b.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
field.setText("Button 1");
}
}
);
b1= new JButton("Button 2");
b1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
field.setText("Button 2");
}
}
);*/
JButton b = new JButton(new AbstractAction ("Button 1") {
public void actionPerformed( ActionEvent e ) {
field.setText("Button 1");
}
});
JButton b1 = new JButton(new AbstractAction ("Button 2") {
public void actionPerformed( ActionEvent e ) {
field.setText("Button 2");
}
});
JButton b2 = new JButton(new AbstractAction ("Button 3") {
public void actionPerformed( ActionEvent e ) {
field.setText("Button 3");
}
});
JTextField field = new JTextField(20);
setLayout(new FlowLayout());
setSize(300, 250);
setVisible(true);
add(b);
add(b1);
add(b2);
add(field);
field.setEditable(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
Using this code I keep getting an error. The way this is supposed to work is that when I click on any button, 1 2 or 3, it puts the name of the button into the TextField. When I click on a button all I get is this:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at gui.javaFP$2.actionPerformed(javaFP.java:46)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6505)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:627 0)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Component.dispatchEvent(Component.java:46 87)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:703)
at java.awt.EventQueue.access$000(EventQueue.java:102 )
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 673)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:97)
No Yodas please...

