わかりました、基本的に、頭が痛いので、今はこれで立ち往生しています。だから私は開くボタンとテキスト領域を持つプログラムを持っています。開くボタンを使用すると、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 と表示されます。