-1

好的,所以基本上,我的头很痛,我现在被困住了。所以我有一个程序,它有一个打开按钮和一个文本区域。当您使用 hte 打开按钮时,您将打开一个用 Excel 制作的 csv 文件。我的程序应该打开一个文件,将其附加到文本区域,但随后我试图将所有值存储到一个多维数组中,以便我可以使用另一个类来计算诸如最高值和最低值。到目前为止,我刚刚开始尝试做最高的价值,但我没有运气。该文件打开并附加,但最大值出现某种错误。如果有人可以帮助我解决这个问题,我会非常感谢它。

主班

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.TextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;


public class task3 extends JFrame {

    public static JPanel contentPane;
    public JTextField txtOpen;
    public JButton btnOpen;
    public static TextArea textArea;
    int rows = 9, columns = 1441; //Columns up and down, rows across. Number of rows and columns in excell file.
    double[][] data = new double[rows][columns]; //Array to hold the rows and columns. 

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    task3 frame = new task3();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public task3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 560, 350);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        btnOpen = new JButton("Open");
        btnOpen.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {


                try{
                    JFileChooser chooser = new JFileChooser(); //File Chooser
                    chooser.setFileFilter(new FileNameExtensionFilter("Excell CSV Files", "csv"));
                    chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter()); //Filters for csv file

                    //Get the file
                    int returnVal = chooser.showOpenDialog(task3.contentPane); 
                    File file;
                    if(returnVal == JFileChooser.APPROVE_OPTION) {    
                        file = chooser.getSelectedFile();  
                    }
                    else{
                        file = null;
                    }


                    //Read the file into variable
                    BufferedReader in = new BufferedReader(new FileReader(file));
                    String line = in.readLine();

                    while(line != null){
                        task3.textArea.append(line + "\n");
                        line = in.readLine(); //Print out into text area
                    }

                    for(int i = 0; i < rows; i++){
                        //Use a temporary array to hold the rows in whilst the data is split.
                        String array [] = line.split(","); //Split the data up using the commas.
                        for(int j = 0; j < columns; j++){
                            //Take the rows and columns and convert them into a double so they can be saved into the array.
                            data[i][j] = Double.parseDouble(array[j]);
                        }
                    }

                }catch(Exception e){
                    e.printStackTrace();
                }

                double max = task3Methods.Max(data);
                textArea.append("\n\nmax is: "+max);
            }
        });
        btnOpen.setBounds(256, 22, 89, 23);
        contentPane.add(btnOpen);

        txtOpen = new JTextField();
        txtOpen.setBounds(61, 23, 174, 20);
        contentPane.add(txtOpen);
        txtOpen.setColumns(10);

        textArea = new TextArea();
        textArea.setBounds(27, 57, 507, 245);
        contentPane.add(textArea);
    }
}

方法类

public class task3Methods {

    public static double Max(double[][] data){

        double temp;

        double high = Double.MIN_VALUE;  //State this variable hold the largest possible double in the file.

        for (int i = 0; i < data.length; i++){ //Loop through the data.
            for(int j = 0; j < data[i].length; j++){
                temp = data[i][j];
                if(temp > high){ //If a number in the array is larger than the current highest value...
                    high = temp; //Store the new highest value.
                }               
            }
        }

        return high;
    }
}

这是我在下拉框中使用的 Excel 文件的链接 https://www.dropbox.com/s/62ssjoph2z6vmv3/Temperature_Log.csv

对不起,这是确切的错误

java.lang.NullPointerException
    at task3$2.mouseClicked(task3.java:86)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

在文本区域中,不是说 Max = 21.5456 或其他什么,而是说 max is: 4.9E-324

4

1 回答 1

1

本节出现问题:

while (line != null) {
   A.textArea.append(line + "\n");
   line = in.readLine(); // Print out into text area
}

for (int i = 0; i < rows; i++) {
   // Use a temporary array to hold the rows in whilst the data is split.
   String array[] = line.split(","); // Split the data up using the commas.
   for (int j = 0; j < columns; j++) {
      // Take the rows and columns and convert them into a double so they can be saved into the array.
      data[i][j] = Double.parseDouble(array[j]);
    }
}

在您的 while 循环的最后一次迭代之后,in.readLine()将始终返回 null。你稍后再打电话line.split(","),这是造成的NullPointerException。如果我理解你的代码,你不想在line.split()这里使用,因为你将所有文件的文本附加到A.textArea. 相反,您应该做两件事之一,要么在从文件读取时将文本存储在数组中,要么A.textArea.getText()用于访问先前读取的行。

于 2013-11-01T15:34:34.680 回答