I have two classes. One reads a text file into a two dimmensional array StringValues. Where I'm stuck is with passing the array into a method within a second class.
Class ReadFileContents works fine:
package data;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadFileContents {
public static String[][] main(String[][] args) throws IOException {
Scanner s = new Scanner(new File("C:\\INV\\TEST.txt"));
List<String[]> list = new ArrayList<String[]>();
while (s.hasNextLine()) {
String[] line = s.nextLine().split(" ");
list.add(new String[] { (line[0]),
(line[1]) });
}
int numberOfRows = list.size();
int numberOfColumns = 2;
String[][] StringValues = new String[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfRows; i++) {
StringValues[i] = list.get(i);
System.out.println(StringValues[i][0] + " " + StringValues[i][1]);
}
return StringValues;
}
}
class table encounters null pointer exception in main method:
package data;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class table extends JFrame{
JTable table;
public table(String[][] StringValues) throws IOException{
setLayout(new FlowLayout());
String[] columnNames = {"ECN", "Customer", "Model", "Serial"};
String[][] data = StringValues;
/*
Object[][] data = {
{"WES1584680", "NCL34IR", "828", "1603"},
{"MW3123WZEG", "AMDCCA", "22990-40", "423"},
{"ME3XZ74RJ5", "AMDCCA", "22990-60", "372"},
{"AAAY8ZKNW6", "AM23CA", "23", "58241172"},
{"ME3XZX16UF", "AMD787A", "23-00-6IG", "61229"}
};
*/
table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
add(scroll);
}
public static void main(String args[]) throws IOException
{
table gui = new table(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(600,200);
gui.setVisible(true);
gui.setLocationRelativeTo(null);
gui.setTitle("Inventory Table GUI");
}
}
Am I initializing the array in the table class wrong? String[][] data = StringValues;
You can see in the commented out portion of table method where I had hard-coded a test dataset for my 2d array values.
Any help is appreciated.

New Topic/Question
Reply



MultiQuote



|