-1

For this problem, my enter key for JTextField is not initalizing the if-else statement inside the chat();

I tried putting the userinput.addKeyListener(this); inside public guitest(); as well but its not working.

How do I get it work so that I can initialize the if else statement to reply me an answer. The error given was:

Exception in thread "main" java.lang.NullPointerException

public class guitest extends JFrame implements KeyListener{
ArrayList questionarray = new ArrayList();
ArrayList answerarray = new ArrayList();
public JTextArea scriptarea = new JTextArea();
public JTextField userinput = new JTextField();
public String stringinput;

public ArrayList getquestionarray(){
    return questionarray;
}
public ArrayList getanswerarray(){
    return answerarray;
}
public guitest(){

    //gui
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    this.setSize(600, 600);
    this.setVisible(true);
    this.setResizable(false);
    this.setLayout(null);
    this.setTitle("Minion AI");
    setLocationRelativeTo(null);

    //jtextfield location and field
    userinput.setLocation(7, 530);
    userinput.setSize(580, 30);

    //jtextarea location and size
    scriptarea.setLocation(15, 5);
    scriptarea.setSize(560, 510);
    scriptarea.setEditable(false);

    //adding methods to frame
    this.add(userinput);
    this.add(scriptarea);
    chat();
}
@Override
public void keyTyped(KeyEvent e) {
}

@Override
public void keyPressed(KeyEvent e) {
    if(e.getKeyChar() == KeyEvent.VK_ENTER){
        stringinput = userinput.getText();
        scriptarea.append("[You] " + stringinput + "\n");
        userinput.setText("");
                }
    if(e.getKeyChar() == KeyEvent.VK_ESCAPE){
        System.exit(0);
    }
}
@Override
public void keyReleased(KeyEvent e) {
}
public void chat(){
    Random random = new Random();
    ArrayList greetingarray = greetings();
    ArrayList quotesarray = quotes();
    scriptarea.append("[AI Minion] "+greetingarray.get(random.nextInt(greetingarray.size()))+"\n");
    userinput.addKeyListener(this);
        if(greetingarray.contains(stringinput)){
            for(int i=0; i<1; i++){
                scriptarea.append("[AI Minion] "+greetingarray.get(random.nextInt(greetingarray.size()))+"\n");
            }
        }else if(questionarray.contains(stringinput)){
            scriptarea.append("[AI Minion] "+answerarray.get(questionarray.indexOf(stringinput))+" \n");
        }else if(stringinput.contains("who")){
            scriptarea.append("[AI Minion] I am a creature created to assist you in your experience in talking to me. \n");
        }else if(stringinput.contains("bye")||stringinput.contains("end")){
            scriptarea.append("[AI Minion] Bye byee ♥‿♥ \n");
        }else{
            scriptarea.append("[AI Minion] Sorry, I dont understand your alien language. \n");
        }
}
public static void main(String[] args){
    new guitest();
}

}

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Phua ZZ
  • 19
  • 2
  • You should provide more relevant codee snippet. It is not clear wich interaces do you class implements, how is it called. Why you call new guitest() since according to the convention all class names (and hence their constructors) are to be capitalized (the first letter). – Alexey R. Aug 03 '17 at 13:42

1 Answers1

1

Too many problems in your code:

  1. You're having the below statement at the begining of the program:

    this.setVisible(true);
    

    This should be one of the last lines in your program, and after pack().

  2. extends JFrame you shouldn't extend a JFrame because you're not changing any of its functionallity and as it's a rigid container, you cannot place it inside other containers. Build your program based on JPanels instead. Read: Extends JFrame vs. creating it inside the program

  3. guitest -> GuiTest and stringinput; -> stringInput, please follow the Java naming conventions:

    • firstWordLowerCaseVariable
    • firstWordLowerCaseMethod()
    • FirstWordUpperCaseClass
    • ALL_WORDS_UPPER_CASE_CONSTANT

    And use them consistently as this will make the program easier to read to you and us

  4. this.setLayout(null); and .setLocation(...); are going to give you headaches, instead use one (or combine) Layout Manager, take this answer as an example of what happens when you use it and try to export it to be seen in another PC. Swing was designed to work with layout managers not with absolute pixel positioning as it has to work with different PLAFs, OS, screen sizes and resolutions, a layout manager will solve all of these.

  5. Regarding to (3), your method names should be verbs greetings() should be getGreetings() for example (method which you haven't included in your question and which may be returning null)

  6. I would also only declare my variables as class members but initialize them on the constructor only.

  7. this.setSize(600, 600); call pack() instead (after you use the layout managers)

And as your code is incomplete, learn to use a debugger and see what variable is null that is why your code is breaking, the exception will tell you in the first three lines.

Frakcool
  • 10,915
  • 9
  • 50
  • 89