Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

awt Swing项目添加鼠标监听事件时出现异常

Exception in thread

Exception in thread

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at day02.LoginFrame$MyActionListener.actionPerformed(LoginFrame.java:134)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)

原因:

clientContext不是一个对象(未被实例化),故而clientContext.login()调用时出现空指针

临时解决:

    ClientContext clientContext = new ClientContext();

正确解决:

    依赖注入 

 

①Jframe变量没有赋值而你直接调用它而产生exception

   JButton没有实例化就添加了按钮监听事件,导致了空指针异常

②null对象访问它的属性,就会出现java.lang.NullPointerException

 

 

总结注意点:

①依赖注入

②MVC

你可能感兴趣的:(学习笔记)