我最近编了一个窗口程序,添加了一个菜单条,代码如下
import javax.swing.*;
public class Jishibenmain extends JFrame{
JMenuBar menub;
JMenu file1=new JMenu("文件");
JMenu edit=new JMenu("编辑");
JMenuItem newfile;
JMenuItem openfile;
JMenuItem savefile;
JMenuItem exitfile;
JMenuItem font;
JMenuItem searchchar;
JMenuItem replacechar;    public Jishibenmain() {
     super("记事本");
     newfile=new JMenuItem("新建");
     openfile=new JMenuItem("打开");
     savefile=new JMenuItem("保存");
     exitfile=new JMenuItem("退出");
     font=new JMenuItem("字体");
     searchchar=new JMenuItem("查找···");
     replacechar=new JMenuItem("替换···");
     setJMenuBar(menub);
     menub.add(file1);//1
     menub.add(edit);//2
     file1.add(newfile);
     file1.add(openfile);
     file1.add(savefile);
     file1.addSeparator();
     file1.add(exitfile);
     edit.add(font);
     edit.add(searchchar);
     edit.add(replacechar);//菜单编辑
     setBounds(40,40,300,200);
     setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    }
    public static void main (String[] args) {
     Jishibenmain j=new Jishibenmain();
}
    
}
不过在向菜单条中添加菜单时出现运行时错误,就是在1,2 两个地方,给出的错误类型是
Exception in thread "main" java.lang.NullPointerException
其大家帮我看看这是怎么回事

解决方案 »

  1.   

    public class Jishibenmain extends JFrame{ 
    JMenuBar menub = new JMenuBar();   //这个地方要实例化
    JMenu file1=new JMenu("文件"); 
    JMenu edit=new JMenu("编辑"); 这样好象就没问题了!
      

  2.   

    public class Jishibenmain extends JFrame{ 
    JMenuBar menub =new JMenuBar();//这个地方要实例化
    JMenu file1=new JMenu("文件"); 
    JMenu edit=new JMenu("编辑"); 
      

  3.   

    file1.addSeparator(); 
    这是添加分割线的吗???
      

  4.   

    JMenuBar menub = new JMenuBar();
      

  5.   

    JMenuBar menub=new JMenuBar(); 必须要有这个 m1.addSeparator(); 是加分割线,想在哪个后面加就在如 m1.add(m11); 后加这句话