这个异常是很常见的
而且问题也一目了然,是用了一个空指针
String str=null;str.equals("str");/////////这是典型例子
解决也很简单,根据报错的地方看哪个对象是空指针一般造成的原因主要是,给一个类对象初始化为null,而之后又没有给它赋值

解决方案 »

  1.   

    你帮我看一下原程序好吗import java.awt.*;
    import java.awt.event.*;public  class Frame1 extends MouseAdapter implements ActionListener{
      Frame frame;
      TextArea msg;
      PopupMenu pm;
      MenuItem i1;
      MenuItem i2;
      MenuItem i3;  public void init(){
         frame=new Frame();
         msg=new TextArea();
         pm=new PopupMenu();
         i1=new MenuItem("复制");
         i1=new MenuItem("剪切");
         i1=new MenuItem("粘贴"); 
         pm.add(i1);
         pm.add(i2);
         pm.add(i3);
         
         msg.add(pm);
         frame.add(msg);
         i1.addActionListener(this);
         i2.addActionListener(this);
         i3.addActionListener(this);
         msg.addMouseListener(this);     frame.setTitle("弹出菜单");
         frame.setSize(350,200);
         frame.addWindowListener(new WindowAdapter(){
           public void WindowClosing(WindowEvent e){
             System.exit(0);
           }
         });
       
         frame.show();
       }   public void actionPerformed(ActionEvent e2){
         msg.append("你选择了"+e2.getActionCommand()+"\n");
       }   public void mouseRelesed(MouseEvent e1){
         if(e1.isPopupTrigger())
            pm.show(msg,e1.getX(),e1.getY());
       }  public static void main(String args[]){
        
       Frame1 f=new Frame1();
       f.init();
       //System.out.println("你好莱坞");
      }
    }
      

  2.   

    Exception in Thread "main" java.lang.NullPointerException
      

  3.   

    msg.append("你选择了"+e2.getActionCommand()+"\n");
    这里getActionCommnad()了,是不是没有set呢?
    可能是没有set的问题吧。
      

  4.   

    i1=new MenuItem("复制");
         i1=new MenuItem("剪切");
         i1=new MenuItem("粘贴");
    ->
         i1=new MenuItem("复制");
         i2=new MenuItem("剪切");
         i3=new MenuItem("粘贴");
      

  5.   

    这种异常发生在你使用了一个对象,但是这个对象是null,或者没有被创建。