package hanshunpingstudy;import java.awt.BorderLayout;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class qqFace extends JFrame {
JFrame jFrame = null;
JPanel jPanel2 = null;
JComboBox jComboBox = null;
JButton jButton = null;
JScrollPane jScrollPane = null;
JTextField jTextField = null;
JTextArea jTextArea = null; public qqFace() {
String chatter[] = { "nimam", "caocao", "daiding" };
// jFrame = new JFrame();
jPanel2 = new JPanel();
jTextArea = new JTextArea();
jScrollPane = new JScrollPane(jTextArea);
jComboBox = new JComboBox(chatter);
jTextField = new JTextField(10);
jButton = new JButton("发送");
// 添加组件
/**
 * 关键是在这部分,那就是把什么组件加到什么组件里面,
 * 要熟知什么是顶级容器,什么是一般容器
 */
// jFrame.add(jScrollPane);
// jPanel.add(jTextArea);
// jScrollPane.add(jPanel);
// jFrame.add(jPane2, BorderLayout.SOUTH);
jPanel2.add(jScrollPane);
jPanel2.add(jTextField);
jPanel2.add(jButton);
this.add(jTextArea);
this.add(jPanel2, BorderLayout.SOUTH);
/**
 * this.add(jScrollPane); this.add(jPane2, BorderLayout.SOUTH);
 * jPanel2.add(jComboBox, FlowLayout.LEFT); jPanel2.add(jTextField,
 * FlowLayout.CENTER); jPanel2.add(jButton, FlowLayout.RIGHT);
 */
// 设置大小
jFrame.setSize(100, 250);
jFrame.setIconImage(new ImageIcon("images/qq.gif").getImage());
jFrame.setTitle("QQ2011");
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true); } public static void main(String[] args) {
qqFace qqface = new qqFace();

}}Exception in thread "main" java.lang.NullPointerException
at hanshunpingstudy.qqFace.<init>(qqFace.java:52)
at hanshunpingstudy.qqFace.main(qqFace.java:61)错误行我用红色标注出来了,我写了几个GUI界面,但是Exception in thread "main" java.lang.NullPointerException这个错误老是出现,很郁闷,我看的是那个韩顺平的视频,人家写的对的,但是我抄过来怎么是错的呢?
我该怎么避免呢?为什么有的代码中这样写是对的,而有的代码中这样写就是错的呢?
我就郁闷了,烦劳各位大神解析,感激不尽啊!
对了,我对组件之间的添加还不是很熟悉其中的规则,对于什么是顶级容器一般容器,一般组件添加规则,不太熟,烦劳了,望各位大神指点。

解决方案 »

  1.   

    public class qqFace extends JFrame {
    JFrame jFrame = null;
    JPanel jPanel2 = null;
    JComboBox jComboBox = null;
    JButton jButton = null;
    JScrollPane jScrollPane = null;
    JTextField jTextField = null;
    JTextArea jTextArea = null;public qqFace() {
    String chatter[] = { "nimam", "caocao", "daiding" };
    // jFrame = new JFrame();
    jPanel2 = new JPanel();
    jTextArea = new JTextArea();
    jScrollPane = new JScrollPane(jTextArea);
    jComboBox = new JComboBox(chatter);
    jTextField = new JTextField(10);
    jButton = new JButton("发送");
    // 添加组件
    /**
    * 关键是在这部分,那就是把什么组件加到什么组件里面,
    * 要熟知什么是顶级容器,什么是一般容器
    */
    // jFrame.add(jScrollPane);
    // jPanel.add(jTextArea);
    // jScrollPane.add(jPanel);
    // jFrame.add(jPane2, BorderLayout.SOUTH);
    jPanel2.add(jScrollPane);
    jPanel2.add(jTextField);
    jPanel2.add(jButton);
    this.add(jTextArea);
    this.add(jPanel2, BorderLayout.SOUTH);
    /**
    * this.add(jScrollPane); this.add(jPane2, BorderLayout.SOUTH);
    * jPanel2.add(jComboBox, FlowLayout.LEFT); jPanel2.add(jTextField,
    * FlowLayout.CENTER); jPanel2.add(jButton, FlowLayout.RIGHT);
    */
    // 设置大小
    jFrame.setSize(100, 250);
    jFrame.setIconImage(new ImageIcon("images/qq.gif").getImage());
    jFrame.setTitle("QQ2011");
    jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    jFrame.setVisible(true);}public static void main(String[] args) {
    qqFace qqface = new qqFace();}}
      

  2.   

    为什么将
    // jFrame = new JFrame();
    注释掉,你注释掉,jFrame 就没有实例化,也就就是null,那你后面再调用
    jFrame.setSize(100, 250);
    jFrame.setIconImage(new ImageIcon("images/qq.gif").getImage());
    jFrame.setTitle("QQ2011");
    jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    jFrame.setVisible(true);
    肯定会报空指针异常了
      

  3.   

    但是我是继承了JFrame的,不声明jFrame也是可以的,所以可以不实例化吧?
      

  4.   

    public class qqFace extends JFrame {
    JFrame jFrame = null;
    这里的属性 就是给null  下面也看到构造~~
    自己说 这是不是 会报空指针 啊~·
    这个跟你继承 JFrame 没关系啊
      

  5.   

    public class qqFace extends JFrame {
    JFrame jFrame = null;
    这里的属性 就是给null 下面没看到实例化啊~~
    自己说 这是不是 会报空指针 啊~·
    这个跟你继承 JFrame 没关系啊
      

  6.   

    你继承JFrame跟声明的变量有关系吗?
    已经有几个人给你答案了,还纠结什么?
      

  7.   

    JFrame 没有实例化,只是定义了,对一个空值进行操作当然出现空指针错误了
    JFrame jFrame = new JFrame();
      

  8.   

    你把jFrame = new JFrame();注释掉了 当然不行喽,你看你前面写的
    “JFrame jFrame = null;”这是你自己写的,你后面没有实例化,就直接用了,JFrame还是null,jFrame.setSize(100, 250);这行JFrame是不能为null的,为空就会报空指针异常,就是NullPointerException
      

  9.   

    yao用 this,我找到方法了!