0

I am quite new to Java and before I end up asking this question I searched and searched SO, but I don't seem to get my head around it. As you will see I have a class that creates a GUI, asks for some input and stores that input in a returnable String[].

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

public class InputConsole {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static String[] details = new String[3];
public static JButton btn2013;
public static JButton btn2014;
public static JButton btn2015;
public static JButton btn2016;
public static JButton btnGo;
public static JTextField textField2;
public static JTextField textField4;

public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    if (shouldFill) {
        c.fill = GridBagConstraints.HORIZONTAL;
    }

    btn2013 = new JButton("ZMR 2013");
    btn2013.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2013";
            btn2014.setEnabled(false);
            btn2015.setEnabled(false);
            btn2016.setEnabled(false);
            textField2.requestFocusInWindow();

        }
    });
    if (shouldWeightX) {
        c.weightx = 0.0;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 0, 0, 0);
    c.gridx = 0;
    c.gridy = 0;
    pane.add(btn2013, c);

    btn2014 = new JButton("ZMR 2014");
    btn2014.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2014";
            btn2013.setEnabled(false);
            btn2015.setEnabled(false);
            btn2016.setEnabled(false);
            textField2.requestFocusInWindow();
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.insets = new Insets(10, 10, 0, 0);
    c.weightx = 0.0;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(btn2014, c);

    btn2015 = new JButton("ZMR 2015");
    btn2015.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2015";
            btn2013.setEnabled(false);
            btn2014.setEnabled(false);
            btn2016.setEnabled(false);
            textField2.requestFocusInWindow();
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.0;
    c.gridx = 2;
    c.gridy = 0;
    pane.add(btn2015, c);

    btn2016 = new JButton("ZMR 2016");
    btn2016.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[2] = "2016";
            btn2013.setEnabled(false);
            btn2014.setEnabled(false);
            btn2015.setEnabled(false);
            textField2.requestFocusInWindow();
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.0;
    c.gridx = 3;
    c.gridy = 0;
    pane.add(btn2016, c);

    JLabel textField1 = new JLabel("What was your Bib number? : ");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 2;
    pane.add(textField1, c);

    textField2 = new JTextField(10);
    textField2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[0] = textField2.getText();
            textField4.requestFocusInWindow();

        }

    });

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.gridy = 2;
    pane.add(textField2, c);

    JLabel textField3 = new JLabel("What is your email address : ");

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 2;
    pane.add(textField3, c);

    textField4 = new JTextField(15);
    textField4.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            details[1] = textField4.getText();
            btnGo.setEnabled(true);
            btnGo.requestFocusInWindow();

        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 2;
    c.gridy = 3;
    pane.add(textField4, c);

    btnGo = new JButton("Go And Get Me My Diploma!");
    btnGo.setEnabled(false);
    btnGo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            JOptionPane.showMessageDialog(null, details[0] + " " + details[1] + " " + details[2]);
        }
    });
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 0;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;
    c.insets = new Insets(10, 0, 0, 0);
    c.gridx = 0;
    c.gridwidth = 4;
    c.gridy = 4;
    pane.add(btnGo, c);
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("Zagori Mountain Running Diploma Maker");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setSize(500, 250);
    frame.setVisible(true);
}

public String[] inputBib() {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });

    return details;

}

But when I call this GUI in another class

public class CheckFiles {
InputConsole bibInput = new InputConsole();

String[] detailsInput = bibInput.inputBib();
private static Scanner scanner;

public String bibCorrected()  {

    String yearToCheck = null;
    {

        if (detailsInput[2] == "2016") {
            yearToCheck = "ZMR2016.txt";
        } else if (detailsInput[2] == "2015") {
            yearToCheck = "ZMR2015.txt";
        } else if (detailsInput[2] == "2014") {
            yearToCheck = "ZMR2014.txt";
        } else {
            yearToCheck = "ZMR2013.txt";

in order to obtain that String[], I get a java.lang.NullPointerException. I Know that I get this because the program does not wait for the GUI to get all the input and files the returnable String[] as null. I think I know that I have to do something with wait() and notify() but I do not seem to understand exactly what. Thank you in advance for any suggestions. (and very sorry for the long thread)

1 Answers1

1

You could add a button on your GUI which will just call the bibCorrected() method. Currently you are showing and then returning so the array is empty and arg 2 is none existent therefor throwing an NPE. This would probably be the easiest way to resolve the issue.

Also, it's better to use String.equals(String) rather than ==. Read this StackOverflow post What is the difference between == vs equals() in Java?

Community
  • 1
  • 1
Walshy
  • 850
  • 2
  • 11
  • 32
  • So, what you are suggesting is that i skip the entire class CheckFiles{} and do all the work inside the GUI? – Yannis Papastylos Jan 31 '16 at 12:23
  • You can have the CheckFiles class just call the method and make it take the array as an argument. – Walshy Jan 31 '16 at 12:27
  • I am sorry to bother you again but as I told you I am new to Java architecture. I have been trying for hours to figure out where to place that button you are talking about and how to call bibCorrected() from there. I understood the fact that I am showing the GUI and then returning but I do not know how to proceed. Would you be so kind as to elaborate on your previous answer. – Yannis Papastylos Jan 31 '16 at 20:20
  • In your GUI class create a JButton with a actionListener which runs the method in CheckFiles. In your CheckFiles class change the bibCorrected method to `public String bibCorrected(String[] detals) {` and use that. – Walshy Jan 31 '16 at 20:40
  • Thank you for pointing me to the right direction. I did not do it the way you specified, but your observation that I just show the GUI and return nothing, helped figure out a way to do my job. All I did was add to the listener of the GUI;s final button my two methods. One for checking the files and a second one for sending an email with the input provided by the user. Thank you again. – Yannis Papastylos Feb 07 '16 at 16:38