尝试运行JFrame时获取null point异常

时间:2014-07-25 14:02:55

标签: java swing nullpointerexception

import java.awt.*;
import javax.swing.*;




class Performance{

private JLabel l1;

private JLabel l2;

  private JLabel l3;

  private JLabel l4;

  private JLabel l5;

  private JLabel l6;

  private JLabel l7;

  private JLabel l8;

  private JLabel l9;

  private JComboBox c1;

  private JComboBox c2;

  private JComboBox c3;

  private JComboBox c4;

  private JComboBox c5;

  private JComboBox c6;

  private JComboBox c7;

  private JComboBox c8;

  private JComboBox c9;

  private String s1[] ={"S","A","B","C","D","E","U"};

  private String s2[] ={"S","A","B","C","D","E","U"};

  private String s3[] ={"S","A","B","C","D","E","U"};

  private String s4[] ={"S","A","B","C","D","E","U"};

  private String s5[] ={"S","A","B","C","D","E","U"};

  private String s6[] ={"S","A","B","C","D","E","U"};

  private String s7[] ={"S","A","B","C","D","E","U"};

  private String s8[] ={"S","A","B","C","D","E","U"};

  private String s9[] ={"S","A","B","C","D","E","U"};

  private JButton submit;

  private JFrame f;

    public void Performance(){
        f = new JFrame("CGPA calc for cse sem6");
        l1 = new JLabel("AI");
        l2 = new JLabel("PCD");
        l3 = new JLabel("OOAD");
        l4 = new JLabel("ACA");
        l5 = new JLabel("WT");
        l6 = new JLabel("KM");
        l7 = new JLabel("LAB1");
        l8 = new JLabel("LAB2");
        l9 = new JLabel("LAB3");
        c1 = new JComboBox(s1);
        c2 = new JComboBox(s2);
        c3 = new JComboBox(s3);
        c4 = new JComboBox(s4);
        c5 = new JComboBox(s5);
        c6 = new JComboBox(s6);
        c7 = new JComboBox(s7);
        c8 = new JComboBox(s8);
        c9 = new JComboBox(s9);
                submit = new JButton("Submit");
    }

    public void Lancher(){
        f.setLayout(new GridLayout(10,2,10,10));
        f.setVisible(true);
        f.setSize(300,300);
        f.add(l1);
        f.add(c1);
        f.add(l2);
        f.add(c2);
        f.add(l3);
        f.add(c3);
        f.add(l4);
        f.add(c4);
        f.add(l5);
        f.add(c5);
        f.add(l6);
        f.add(c6);
        f.add(l7);
        f.add(c7);
        f.add(l8);
        f.add(c8);
        f.add(l9);
                f.add(c9);
                f.add(submit);
    }
}

public class cse_sem6
{

public static void main(String a[]){

  Performance obj = new Performance();

  obj.Lancher();

}
}

2 个答案:

答案 0 :(得分:4)

public void Performance()

中删除空格

您需要构造函数而不是void方法。

错误是没有实例化对象的结果。

答案 1 :(得分:0)

这里的代码性能类只需用以下内容替换您的Performance类:

class Performance{

private JLabel l1;

private JLabel l2;

private JLabel l3;

private JLabel l4;

private JLabel l5;

private JLabel l6;

private JLabel l7;

private JLabel l8;

private JLabel l9;

private JComboBox c1;

private JComboBox c2;

private JComboBox c3;

private JComboBox c4;

private JComboBox c5;

private JComboBox c6;

private JComboBox c7;

private JComboBox c8;

private JComboBox c9;

private String s1[] ={"S","A","B","C","D","E","U"};

private String s2[] ={"S","A","B","C","D","E","U"};

private String s3[] ={"S","A","B","C","D","E","U"};

private String s4[] ={"S","A","B","C","D","E","U"};

private String s5[] ={"S","A","B","C","D","E","U"};

private String s6[] ={"S","A","B","C","D","E","U"};

private String s7[] ={"S","A","B","C","D","E","U"};

private String s8[] ={"S","A","B","C","D","E","U"};

private String s9[] ={"S","A","B","C","D","E","U"};

private JButton submit;

private JFrame f;

public Performance(){
    f = new JFrame("CGPA calc for cse sem6");
    l1 = new JLabel("AI");
    l2 = new JLabel("PCD");
    l3 = new JLabel("OOAD");
    l4 = new JLabel("ACA");
    l5 = new JLabel("WT");
    l6 = new JLabel("KM");
    l7 = new JLabel("LAB1");
    l8 = new JLabel("LAB2");
    l9 = new JLabel("LAB3");
    c1 = new JComboBox(s1);
    c2 = new JComboBox(s2);
    c3 = new JComboBox(s3);
    c4 = new JComboBox(s4);
    c5 = new JComboBox(s5);
    c6 = new JComboBox(s6);
    c7 = new JComboBox(s7);
    c8 = new JComboBox(s8);
    c9 = new JComboBox(s9);
            submit = new JButton("Submit");
}

public void Lancher(){
    f=new JFrame();
    f.setLayout(new GridLayout(10,2,10,10));
    f.setVisible(true);
    f.setSize(300,300);
    f.add(l1);
    f.add(c1);
    f.add(l2);
    f.add(c2);
    f.add(l3);
    f.add(c3);
    f.add(l4);
    f.add(c4);
    f.add(l5);
    f.add(c5);
    f.add(l6);
    f.add(c6);
    f.add(l7);
    f.add(c7);
    f.add(l8);
    f.add(c8);
    f.add(l9);
            f.add(c9);
            f.add(submit);
}

}