每当在JTextField中键入内容时,就会弹出“ AWT-EventQueue-0” java.lang.NullPointerException错误

时间:2019-05-11 22:53:30

标签: java swing user-interface file-io jtextfield

我正在创建一个游戏,其中从文本文件中提取问题,并将问题设置到JTextArea中。每个问题的答案都在文本文件中,并且用户应将给定问题的答案输入文本字段。从文本文件中提取问题时,无论何时我输入任何内容都不一定是答案,都会弹出一堆错误。

// 4/28/2019
// GamePanel.java
import java.awt.BorderLayout; // import everything needed
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JSlider;
import javax.swing.JButton;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.Timer;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;  

import java.util.Scanner;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.IOException;




// This panel is the panel which has the game. It is mainly based off of graphics.
// It creates a clown which is juggling balls, and also the background is drawn
// and it is all animated. It also has a TextArea-which displays the question
// and a TextField- which is for the user to type in their answer

public class GamePanel extends JPanel implements ActionListener
{

    private CardPanel cp; // instance of CardPanel from the class 'StartPanel'
                            // to use to switch between cards
    private CardLayout cards; // instance of cardLayout

    private Timer timer;  //Timer for animation
    private int counter;  // counter for animating arms
    private Color colors[];  // array for holding different colors
    private int colorCounter;  // counter for animating balls

    private Scanner questionReader;
    private Scanner answerReader;

    private String questions[];
    private String answers[];

    private String question;
    private String answer;

    private int random;
    private String num;
    private int numQuestion;
    private int numAnswer;



    // this panel is also the holder panel- it holds the question TextArea and
    // the TextField which the answer is supposed to be typed into
    public GamePanel(CardPanel cpIn, CardLayout cardsIn)
    {

        questions = new String[25];
        answers = new String[25];



        answer = new String(" ");
        question = new String(" ");

        cards = cardsIn;
        cp = cpIn;  

        QuestionTA qta = new QuestionTA();
        qta.setBounds(300, 35, 400,100);
        add(qta);

        AnswerTF atf = new AnswerTF();
        atf.setBounds(375, 150, 250, 50);
        add(atf);

        BackButtonPanel bbp = new BackButtonPanel();
        bbp.setBounds(600,250,100,200);
        add(bbp);



        setLayout(null); // this panel is a null layout

        Color color = new Color(217, 217, 253);
        setBackground(color);

        timer = new Timer(25, this);
        counter = 2;
        colorCounter = 5;




    }
    public static void main(String[] args)
    {
    }






     }

     // this class creates the TextArea which contains the text area, which  
     // will show all the question that needs to be answered  
    class QuestionTA extends JPanel  
    {
        private JTextArea questionArea; // the JTextArea which will show the question
        private String inFileName;

        public QuestionTA()
        {
            questionArea = new JTextArea("");
            questionArea.setPreferredSize(new Dimension(400,400));  
            questionArea.setEditable(false);
            questionArea.setLineWrap(true);

            add(questionArea);

            getQuestions();


        }

        public void getQuestions()
        {
            inFileName= new String("easyequations.txt");
            File inFile = new File(inFileName);

            try
            {
                questionReader = new Scanner(inFile);

            }
            catch(FileNotFoundException e)
            {
                System.err.println(inFileName + " not found.");
                System.exit(1);

            }


            while(questionReader.hasNext())
            {
                int random = (int)(Math.random()*25 + 1);
                String line = questionReader.nextLine();

                num = line.charAt(0) + "";
                numQuestion  = Integer.parseInt(num);

                if(random == numQuestion)
                {
                    questionArea.setText(line);
                }



            }


        }



     }

    // this class creates the text field which the user will type the answer into
    // based on the question shown in the text area. We haven't set up the
    // question and answer logic yet in this draft.  
    class AnswerTF extends JPanel implements ActionListener
    {
        private JTextField answer;
        private int parsedAnswerNum; private String line;
        private String wholeFile;
        private String realAnswer;

        String answered;

        public AnswerTF()
        {
            answer = new JTextField("Enter the answer");
            answer.addActionListener(this);
            getAnswers();
            add(answer);

            answered = "";



        }

        public void getAnswers()
        {

            int numberOfA = numQuestion;


            String inFileAnswers = new String("easysolutions.txt");
            File answerFile = new File(inFileAnswers);

            try
            {
                answerReader = new Scanner(answerFile);
            }
            catch(FileNotFoundException e)
            {
                System.err.println("Could not read from " + inFileAnswers);
                System.exit(1);
            }

            while(answerReader.hasNext())
            {

                String line = new String(answerReader.nextLine());
                if(line.indexOf(numberOfA) != -1)
                {
                    System.out.println(line);
                    realAnswer = line;
                }

            }



        }

        public void actionPerformed(ActionEvent evt)
        {
            if(evt.getSource() == answer)
            {
                answered = answer.getText();

                if(answered.equals( realAnswer.substring( (realAnswer.indexOf(" ") + 1), realAnswer.length()-1 )))
                {
                    System.out.println("you are corect");
                }


            }

        }


    }

    class BackButtonPanel extends JPanel implements ActionListener
    {
        private JButton goBack;
        private QuestionTA qta;
        public BackButtonPanel()
        {
            goBack = new JButton("Return Home");
            goBack.addActionListener(this);
            add(goBack);
            qta  = new QuestionTA();

        }
        public void actionPerformed(ActionEvent evt)
        {
            if(evt.getActionCommand().equals("Return Home"))
            {

                cards.show(cp, "home");
                qta.getQuestions();
            }

        }
    }

}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at GamePanel$AnswerTF.actionPerformed(GamePanel.java:403)
        at java.desktop/javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
        at java.desktop/javax.swing.JTextField.postActionEvent(JTextField.java:723)
        at java.desktop/javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:839)
        at java.desktop/javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1810)
        at java.desktop/javax.swing.JComponent.processKeyBinding(JComponent.java:2900)
        at java.desktop/javax.swing.JComponent.processKeyBindings(JComponent.java:2948)
        at java.desktop/javax.swing.JComponent.processKeyEvent(JComponent.java:2862)
        at java.desktop/java.awt.Component.processEvent(Component.java:6366)
        at java.desktop/java.awt.Container.processEvent(Container.java:2261)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4966)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2319)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
        at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950)
        at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:871)
        at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1140)
        at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1010)
        at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:836)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4847)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2319)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4798)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.desktop/java.awt.EventQueue.access$600(EventQueue.java:97)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

错误在底部。我的预期结果是,只要用户输入正确的答案,终端就会打印“好作业”。我希望这些错误出现在“ QuestionTA”类的“ getQuestions”方法中,并且 “ AnswerTF”类中的“ getAnswers” +“ actionPerformed”方法。这两类都接近最低水平。请问我是否要标记用于分析错误的方法。

0 个答案:

没有答案