Question

I am making a gui for POP3 commands I am having a problem editing my JTextPane in the GUI outside of the initialize() method

Part of the Action Listener:

       public void actionPerformed(ActionEvent e)
        {
            String Input = Commands.getText();
            verifyUserAndPass();

                    if(Input.substring(0).equals("QUIT")) {
                        System.exit(0);
                    }
                    if(Input.substring(0,4).equals("LIST")) {
                        ListCommand(Input);
                    }
                    if(Input.substring(0,4).equals("STAT")) {
                        ListCommand(Input);
                    }
                    if(Input.substring(0,4).equals("RETR")) {
                        try {
                            RETRCommand(Input);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                    if(Input.substring(0,4).equals("DELE")) {
                        Delete(Input);
                    }
                    if(Input.substring(0,4).equals("NOOP")) {
                        Display.setText("+OK");
                    }
                    if(Input.substring(0,4).equals("UIDL")) {
                        if(userEntered == true && passEntered == true) {
                        Display.setText("the UIDL is"+String.valueOf(ui));
                        ui++;
                        }else {
                            Display.setText("Please sign in first");
                        }
                    }
                    if(Input.substring(0,3).equals("TOP")) {
                        try {
                            TOP(Input);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                    if(Input.substring(0,4).equals("RSET")) {
                        Delete(Input);

            }

the verifyUserAndPass method:

    public void verifyUserAndPass() {
    String Input = Commands.getText();
    System.out.println(Input+"randomstring");
    if(Input.substring(0, 4).equals("USER")) {                  
        try {
            if(verifyUser(Input.substring(5))) {
                Display.setText("+OK");
                Commands.setText("");
                userEntered = true;
            } else {
                Display.setText("-ERR");
            }
        } catch (IOException e1) {
            e1.printStackTrace();
            Display.setText("-ERR");
        }
    } 
            if(Input.substring(0, 4).equals("PASS")) {
                    try {
                        if(userEntered == true) {
                        if(verifyPass(Input.substring(5))) {
                            Display.setText("+OK");
                            Display.setText("Welcome, you are now logged in");
                            Commands.setText("");  
                            passEntered = true;
                        } else {
                            Display.setText("-ERR");
                        }
                        } else {
                            Display.setText("Please enter USER first");
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

            }
    }

Commands is a JTextField, Display is a JTextPane. for some reason, I can edit Commands outside the ActionListener but not Display e.g. The Display.setText under the NOOP if works but not the one in verifyUserAndPass() method but the Commands.setText works What am I doing wrong?

Was it helpful?

Solution

It is quite hard to answer this question without you providing more details, but I can name a common issue:

Display may not be initialized, check your program's flow and then you can see why it is not being initialized.

Moreover, please abide the Java conventions for everyone's sake. Variables and methods are typed in camelcasing. So these ones would need to be changed:

  • String Input to String input.
  • ListCommand() to listCommand().
  • etc. I hope you get the idea.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top