package untitled9;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
 class Untitled1 extends Frame  //extends mouseAdapter
{
private static Untitled1 frame;//  private Untitled1 frame;
 private static   boolean is;
public static void main(String[] args)
{  frame =Untitled1.frame;
     //frame = new JFrame("ddd");
     frame.addMouseListener(new MouseAdapter() {
       public void mouseEntered(MouseEvent e) {
//repaint();
         is = true;
       }       public void mouseExited(MouseEvent e) {
//repaint();
         is = false;
       }       public void paint(Graphics g) { //super.paint(g);
         if (is) {
           g.fillRect(60, 60, 60, 120);
           g.setColor(Color.red);
         }
         else {
           g.fillRect(60, 60, 180, 120);
           g.setColor(Color.red);
         }
       }     });
   
    frame.setSize(500,500);
frame.setVisible(true);
   }
}
java.lang.NullPointerException
at untitled9.Untitled1.main(Untitled1.java:16)
Exception in thread "main
有谁帮忙过去运行一下 ,看什么错误

解决方案 »

  1.   

    private static Untitled1 frame;//这行没有初始化
    我修改了下你的代码 ,现在能达到你要得动态画图效果
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.*;
     class Untitled1 extends Frame  //extends mouseAdapter
    {
    private static Untitled1 frame=new Untitled1();//  private Untitled1 frame;
     private static   boolean is=true;
     Untitled1(){
        super("ddd");
     }
    public static void main(String[] args)
    {  frame =Untitled1.frame;
         //frame = new JFrame("ddd");     frame.addMouseListener(new MouseAdapter() {
           public void mouseEntered(MouseEvent e) {
    //repaint();
             is = true;
     frame.repaint();
           }       public void mouseExited(MouseEvent e) {
    //repaint();
             is = false;
      frame.repaint();
           }       public void paint(Graphics g) { //super.paint(g);
             if (is) {
               g.fillRect(60, 60, 60, 120);
               g.setColor(Color.red);
             }
             else {
               g.fillRect(60, 60, 180, 120);
               g.setColor(Color.red);
             }
           }     });
       
        frame.setSize(500,500);
    frame.setVisible(true);
       }
       public void paint(Graphics g) { //super.paint(g);
             if (is) {
               g.fillRect(60, 60, 60, 120);
               g.setColor(Color.red);
             }
             else {
               g.fillRect(60, 60, 180, 120);
               g.setColor(Color.red);
             }
           }
    }
      

  2.   

    private static Untitled1 frame;   没有实例化,当然抛null异常了。楼上的正解