在JFrame中读取文件和actionPerformed

时间:2018-04-18 18:36:03

标签: java swing nullpointerexception readfile

我正在研究一个项目的一部分。在这个NameFrame类中,我需要使用swing来绘制一个名为NameRecord的类的输入。并且该程序的工作方式是,如果我在文本框中键入名称并单击“图形”按钮;搜索与名称关联的数据,然后要求NameGraph类将其添加到当前显示的名称。如果找不到请求的名称,请让您的程序发出哔声。

我想在NameGraph类中当前显示的名称列表中添加一些存储空间。我不确定我的代码的哪一部分应该这样做。而且我不知道如何问清楚,清除所有,并搜索按钮来执行我在评论中写的内容。

我的阅读方法无法按预期工作。我从其他答案中得到了这个。当我运行它时,它给了我一个错误。

我现在真的很困惑。任何帮助将不胜感激。

import java.awt.BorderLayout;
import java.awt.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;
import java.io.FileNotFoundException;

public class NameFrame extends JFrame implements ActionListener {

 private NameGraph myGraph;
 private JTextField inputField;
 private JTextArea displayArea;
 private static final boolean SEARCH = false;
 NameRecord[] namesArray;

/**
 * Creates a new instance of NameFrame
 */
public NameFrame() {
    myGraph = new NameGraph();
    add(BorderLayout.CENTER, myGraph);

    JPanel buttonPanel = new JPanel();
    inputField = new JTextField(25);

    inputField.addActionListener(this);
    buttonPanel.add(inputField);

    JButton b = new JButton("Graph");
    b.addActionListener(this);
    buttonPanel.add(b);

    b = new JButton("Search");
    b.addActionListener(this);
    buttonPanel.add(b);

    b = new JButton("Clear All");
    b.addActionListener(this);
    buttonPanel.add(b);

    b = new JButton("Clear One");
    b.addActionListener(this);
    buttonPanel.add(b);

    b = new JButton("Quit");
    b.addActionListener(this);
    buttonPanel.add(b);

    add(BorderLayout.SOUTH, buttonPanel);

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    NameFrame f = new NameFrame();

    f.read("names-data.txt");
    f.setTitle("NameSurfer");
    f.pack();
    f.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    String s = e.getActionCommand();

    if (s.equalsIgnoreCase("graph")) {
        doGraph();
    } else if (s.equalsIgnoreCase("search")) {
        //– use the text in the text field and print to standard output a list of all
        //names that contain the string as a substring along with the highest rank for the
        //name for any year.
    } else if (s.equalsIgnoreCase("clear all")){
        setVisible(false);
        //– remove all names from the NameGraph

    } else if(s.equalsIgnoreCase("clear one")){

    //remove the name that was added to the NameGraph earliest.
    } else if (s.equalsIgnoreCase("quit")) {
        setVisible(false);
        dispose();
        System.exit(0);
    }
    else {
        // This is the response to the Enter Key pressed in inputField.
        doGraph();
    }
}

public void doGraph() {
    String s = inputField.getText();
    NameRecord r = new NameRecord(s);
    if (r == null) {
        Toolkit.getDefaultToolkit().beep();
    } else {
        myGraph.addName(r);
    }

    inputField.setText("");
}

public void read(String filename) {

    try {
        Scanner inFile = new Scanner(new File(filename));

        int i = 0;
        int num = Integer.parseInt(inFile.nextLine().trim());
        namesArray = new NameRecord[num];

        String inputString;
        while (inFile.hasNextLine()) {
            inputString = inFile.nextLine();
            namesArray[i] = new NameRecord(inputString);
            displayArea.append(namesArray[i].toString() + "\n");
        }
    } catch (IOException io) {
        System.out.println(io);
    }
}  // readFile()

}

~~~~~~~

编辑: 这是有问题的阅读方法。

    public void read(String fileName) {
    File theFile = new File(fileName);
    HashDict dictionary = new HashDict(); 

    try{
        Scanner reader = new Scanner(theFile); 

        while (reader.hasNext()){
            //creates a NameRecord for each line of file 
            String input = reader.nextLine();
            NameRecord r = new NameRecord(input); 
            String name = r.getName(); 

            for (int i = 0; i < 12; i++){
                 decades[i] = r.getRank(i);

            }
            dictionary.add(name, decades);       
        }             
    } 
    catch (FileNotFoundException e){
        System.out.print("File not found");
    }

错误消息是

Exception in thread "main" java.lang.NullPointerException
at NameFrame.read(NameFrame.java:133)
at NameFrame.main(NameFrame.java:76)

我在哪里有空指针?我为什么要这个? 非常感谢你。

0 个答案:

没有答案