close.addActionListener(new ActionListener(){       //对关门进行监听
     public void actionPerformed(ActionEvent e){
         display(); 
         try {
         
statement.execute("insert into info values(1,1);");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} 为什么总是显示java.lang.NullPointerException?
前面我已经定义好了 数据库的。

解决方案 »

  1.   

    声明只是定义了一个引用,并没有分配内存。
    public class T {
        public static void main(String args[]){
            int[] a0 = new int[5];
            a0[3]=3;//因为是原始类型,所以会自动分配内存空间
            System.out.println(a0[3]);
            Integer[] a1 = new Integer[5];
            a1[3]=3;//因为是原始类型的打包,所以会自动分配内存空间
            System.out.println(a1[3]);
            V[] a2 = new V[5];
            a2[3].v =3;//!!!因为是自定义类,所以不会自动分配内存空间,异常在此抛出
            System.out.println(a2[3].v);
        }
    }
    class V{
        int v = 0;
    }
    运行结果:
    3
    3
    Exception in thread "main" java.lang.NullPointerException
            at Chapter2.T.main(T.java:21)
    Java Result: 1
      

  2.   

    NullpointException发生在对象的话一般是没有进行实例化,我觉得有可能是你的statemetn变量没有进行实例化,所以在执行的时候才会发生空值异常