程序抛出NullPointerException异常不明白!

问题描述

在myFrame2中的actionPerformed方法下面加上Form1 = Application.Form1;就没问题,但是在windowOpened方法下面已经有了为什么还出现这种问题? import java.awt.*;public class j02110501_Project1  {  private static j02110501_Project1 Application;    myFrame1 Form1;  myFrame2 Form2;  myFrame3 Form3;    public static void main(String arg[])  {    Application = new j02110501_Project1();    Application.Form1 = new myFrame1( Application );    Application.Form2 = new myFrame2( Application );        Application.Form1.setVisible(true);  }}import java.awt.*;import java.awt.event.*;public class myFrame1 extends Frame  {  private j02110501_Project1 Application;  private myFrame1 Form1;  private myFrame2 Form2;  private myFrame3 Form3;    Button B1, B2;  Label L1, L2;  public myFrame1(j02110501_Project1 app)  {    Application = app;    Form1 = this;        this.setLayout(null);    this.setBounds(100,300,265,200);    this.setTitle("Form1:主窗体");    this.addWindowListener(new myWindowAdapter() );    B1 = new Button("Show Form2");    B1.setBounds(60,30,140,30);    B1.addActionListener(new myActionListener() );    this.add(B1);    L1 = new Label("0",Label.RIGHT);    L1.setBounds(60,70,140,30);    L1.setBackground(Color.PINK);    this.add(L1);    B2 = new Button("Show Form3");    B2.setBounds(60,120,140,30);    B2.addActionListener(new myActionListener());    this.add(B2);    L2 = new Label("0",Label.RIGHT);    L2.setBounds(60,160,140,30);    L2.setBackground(Color.PINK);    this.add(L2);  }    class myWindowAdapter extends WindowAdapter  {    public void windowOpened(WindowEvent e)  {      //此时Application所有静态建立的窗体都已建立完成      //取得Form1所属项目的其他窗体      Form2 = Application.Form2;    }    public void windowClosing(WindowEvent e)  {      System.exit(0);    }  }    class myActionListener implements ActionListener  {    public void actionPerformed(ActionEvent e)  {      if(e.getSource() == B1)  {        Form2.setVisible(true);      }      else if(e.getSource()==B2)  {        if(Form3 == null)  {          Application.Form3 = new myFrame3(Application);          Form3 = Application.Form3;          System.out.println("Create Form");        }        else          System.out.println("Show Form");                Form3.setVisible(true);      }    }  }}import java.awt.*;import java.awt.event.*;import javax.swing.*;public class myFrame2 extends Frame  {  private j02110501_Project1 Application;  private myFrame1 Form1;  private myFrame2 Form2;  private myFrame3 Form3;    TextField text1,text2;  Label lb1;  Button B1;  public myFrame2( j02110501_Project1 app)  {    Application = app;    Form2 = this;        this.setLayout(null);    this.setBounds(420,300,300,90);    this.setTitle("Foem2: 加法运算");    this.addWindowListener(new myWindowAdapter());        text1 = new TextField("0",6);    text1.setBounds(30,40,80,30);    this.add(text1);    lb1 = new Label("+");    lb1.setBounds(115,40,20,30);    this.add(lb1);    text2 = new TextField("0",6);    text2.setBounds(150,40,80,30);    this.add(text2);    B1 = new Button("计算");    B1.setBounds(240,40,40,30);    B1.addActionListener(new myActionListener());    this.add(B1);  }    class myActionListener implements ActionListener  {      public void actionPerformed(ActionEvent e)  {      //Form1 = Application.Form1;      double sum;      try  {        sum = Double.parseDouble( Form2.text1.getText().trim())            + Double.parseDouble(Form2.text2.getText().trim());                Form1.L1.setText(String.valueOf(sum));        if(Form3 != null)  {          System.out.println("called by Form2:n Form3.text1 = "                      + Form3.text1.getText()                      + " Form3.text2 = "                      + Form3.text2.getText() );        }      }      catch(NumberFormatException ecp)  {        JOptionPane.showMessageDialog(null,"必须输入两个“数值”!");      }    }  }    class myWindowAdapter extends WindowAdapter  {    public void windowOpend(WindowEvent e)  {      Form1 = Application.Form1;      Form3 = Application.Form3;    }        public void windowActivated(WindowEvent e)  {      if(Form3 == null)        Form3 = Application.Form3;    }    public void windowClosing(WindowEvent e)  {      Form2.setVisible(false);    }  }}import java.awt.*;import java.awt.event.*;import javax.swing.*;public class myFrame3 extends Frame  {  private j02110501_Project1 Application;  private myFrame1 Form1;  private myFrame2 Form2;  private myFrame3 Form3;    TextField text1,text2;  Label lb1;  Button B1;  public myFrame3(j02110501_Project1 app)  {    Application = app;    Form3 = this;        this.setLayout(null);    this.setBounds(420,410,300,90);    this.setTitle("Foem3: 乘法运算");    this.addWindowListener(new myWindowAdapter());        text1 = new TextField("1",6);    text1.setBounds(30,40,80,30);    this.add(text1);    lb1 = new Label("*");    lb1.setBounds(115,40,20,30);    this.add(lb1);    text2 = new TextField("1",6);    text2.setBounds(150,40,80,30);    this.add(text2);    B1 = new Button("计算");    B1.setBounds(240,40,40,30);    B1.addActionListener(new myActionListener());    this.add(B1);  }    class myActionListener implements ActionListener  {    public void actionPerformed(ActionEvent e)  {      double multiplication;      try  {        multiplication =              Double.parseDouble(Form3.text1.getText().trim())             * Double.parseDouble(Form3.text2.getText().trim());        Form1.L2.setText(String.valueOf(multiplication));        System.out.println("called by Form3:n Form2.text1 = "                                + Form2.text1.getText()                                + " Form2.text2 = "                                + Form2.text2.getText() );      }      catch(NumberFormatException ecp)  {        JOptionPane.showMessageDialog(null,"必须输入两个“数值”!");      }    }  }    class myWindowAdapter extends WindowAdapter  {    public void windowOpened(WindowEvent e)  {      Form1 = Application.Form1;      Form2 = Application.Form2;    }    public void windowClosing(WindowEvent e)  {      Form3.setVisible(false);    }  }}错误信息:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException        at myFrame2$myActionListener.actionPerformed(myFrame2.java:46)        at java.awt.Button.processActionEvent(Unknown Source)        at java.awt.Button.processEvent(Unknown Source)        at java.awt.Component.dispatchEventImpl(Unknown Source)        at java.awt.Component.dispatchEvent(Unknown Source)        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)        at java.awt.EventQueue.access$000(Unknown Source)        at java.awt.EventQueue$1.run(Unknown Source)        at java.awt.EventQueue$1.run(Unknown Source)        at java.security.AccessController.doPrivileged(Native Method)        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)        at java.awt.EventQueue$2.run(Unknown Source)        at java.awt.EventQueue$2.run(Unknown Source)        at java.security.AccessController.doPrivileged(Native Method)        at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)        at java.awt.EventQueue.dispatchEvent(Unknown Source)        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)        at java.awt.EventDispatchThread.run(Unknown Source)

解决方案

引用在myFrame2中的actionPerformed方法下面加上Form1 = Application.Form1;就没问题,但是在windowOpened方法下面已经有了为什么还出现这种问题?这个我运行了。问题出在myFrame2中的全局变量 private myFrame1 Form1; 它是你的主窗体没有被实例化。在myFrame2你应该让他指向j02110501_Project1 中已经实例化的主窗体。在这里就是你的Application.Form1。一个对象在没有被实例化之前,对象应用是不能操作对象的方法的。会报Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 也就是你的对象引用指向了空指针。在这里Form1 指向了空指针。所以报错。你让它指向Application.Form1就不会报错了。
解决方案二:
windowOpened不是windowOpend
解决方案三:
Form1.L1.setText(String.valueOf(sum)); Form1是null,或者L1是null
解决方案四:
myFrame2.java:46
解决方案五:
空指针,有对像没有实例化啊

时间: 2024-11-01 05:38:17

程序抛出NullPointerException异常不明白!的相关文章

java-JavaCompiler中run方法运行时抛出NullPointerException异常

问题描述 JavaCompiler中run方法运行时抛出NullPointerException异常 解决方案 解决方案二: 解决方案三: 参考:http://blog.csdn.net/liu578182160/article/details/25103905

子类方法返回类型必须和父类相同,抛出的异常声明可以小于或等于父类

Overload译为重载:Override译为重写或者覆盖:  Overload讨论: Java中同一个类不可以有两个相同的方法(方法名.参数类型.参数个数和参数位置都  相同).但可以有方法名相同,参数不同(参数类型.参数个数和参数位置不相同)的方法.这  种相同的方法名,参数不同的方法称为重载. public class Test { public void fn(String name) {} public void fn(int age) {} public void fn(String

SRVE0068E: 未捕获到 servlet CXFServlet 的其中一个服务方法中抛出的异常。抛出的异常:java.lang.IncompatibleClassChangeError

问题描述 RT.[08-10-2516:37:40:421CST]00000030ServletWrappeESRVE0068E:未捕获到servletCXFServlet的其中一个服务方法中抛出的异常.抛出的异常:java.lang.IncompatibleClassChangeErroratorg.apache.cxf.wsdl11.ServiceWSDLBuilder.addExtensibiltyElements(ServiceWSDLBuilder.java:227)atorg.apa

HibernateTemplate类里面的flush方法抛出了异常,异常处理

问题描述 图1图2图1中,HibernateTemplate类里面的flush方法抛出了异常,图2中,调用它的时候却不会抛出异常各位大神,请问下这是做了什么处理啊? 解决方案 解决方案二:异常分为两种:一种是检查式异常就是我们写程序时必须try,catch,比如IOException另一种就是运行时异常,是可以不捕获的比如NullPonterException而DataAccessException就是RuntimeExcption的子类,也就是运行时异常!解决方案三:图2中调用的就是图1的fl

缺省构造函数不能处理隐式超构造函数抛出的异常类型 IOException。必须定义显式构造函数

问题描述 缺省构造函数不能处理隐式超构造函数抛出的异常类型 IOException.必须定义显式构造函数 int lastnum = getNum(source.getProperty(""fileName"")); public static int getNum(String Filename) throws IOException { InputStream myxls; myxls = new FileInputStream(Filename); sr =

[WPF]如何在关闭非模态子窗体时用消息框确认:解决最小化窗体时抛出的异常

又是一个看起来很简单的问题.像下面这样在Closing里弹出个MessageBox确认一下不就行了? public static void OnWindowClosing(object sender, CancelEventArgs e) { if (MessageBox.Show(string.Format("Are you sure to close the {0}?", (sender as Window).Title), "Confirm", Message

ssh2在tomcat启动时抛出的异常,无解

问题描述 ssh2在tomcat启动时抛出的异常,无解 Mar 16, 2015 3:22:39 PM org.apache.catalina.core.StandardContext listenerStart SEVERE: Error configuring application listener of class org.springframework.web.util.Log4jConfigListener java.lang.ClassNotFoundException: org.

使用richface4,如何捕获后台抛出的异常?

问题描述 我最近想做一个项目,用的是richface4+jsf2我不知道如何捕获后台抛出的异常,并且展示在页面上比如使用a4j:jsfunction调用action中的test();方法这个test()方法throw一个exception做测试页面如何用js捕获并且alert出来?我查了有一个A4J.AJAX.OnErrer,但是一直报A4J未定义,求解答~

在java中,下面画横线抛出的异常必须一样吗?谢谢!,,,,,

问题描述 在java中,下面画横线抛出的异常必须一样吗?谢谢!,,,,, 解决方案 不需要,throws 后面抛出整个方法中所有可能出现的异常(抛出的是异常类),而throw new Exception抛出特定异常(抛出的是异常对象), 解决方案二: 看情况.完全的解释是,Exception 异常,是所有异常的父类,所有异常都继承于它,所以上面throws 关键字捕获的异常如果写成Exception,那么不管方法里抛出任何种类的异常,都能捕获到,而不需要再捕获特定类型的异常.而如果throws关