NullPointerException同时引用另一个类中的复选框?

时间:2016-06-07 15:21:36

标签: java nullpointerexception jcheckbox

为什么在引用其他类中的复选框时会出现空指针异常?这是因为我必须使CheckBox以图形方式显示,以使它们不是null吗?

这是错误:

  

线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException

     

在   com.company.Output.Constraint.removePersonWithConstraint1(Constraint.java:125)

和第125行位于System.out.println(inputChange.wk1.isSelected());

中的removePersonWithConstraint1()
package com.company.Output;

import com.company.Input.Input;
import com.company.Input.InputChange;
import com.company.Input.InputMain;

import java.time.Month;
import java.time.Year;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by paull on 6/6/2016.
 */
public class Constraint {

    Input input;
    DateRange dateRange;
    Output output;
    InputMain inputMain;
    InputChange inputChange;

    Calendar calendar;

    ArrayList<String> listOfFirstSundays;
    ArrayList<String> listOfSecondSundays;
    ArrayList<String> listOfThirdSundays;
    ArrayList<String> listOfFourthSundays;
    ArrayList<String> listOfFifthSundays;

    public Constraint(Input input, DateRange dateRange,Output output,InputMain inputMain,InputChange inputChange) {
        this.input = input;
        this.dateRange = dateRange;
        this.output = output;
        this.inputMain = inputMain;
        this.inputChange = inputChange;

        listOfFirstSundays = new ArrayList<String>();
        listOfSecondSundays = new ArrayList<String>();
        listOfThirdSundays = new ArrayList<String>();
        listOfFourthSundays = new ArrayList<String>();
        listOfFifthSundays = new ArrayList<String>();

        calendar = Calendar.getInstance();
    }


    public void constraint1(int year, int month) {
        calendar.set(year,month - 1,1);
        int monthDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        int monthlySundayCounter = 0;

        String ithSunday = "";

        for (int i = 1; i <= monthDays; i++) {
            calendar.set(year,month - 1,i);
            int day = calendar.get(Calendar.DAY_OF_WEEK);

            String dayString = year + "/" + month + "/" + i; //setup the daystring
            //System.out.println(dayString);
            if (day == Calendar.SUNDAY) {
                monthlySundayCounter++;//count which sunday it is in a month

                //System.out.println(year + "/" + month + "/" + i + " is the " + monthlySundayCounter + " th Sunday."); //this line works

                for (int j = 0; j < output.dtm.getRowCount(); j++) {
                    //test
                    //System.out.println(output.tableResult.getValueAt(j,0));
                    //System.out.println("dayString = " + dayString);
                    //System.out.println("output at " + j + "row and 0 column = " + output.tableResult.getValueAt(j,0));
                    //System.out.println(output.tableResult.getValueAt(j,0).equals(dayString));

                    if (output.tableResult.getValueAt(j,0).equals(dayString)) {
                        //System.out.println("this works");
                        switch (monthlySundayCounter) {
                            case 1:
                                listOfFirstSundays.add(dayString);
                                //System.out.println(dayString + " is the 1st Sunday.");
                                break;
                            case 2:
                                listOfSecondSundays.add(dayString);
                                //System.out.println(dayString + " is the 2nd Sunday.");
                                break;
                            case 3:
                                listOfThirdSundays.add(dayString);
                                //System.out.println(dayString + " is the 3rd Sunday.");
                                break;
                            case 4:
                                listOfFourthSundays.add(dayString);
                                //System.out.println(dayString + " is the 4th Sunday.");
                                break;
                            case 5:
                                listOfFifthSundays.add(dayString);
                                //System.out.println(dayString + " is the 5th Sunday.");
                                break;
                        }
                    }
                }
            }
        }
    }

    public void removePersonWithConstraint1() {
        //for each person within the inputmain table
        //1. setCheckboxBasedOnConstraintText()
        //if (checkbox is checked for nth Sunday), then {delete him from every list (list of sat, list of eight, etc.) for that Sunday}
        for (int i = 0; i < inputMain.model.getRowCount(); i++) { //for each row within the inputmain table
            //reset all the checkboxes

            System.out.println(inputChange.wk1.isSelected());
            System.out.println(inputChange.wk2.isSelected());
            System.out.println(inputChange.wk3.isSelected());
            System.out.println(inputChange.wk4.isSelected());
            System.out.println(inputChange.wk5.isSelected());



            inputChange.wk1.setSelected(false);
            inputChange.wk2.setSelected(false);
            inputChange.wk3.setSelected(false);
            inputChange.wk4.setSelected(false);
            inputChange.wk5.setSelected(false);


            inputMain.setCheckboxBasedOnConstraintText(i); //set the checkbox based on the constraint text

            System.out.println(inputChange.wk1.isSelected());
            System.out.println(inputChange.wk2.isSelected());
            System.out.println(inputChange.wk3.isSelected());
            System.out.println(inputChange.wk4.isSelected());
            System.out.println(inputChange.wk5.isSelected());

            String name = inputMain.table.getValueAt(i,0).toString();//get the name of that person from inputmain
            System.out.println(name);

            if (inputChange.wk1.isSelected()) { //if the checkbox is checked
                System.out.println(inputChange.wk1.isSelected());
                for (int j = 0; j < output.dtm.getRowCount(); j++) {
                    for (String lofs : listOfFirstSundays) { //for every sunday within listoffirstsunday, if it equals to a sunday in the output tableresult
                        if (lofs.equals(output.tableResult.getValueAt(j, 0).toString())) {
                            System.out.println(lofs);
                            System.out.println(output.tableResult.getValueAt(j,0).toString());
                            //remove the person who chose that constraint
                            output.tableResult.getValueAt(j,0).toString().replaceAll((name),""); //(name) is a regular expression
                        }
                    }
                }
            }
                if (inputChange.wk2.isSelected()) { //if the checkbox is checked
                    for (int j = 0; j < output.dtm.getRowCount(); j++) {
                        for (String lofs : listOfSecondSundays) { //for every sunday within listoffirstsunday, if it equals to a sunday in the output tableresult
                            if (lofs.equals(output.tableResult.getValueAt(j, 0).toString())) {
                                System.out.println(lofs);
                                System.out.println(output.tableResult.getValueAt(j,0).toString());
                                //remove the person who chose that constraint
                                output.tableResult.getValueAt(j,0).toString().replaceAll((name),""); //(name) is a regular expression
                            }
                        }
                    }
                }
                if (inputChange.wk3.isSelected()) { //if the checkbox is checked
                    for (int j = 0; j < output.dtm.getRowCount(); j++) {
                        for (String lofs : listOfThirdSundays) { //for every sunday within listoffirstsunday, if it equals to a sunday in the output tableresult
                            if (lofs.equals(output.tableResult.getValueAt(j, 0).toString())) {
                                System.out.println(lofs);
                                System.out.println(output.tableResult.getValueAt(j,0).toString());
                                //remove the person who chose that constraint
                                output.tableResult.getValueAt(j,0).toString().replaceAll((name),""); //(name) is a regular expression
                            }
                        }
                    }
                }
                if (inputChange.wk4.isSelected())  { //if the checkbox is checked
                    for (int j = 0; j < output.dtm.getRowCount(); j++) {
                        for (String lofs : listOfFourthSundays) { //for every sunday within listoffirstsunday, if it equals to a sunday in the output tableresult
                            if (lofs.equals(output.tableResult.getValueAt(j, 0).toString())) {
                                System.out.println(lofs);
                                System.out.println(output.tableResult.getValueAt(j,0).toString());
                                //remove the person who chose that constraint
                                output.tableResult.getValueAt(j,0).toString().replaceAll((name),""); //(name) is a regular expression
                            }
                        }
                    }
                }
                if (inputChange.wk5.isSelected()) { //if the checkbox is checked
                    for (int j = 0; j < output.dtm.getRowCount(); j++) {
                        for (String lofs : listOfFifthSundays) { //for every sunday within listoffirstsunday, if it equals to a sunday in the output tableresult
                            if (lofs.equals(output.tableResult.getValueAt(j, 0).toString())) {
                                System.out.println(lofs);
                                System.out.println(output.tableResult.getValueAt(j,0).toString());
                                //remove the person who chose that constraint
                                output.tableResult.getValueAt(j,0).toString().replaceAll((name),""); //(name) is a regular expression
                            }
                        }
                    }
                }

        }
    }


}

按要求输入InputChange。我认为复选框已初始化。

    package com.company.Input;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by paull on 6/6/2016.
 */
public class InputChange {
    JLabel nameLabel;
    JLabel optionLabel;
    JLabel constraintLabel;
    JButton enter;
    JPanel labelPanel;
    JPanel jp2;
    JPanel jp3;
    JPanel jp4;
    JTextField nameText;
    String[] optionStrings;
    JComboBox<String> optionText;
    public JCheckBox wk1;
    public JCheckBox wk2;
    public JCheckBox wk3;
    public JCheckBox wk4;
    public JCheckBox wk5;

    String constraintText;

    JPanel checkboxPanel;

    private InputMain im;


    public InputChange(InputMain inputMain) {
        this.im = inputMain;
        nameLabel = new JLabel("");
        optionLabel = new JLabel("");
        constraintLabel = new JLabel("");
        enter = new JButton("");
        nameText = new JTextField(10);
        Options c = new Options();
        optionStrings = new String[]{c.satNight,c.sunEight,c.sunNineThirty,c.sunEleven,c.sunNight};
        optionText = new JComboBox<String>(optionStrings);
        wk1 = new JCheckBox("");
        wk2 = new JCheckBox("");
        wk3 = new JCheckBox("");
        wk4 = new JCheckBox("");
        wk5 = new JCheckBox("");
        constraintText = new String();
        checkboxPanel = new JPanel(new FlowLayout(10,30,10));


        int fontSize = 50;
        nameLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        optionLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        constraintLabel.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        enter.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        nameText.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        optionText.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk1.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk2.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk3.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk4.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        wk5.setFont(new Font(nameLabel.getName(),Font.PLAIN,fontSize));
        //Image ticked = Toolkit.getDefaultToolkit().getImage("Ticked.png");
        //Image unticked = ImageIO.s("Unticked.png");
        ImageIcon unticked = new ImageIcon(getClass().getResource("Unticked.png"));
        Image utimg = unticked.getImage();
        //Icon tickedIcon = new ImageIcon(ticked.getScaledInstance(100,100, Image.SCALE_DEFAULT));
        Icon untickedIcon = new ImageIcon(utimg.getScaledInstance(50,50, Image.SCALE_DEFAULT));
        ImageIcon ticked = new ImageIcon(getClass().getResource("Ticked.png"));
        Image timg = ticked.getImage();
        Icon tickedIcon = new ImageIcon(timg.getScaledInstance(50,50,Image.SCALE_DEFAULT));
        wk1.setIcon(untickedIcon);
        wk2.setIcon(untickedIcon);
        wk3.setIcon(untickedIcon);
        wk4.setIcon(untickedIcon);
        wk5.setIcon(untickedIcon);
        wk1.setSelectedIcon(tickedIcon);
        wk2.setSelectedIcon(tickedIcon);
        wk3.setSelectedIcon(tickedIcon);
        wk4.setSelectedIcon(tickedIcon);
        wk5.setSelectedIcon(tickedIcon);

        checkboxPanel.add(wk1);
        checkboxPanel.add(wk2);
        checkboxPanel.add(wk3);
        checkboxPanel.add(wk4);
        checkboxPanel.add(wk5);

        JPanel namePanel = new JPanel(new FlowLayout(10,30,10));
        namePanel.add(nameLabel);
        namePanel.add(nameText);

        JPanel optionPanel = new JPanel(new FlowLayout(10,30,10));
        optionPanel.add(optionLabel);
        optionPanel.add(optionText);

        JPanel constraintPanel = new JPanel(new FlowLayout(10,30,0));
        constraintPanel.add(constraintLabel);
        constraintPanel.add(checkboxPanel);


        labelPanel = new JPanel();
        labelPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;//Start everythin from the top
        gbc.anchor = GridBagConstraints.CENTER;//place everything in the center

        gbc.weightx = 0.25;
        gbc.weighty = 0.5;
        gbc.gridx = 0;
        gbc.gridy = 0;
        labelPanel.add(nameLabel,gbc);

        gbc.weighty = 0.5;
        gbc.gridx = 1;
        gbc.gridy = 0;
        labelPanel.add(nameText,gbc);

        gbc.weighty = 0.5;
        gbc.gridx = 0;
        gbc.gridy = 1;
        labelPanel.add(optionLabel,gbc);

        gbc.weighty = 0.5;
        gbc.gridx = 1;
        gbc.gridy = 1;
        labelPanel.add(optionText,gbc);

        gbc.weightx = 10;
        gbc.weighty = 0.5;
        gbc.gridx = 0;
        gbc.gridy = 2;
        labelPanel.add(constraintLabel,gbc);

        gbc.weightx = 1.5;
        gbc.weighty = 0.5;
        gbc.gridx = 1;
        gbc.gridy = 2;
        labelPanel.add(checkboxPanel,gbc);

        gbc.weighty = 0.5;
        gbc.gridx = 1;
        gbc.gridy = 3;
        labelPanel.add(enter,gbc);


//        InputMain i = new InputMain();
        //inputMain.table.setModel(new DefaultTableModel(null,inputMain.columns));//i.data,i.columns --> defaulttablemodel is defined in inputMain.java
        DefaultTableModel model = (DefaultTableModel) inputMain.table.getModel();

        enter.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setConstraintText();
                String[] data = new String[]{nameText.getText(),optionText.getSelectedItem().toString(),constraintText};
                model.addRow(data);

                nameText.setText("");
                wk1.setSelected(false);//To clear out the previous checkboxes
                wk2.setSelected(false);
                wk3.setSelected(false);
                wk4.setSelected(false);
                wk5.setSelected(false);

            }
        });
}

这是我创建约束实例的地方,并调用了constranint:

package com.company.Output;

//import com.company.Input.InputMain;

import com.company.Input.Input;
import com.company.Input.InputChange;
import com.company.Input.InputMain;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;


/**
 * Created by paull on 29/5/2016.
 */
public class DateRange {

    JLabel topic;
    JLabel year1;
    JLabel month1;
    JLabel day1;
    JLabel till;
    JLabel year2;
    JLabel month2;
    JLabel day2;
    JTextField beginYear;
    JTextField beginMonth;
    JTextField beginDay;
    JTextField endYear;
    JTextField endMonth;
    JTextField endDay;
    JButton confirm;
    JPanel words;
    public JPanel pane;

    Calendar calendar;

    int beginYearInt;
    int beginMonthInt;
    int beginDayInt;

    int endYearInt;
    int endMonthInt;
    int endDayInt;

    Output op;
    private InputMain im;
    Input input;
    InputChange inputChange;

    Constraint constraint;

    public DateRange(InputMain im, Input input, InputChange inputChange) {
        topic = new JLabel("");
        year1 = new JLabel("");
        month1 = new JLabel("");
        day1 = new JLabel("");
        till = new JLabel("");
        year2 = new JLabel("");
        month2 = new JLabel("");
        day2 = new JLabel("");


        beginYear = new JTextField(3);
        beginMonth = new JTextField(2);
        beginDay = new JTextField(2);

        endYear = new JTextField(3);
        endMonth = new JTextField(2);
        endDay = new JTextField(2);

        confirm = new JButton("");

        words = new JPanel(new FlowLayout(3,10,3));
        pane = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.anchor = GridBagConstraints.CENTER;

        gbc.weighty = 1;//vertical axis ratio
        gbc.gridx = 0;//which goes first starts from left;integer
        gbc.gridy = 0;//which goes first starts from top;integer
        pane.add(topic,gbc);

        gbc.weighty = 1;//vertical axis ratio
        gbc.gridx = 0;//which goes first starts from left;integer
        gbc.gridy = 1;//which goes first starts from top;integer
        words.add(beginYear);
        words.add(year1);
        words.add(beginMonth);
        words.add(month1);
        words.add(beginDay);
        words.add(day1);
        words.add(till);
        words.add(endYear);
        words.add(year2);
        words.add(endMonth);
        words.add(month2);
        words.add(endDay);
        words.add(day2);
        pane.add(words,gbc);

        gbc.weighty = 1;//vertical axis ratio
        gbc.gridx = 0;//which goes first starts from left;integer
        gbc.gridy = 2;//which goes first starts from top;integer
        pane.add(confirm,gbc);

        int fontSize = 50;
        topic.setFont(new Font(topic.getName(),Font.PLAIN,fontSize));
        words.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        beginYear.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        year1.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        year2.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        beginMonth.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        month1.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        month2.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        beginDay.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        day1.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        day2.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        till.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        endYear.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        endMonth.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        endDay.setFont(new Font(words.getName(),Font.PLAIN,fontSize));
        confirm.setFont(new Font(confirm.getName(),Font.PLAIN,fontSize));


        calendar = Calendar.getInstance();

        //addColumnConfirmListener(2010,6,1);
        //datesOfSundaysInTheSameMonth();
        confirmActionListener();
        //datesOfSundaysInTheBeginningMonth();
        //datesOfSundaysInTheEndingMonth();

        this.im = im;
        //im = new InputMain();
        op = new Output(im);

        this.input = input;
        constraint = new Constraint(input,this,op,im,inputChange);
    }



    public void datesUsingIndicator() {
        //parse those integers immediately after clicking the confirm button
        beginYearInt = parseIntWithException(beginYear,-1);//Integer.parseInt(beginYear.getText()) ;
        beginMonthInt = parseIntWithException(beginMonth,-1);//Integer.parseInt(beginMonth.getText());
        beginDayInt = parseIntWithException(beginDay,-1);//Integer.parseInt(beginDay.getText());

        endYearInt = parseIntWithException(endYear,-1);//Integer.parseInt(endYear.getText());
        endMonthInt = parseIntWithException(endMonth,-1);//Integer.parseInt(endMonth.getText());
        endDayInt = parseIntWithException(endDay,-1);//Integer.parseInt(endDay.getText());

        int noOfMonths = 0;
        if (beginYearInt == endYearInt) { // for the case of same year
            //THIS PART WORKS PERFECT
            for (int i = beginMonthInt; i <= endMonthInt; i++) {
                noOfMonths++;
            }
            if (noOfMonths >= 3) {
                datesOfSundaysInTheBeginningMonth();
                for (int i = beginMonthInt + 1; i < endMonthInt; i++) {
                    datesOfSundaysInTheMiddleMonths(endYearInt,i);
                }
                datesOfSundaysInTheEndingMonth();
            }
            if (noOfMonths == 2) {
                datesOfSundaysInTheBeginningMonth();
                datesOfSundaysInTheEndingMonth();
            }
            if (noOfMonths == 1) {
                datesOfSundaysInTheSameMonth();
            }
        }
        if (beginYearInt != endYearInt){ //for the case of different year
            int noOfYears = 0;
            for (int i = beginYearInt; i <= endYearInt; i++) {
                noOfYears++;
            }
            //calcualtion of sundays start from here:
            datesOfSundaysInTheBeginningMonth(); //Till the end of first month
            int beginyrmonthcounter = beginMonthInt + 1; // starts from the second month
            while (beginyrmonthcounter <= 12) {
                datesOfSundaysInTheMiddleMonths(beginYearInt,beginyrmonthcounter); //2nd month till December
                beginyrmonthcounter++;
            } //first year ended

            if (noOfYears >= 3) {//this case only happens if number of years is larger than or equal to 3
                for (int i = beginYearInt + 1; i <= endYearInt - 1; i++) { //second year till the second last year
                    for (int j = 1; j <= 12; j++) { //from jan till december
                        datesOfSundaysInTheMiddleMonths(i, j);
                    }
                }
            }

            int endyrmonthcounter = 1;
            while (endyrmonthcounter < endMonthInt) { //from january of last year till the month before endmonth
                datesOfSundaysInTheMiddleMonths(endYearInt,endyrmonthcounter);
                endyrmonthcounter++;
            }
            datesOfSundaysInTheEndingMonth();
        }
    }


    public void confirmActionListener() { //just enter the actual month.  The functions already -1ed the month for calendar

        confirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //create an output frame

                OutputFrame of = new OutputFrame();
                of.add(op.outPanel);

                op.topic3.setText(beginYear.getText() + "/" + beginMonth.getText() + "/" + beginDay.getText() + " - "
                + endYear.getText() + "/" + endMonth.getText() + "/" + endDay.getText());

                //remove all the previous data in case the user clicks the confirm button once more time
                for (int i = op.tableResult.getRowCount()-1; i >= 0; i--) {
                    op.dtm.removeRow(i);
                }
                for (int i = im.listOfSat.size() - 1; i >= 0; i--) {
                    im.listOfSat.remove(i);
                }
                for (int i = im.listOfEight.size() - 1; i >= 0; i--) {
                    im.listOfEight.remove(i);
                }
                for (int i = im.listOfNineThirty.size() - 1; i >= 0; i--) {
                    im.listOfNineThirty.remove(i);
                }
                for (int i = im.listOfEleven.size() - 1; i >= 0; i--) {
                    im.listOfEleven.remove(i);
                }
                for (int i = im.listOfSunSix.size() - 1; i >= 0; i--) {
                    im.listOfSunSix.remove(i);
                }

                datesUsingIndicator();

                im.addPersonToCorrectQueue();

                //TODO: removePersonWithConstraint1();

                //op.setToCorrectTimetableSlot();

                im.resetEveryTwoWeeksAndSetToCorrectTimeSlot(op);

                constraint.removePersonWithConstraint1();

                of.setVisible(true);
            }
        });
    }


}

0 个答案:

没有答案