Código:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Dimension;
public class Formulario extends JFrame implements ActionListener{
private JMenu menu1;
private JMenuBar mb;
private JMenuItem telefono,nombre,apellido;
public Formulario () {
mb=new JMenuBar();
setJMenuBar(mb);
menu1=new JMenu("Buscar");
mb.add(menu1);
telefono=new JMenuItem("telefono");
menu1.add(telefono);
telefono.addActionListener(this);
nombre=new JMenuItem("nombre");
menu1.add(nombre);
nombre=new JMenuItem("apellido");
menu1.add(apellido); //LINEA 40
}
public void actionPerformed(ActionEvent e) {
Container f=this.getContentPane();
if (e.getSource()==telefono) {
System.out.printf ("Su nombre de usuario es: ");
}
}
public static void main(String[] ar) {
Formulario formulario1=new Formulario();
formulario1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
formulario1.setBounds(10,20,300,200);
formulario1.setVisible(true);
}
}
C:\F>java Formulario
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JPopupMenu.add(Unknown Source)
at javax.swing.JMenu.add(Unknown Source)
at Formulario.<init>(Formulario.java:40)
at Formulario.main(Formulario.java:53)