Java Swing不会显示

时间:2014-12-04 13:40:20

标签: eclipse nullpointerexception

我收到关于nullPointerException的错误,并且我的JavaSwing项目不会显示为结果。我想我需要在第55行将employeeInfoPanel初始化为JFrams,但我不知道如何做到这一点。之后可能还有其他错误。任何信息/提示将不胜感激!

package employee;

import java.awt.*; 
import java.awt.event.*;

import javax.swing.*;

public class taxSystemDriver1 extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel homePanel, employeeInfoPanel, taxDetailsPanel, textInputPanel; 
    private JPanel ppsPanel, surnamePanel, firstnamePanel, dobPanel, salaryPanel, maritialStatusPanel,noIncomesPanel, noDependentChildrenPanel, noBIKPanel;
    private JPanel buttonPanel, buttonPanelTop, buttonPanelBottom;
    private JButton calculatorButton, recordsButton, addEmployeeButton, modifyEmployeeButton, deleteEmployeeButton, saveButton, backButton;
    private JLabel comboLabel1, ppsLabel, surnameLabel, firstnameLabel, dobLabel, salaryLabel, maritialStatusLabel, noIncomesLabel, noDependentChildrenLabel, noBIKLabel;
    private JLabel birthDayLabel, birthMonthLabel, birthYearLabel;
    private JComboBox birthDayComboBox, birthMonthComboBox, birthYearComboBox, incomeComboBox;
    private JRadioButton childrenRadioButton, maritialStatusRadioButton;
    private JButton addEmployee, deleteEmployee, modifyEmployee, displayFirst, displayNext, displayLast, displayTaxDetails;
    private JTextField ppsTF, surnameTF, firstnameTF, dobTF, salaryTF, maritialStatusTF, noIncomesTF, noDependentChildrenTF, noBIKTF;
    private JTextArea statementTextArea;
    private JScrollPane statementPane;

    public taxSystemDriver1 (String title) {
        super(title);
        initComponents();
    }

    private void initComponents() {
        setSize(300, 500);
        setLocation(100, 100);

        //construct components

        //**************************************************************************
        // HomePanel North
        //**************************************************************************

        //*********************************************
        // EmployeeInfo Panel West
        //*********************************************

        // PPS NUMBER (TextField)
        ppsPanel = new JPanel();

        ppsLabel = new JLabel ("PPS Number: ");
        ppsLabel.setHorizontalAlignment(SwingConstants.LEFT);
        ppsTF = new JTextField (5);
        ppsTF.setEditable(true);

        ppsPanel.add(ppsLabel);
        ppsPanel.add(ppsTF);

        employeeInfoPanel.add(ppsPanel);

        // SURNAME (TextField)
        surnamePanel = new JPanel();

        surnameLabel = new JLabel ("Employee Surname: ");
        surnameLabel.setHorizontalAlignment(SwingConstants.LEFT);
        surnameTF = new JTextField (5);
        surnameTF.setEditable(true);

        surnamePanel.add(surnameLabel);
        surnamePanel.add(surnameTF);

        employeeInfoPanel.add(surnamePanel);

        // FIRST NAME (TextField)
        firstnamePanel = new JPanel();

        firstnameLabel = new JLabel ("Employee First Name: ");
        firstnameLabel.setHorizontalAlignment(SwingConstants.LEFT);
        firstnameTF = new JTextField (5);
        firstnameTF.setEditable(true);

        firstnamePanel.add(firstnameLabel);
        firstnamePanel.add(firstnameTF);

        employeeInfoPanel.add(firstnamePanel);


        // DOB (ComboBox)
        dobPanel = new JPanel();

        dobPanel.add(new JLabel ("Employee Date of Birth: "));
        String[] days = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
                         "16","17","18","19","20","21","22","23","24","25","26","27","28",
                         "29","30","31"};
        birthDayComboBox = new JComboBox(days);
        dobPanel.add(birthDayComboBox);

        String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
                           "Oct", "Nov", "Dec"};
        birthMonthComboBox = new JComboBox(months);
        dobPanel.add(birthMonthComboBox);

        String[] years = {"1996", "1995", "1994", "1993", "1992", "1991", "1990", "1989", "1988", "1987", "1986", "1985", "1984",
                          "1983", "1982", "1981", "1980", "1979", "1978", "1977", "1976", "1975", "1974", "1973", "1972", "1971", "1970",
                          "1969", "1968", "1967", "1966", "1965", "1964", "1963", "1962", "1961", "1960", "1959", "1958", "1957", "1956", 
                          "1955", "1954", "1953", "1952", "1951", "1950", "1949", "1948", "1947", "1946", "1945", "1944", "1943", "1942", 
                          "1941", "1940"};
        birthYearComboBox = new JComboBox (years);
        dobPanel.add(birthYearComboBox);

        employeeInfoPanel.add(dobPanel);

        //*********************************************
        // TaxDetails Panel East
        //*********************************************

        // SALARY (TextField)
        salaryPanel = new JPanel();

        salaryLabel = new JLabel ("Employee yearly salary: €");
        salaryLabel.setHorizontalAlignment(SwingConstants.LEFT);
        salaryTF = new JTextField (5);
        salaryTF.setEditable(true);

        salaryPanel.add(salaryLabel);
        salaryPanel.add(salaryTF);

        taxDetailsPanel.add(salaryPanel);


        // MARITIAL STATUS (Radio Button)       
        final ButtonGroup maritialStatus = new ButtonGroup();
        maritialStatusPanel = new JPanel();

        maritialStatusPanel.add(new JLabel("Relationship Status: "));

        maritialStatusRadioButton = new JRadioButton ("Single", true);
        maritialStatusRadioButton.setActionCommand("Single");
        maritialStatus.add(maritialStatusRadioButton);
        maritialStatusPanel.add(maritialStatusRadioButton);

        maritialStatusRadioButton = new JRadioButton ("In unmarried Relationship");
        maritialStatusRadioButton.setActionCommand("In unmarried Relationship");
        maritialStatus.add(maritialStatusRadioButton);
        maritialStatusPanel.add(maritialStatusRadioButton);

        maritialStatusRadioButton = new JRadioButton ("Married");
        maritialStatusRadioButton.setActionCommand("Married");
        maritialStatus.add(maritialStatusRadioButton);
        maritialStatusPanel.add(maritialStatusRadioButton);

        taxDetailsPanel.add(maritialStatusRadioButton);

        // NUMBER OF INCOMES IN HOUSEHOLD (ComboBox)
        noIncomesPanel = new JPanel();

        noIncomesPanel.add(new JLabel("Number of Incomes in the Household"));
        String[] income = {"1", "2"};
        incomeComboBox = new JComboBox(income);
        noIncomesPanel.add(incomeComboBox);

        taxDetailsPanel.add(incomeComboBox);


        // NUMBER OF INDEPENDENT CHILDREN (Radio Button)
        final ButtonGroup childrenNumber = new ButtonGroup();
        noDependentChildrenPanel = new JPanel();

        noDependentChildrenPanel.add(new JLabel("Number of Dependent Children: "));

        childrenRadioButton = new JRadioButton ("0", true);
        childrenRadioButton.setActionCommand("0");
        childrenNumber.add(childrenRadioButton);
        noDependentChildrenPanel.add(childrenRadioButton);

        childrenRadioButton = new JRadioButton ("1");
        childrenRadioButton.setActionCommand("1");
        childrenNumber.add(childrenRadioButton);
        noDependentChildrenPanel.add(childrenRadioButton);

        childrenRadioButton = new JRadioButton ("2");
        childrenRadioButton.setActionCommand("2");
        childrenNumber.add(childrenRadioButton);
        noDependentChildrenPanel.add(childrenRadioButton);

        childrenRadioButton = new JRadioButton ("3");
        childrenRadioButton.setActionCommand("3");
        childrenNumber.add(childrenRadioButton);
        noDependentChildrenPanel.add(childrenRadioButton);

        childrenRadioButton = new JRadioButton ("4");
        childrenRadioButton.setActionCommand("4");
        childrenNumber.add(childrenRadioButton);
        noDependentChildrenPanel.add(childrenRadioButton);

        childrenRadioButton = new JRadioButton ("5");
        childrenRadioButton.setActionCommand("5");
        childrenNumber.add(childrenRadioButton);
        noDependentChildrenPanel.add(childrenRadioButton);


        taxDetailsPanel.add(childrenRadioButton);

        // AMOUNT OF BENEFIT IN KIND (TextField)
        noBIKPanel = new JPanel();

        noBIKLabel = new JLabel ("Amount of benefits in kind : €");
        noBIKLabel.setHorizontalAlignment(SwingConstants.LEFT);
        noBIKTF = new JTextField (5);
        noBIKTF.setEditable(true);

        noBIKPanel.add(noBIKLabel);
        noBIKPanel.add(noBIKTF);

        taxDetailsPanel.add(noBIKPanel);

        Container content = getContentPane();
        content.setLayout(new BorderLayout());

        content.add(homePanel, BorderLayout.NORTH);
        homePanel.setLayout(new BorderLayout());
        homePanel.add(employeeInfoPanel, BorderLayout.WEST);
        homePanel.add(taxDetailsPanel, BorderLayout.EAST);

        //*****************************************************
        // ButtonGroup Panel Center
        //*****************************************************
        addEmployeeButton = new JButton("Add Employee");
        modifyEmployeeButton = new JButton("Modify Employee");
        deleteEmployeeButton = new JButton("Delete Employee");
        displayFirst = new JButton("Display First Employee");
        displayNext = new JButton("Display Next Employee");
        displayLast = new JButton("Display Last Employee");
        displayTaxDetails = new JButton("Display Employee Tax Details");

        content.add(buttonPanel, BorderLayout.CENTER);
        buttonPanel.setLayout(new BorderLayout());
        buttonPanel.add(buttonPanelTop, BorderLayout.NORTH);
        buttonPanel.add(buttonPanelBottom, BorderLayout.SOUTH);

        buttonPanelTop.setLayout(new FlowLayout());
        buttonPanelTop.add(addEmployeeButton);
        buttonPanelTop.add(modifyEmployeeButton);
        buttonPanelTop.add(deleteEmployeeButton);

        buttonPanelBottom.setLayout(new FlowLayout());
        buttonPanelBottom.add(displayFirst);
        buttonPanelBottom.add(displayLast);
        buttonPanelBottom.add(displayNext);
        buttonPanelBottom.add(displayTaxDetails);


    }

        /*employeeInfoPanel = new JPanel();
        employeeInfoPanel.setLayout(new GridLayout(7, 1));


        public static void main(String[] args)
        {
            JFrame taxFrame = new taxSystemDriver1("Tax and Revenue System");
            taxFrame.setVisible(true);

1 个答案:

答案 0 :(得分:0)

您正在调用一个不存在的对象上的方法(employeeInfoPanel是一个从未在#add(...)方法调用之前构造或赋值的属性)。

初始化employeeInfoPanel,这是一个简单的默认构造:

employeeInfoPanel = new JPanel();