你有一个变量是Null,设断点好好找,这种错误不难找的

解决方案 »

  1.   

    先问一句,你学过什么语言?虽然你的程序比较乱,我还是要谢谢你,因为通过给你改程序,我又找回了我很多的java知识(因为我不是学计算机出身,我都是自学的)。不知道你的源程序是否和你贴的一样?因为错误太多了。顺便说一句,我现在用的编程工具是IntelliJ IDEA。先看看我给你改过后的程序,看看你能看出什么。
    //Aframe.javaimport java.awt.*;
    import java.awt.event.*;
    public class Aframe extends Frame {
        Button b;
        Label look1;
        TextField t1;
        Doo doo;    public Aframe() {
            b = new Button("Add");
            doo = new Doo(this);
            look1 = new Label("Look1");
            t1 = new TextField();
            doo = new Doo(this);
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    doo.join();
                    doo.getnum();
                }
            });
            this.setLayout(new GridLayout(5, 5));
            this.add(b);
            this.add(look1);
            this.add(t1);
            this.addWindowListener
                    (new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                            System.exit(0);
                        }
                    });
        }
    }class Doo {    Aframe e;
        int n;
        double num[] = new double[100];    public Doo(Aframe e) {
            n = -1;
            this.e = e;//这里是关键!!!
        }    public void join() {
            n++;
            num[n] = Double.parseDouble(e.t1.getText());
        }    public void getnum() {
            e.look1.setText(String.valueOf(num[n]));
        }
    }//A.javaimport java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class A {    public static void main(String[] args) {
            // Create application frame.        Aframe frame = new Aframe();
            frame.setSize(200, 150);        // Show frame
            frame.setVisible(true);
        }
    }你的关键错误在于:
    e=e;//倒底是怎么回事?知道this怎么用吗?改成this.e=e;就没问题了。其它的原则性错误我就不想多说了,JAVA书上要比我说得好多了。不懂可以问。
      

  2.   

    我以前只学过c和c++,没有学别的多谢这位兄弟了,其实这个程序是我从我写的东西里面为了
    问问题简化出来的,有几个地方忘了修改,不好意思啊!仔细修改了一遍已经可以正常运行了,不过我还有几个地方想请教你
    就是在下面的Doo类中能不能不做AFrame的拷贝,就是有e的地方直接
    用frame(frame在A.java中定义),不过这样编译通不过或者有什么办法能不拷贝也正常(比如说怎么加个包什么的,我不太懂)还有,你说的那个编译环境我怎么没有听说过啊,你是做什么的?最后感谢你能认真看完了我的问题并做了这么好的回答!//AFrame.javaimport java.awt.*;
    import java.awt.event.*;
    import java.util.*;
     public class AFrame extends Frame {
        Button b;
        Label look1;
        TextField t1;
        Doo doo;
        BListener bListener;
        public AFrame() {
            b=new Button("Add");
            doo=new Doo(this);    
            look1=new Label("Look1"); 
            t1=new TextField();
             
            bListener=new BListener();
            b.addActionListener(bListener);
            this.setSize(200,150);
            this.setLayout(new GridLayout(5,5));
            this.add(b);
            this.add(look1);
            this.add(t1);
            this.addWindowListener
            (
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        AFrame.this.windowClosed();
                    }
                }
            );  
        }
        public class Doo
        {
        
         AFrame e;
         int n;
         double num[]=new double[100];
         public Doo(AFrame e)
         {
         n=-1;
         this.e=e;
        
         }
         public void join()
         {
         n++;
         num[n]=Double.parseDouble(e.t1.getText());  //-----------------
         }
            public void getnum()
            {
              e.look1.setText(String.valueOf(num[n]));//-----------------------------
            }
        }
        public class BListener implements ActionListener
        {
        
         public void actionPerformed(ActionEvent e)
         {
        
           doo.join();
         doo.getnum();
         }
      
        
        }
        protected void windowClosed() {
        
        
            System.exit(0);
        }
      }//A.java   import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class A {
        
        public static void main(String[] args) {
            // Create application frame.
            
            AFrame frame = new AFrame();
            
            // Show frame
            frame.setVisible(true);
        }
    }
      

  3.   

    要想在Doo中对Aframe进行操作,那么Aframe必须是Doo的一个member。你想用frame是不可能的,就象C++中的局部变量一样。java中的包就相当于C++中的namespace。所以你的想法不太可能实现。不生成instance就能使用的是静态成员,是用static声明的。我只是个电脑爱好者,和你所学比起来差得远了。有关IntelliJ IDEA:
    http://www.jetbrains.com/idea/index.html