问题
I need to fill a JTable with a static 2D array. I've created this model for the JTable
:
public class InsertMatToJTable extends AbstractTableModel{
String titre[] = {"age real", "sex real", "chest real", "resting_blood_pressure real","serum_cholestoral real","fasting_blood_sugar real","resting_electrocardiographic_results real","maximum_heart_rate_achieved real","exercise_induced_angina real","oldpeak real","slope real","number_of_major_vessels real","thal real", "class"};
String line;
float mat[][]= new float[270][13];
float matrice_normalise[][];
int i = 0,j=0;
public void InsertMatToJTable()
{
try {
FileInputStream fis = new FileInputStream("fichier.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
while ((line = br.readLine()) != null) {
StringTokenizer st1 = new StringTokenizer(line, " ");
while (st1.hasMoreTokens())
{mat[i][j]=Float.valueOf(st1.nextToken()).floatValue();
j++;
if (st1.hasMoreTokens()!=true) i++;
}
}
br.close();
}catch(Exception e) {
e.printStackTrace();}
Normalisation norm = new Normalisation(mat);
// for(i=0;i<270;i++)
//{for(j=0; j<14;j++)
//{matrice_normalise[i][j]=norm.mat_normalised[i][j];
//}
matrice_normalise=norm.mat_normalised;
}
@Override
public int getRowCount() {
return 270*13;
}
@Override
public int getColumnCount() {
return 13;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return matrice_normalise[rowIndex][columnIndex];
}
public String getColumnName(int columnIndex) {
return titre[columnIndex];
}
}
Basically, this code reads from a text file, each line contains 13 numeric values, and stores them into a static matrix, then applies some other treatment called "normalisation".
The problem here seems to be in the "getValueAt" function. I have this error everytime :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at prodm.InsertMatToJTable.getValueAt(InsertMatToJTable.java:62)
First, I need to know if this code actually does what I think does, which is storing the data in the matrix the same way it is stored in the text file.
And second, I really have no idea about what's wrong with the getValueAt function ?
Also, I have noticed something else. There's definitely something wrong with this part :
while ((line = br.readLine()) != null) {
StringTokenizer st1 = new StringTokenizer(line, " ");
while (st1.hasMoreTokens())
{mat[i][j]=Float.valueOf(st1.nextToken()).floatValue();
j++;
if (j==13) {i++;j=1;}
}
It reads from the file, but it doesn't store the data the way it should. Basically, it introduces a "shift", starting from the second line. For exemple, what should be stored at [1][0] is at [1][1], [2][0] is in [2][2]...etc.
回答1:
The problem is that you have written:
public void InsertMatToJTable()
but you should have written:
public InsertMatToJTable()
Notice that there is no void
in the second snippet.
You have declared a method called InsertMatToJTable
and not the constructor of the class with the same name. Therefore, when you invoke new InsertMatToJTable()
you invoke the default no-args constructor and your code is never run, leaving your matrix remains uninitalized, hence the NullPointerException
.
To avoid these kind of typo issues, add logs to your code and use a debugger to find problems.
Here is an example demo of a working code.
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
public class TestTables {
private static final int ROWS = 270;
private static final String titre[] = { "age real", "sex real", "chest real", "resting_blood_pressure real", "serum_cholestoral real",
"fasting_blood_sugar real", "resting_electrocardiographic_results real", "maximum_heart_rate_achieved real",
"exercise_induced_angina real", "oldpeak real", "slope real", "number_of_major_vessels real", "thal real", "class" };
public static class InsertMatToJTable extends AbstractTableModel {
private float[][] matrice_normalise;
public InsertMatToJTable(float[][] matrice_normalise) {
this.matrice_normalise = matrice_normalise;
}
@Override
public int getRowCount() {
return matrice_normalise.length;
}
@Override
public int getColumnCount() {
return titre.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return matrice_normalise[rowIndex][columnIndex];
}
@Override
public String getColumnName(int columnIndex) {
return titre[columnIndex];
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame(TestTables.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
float[][] matrix = new float[ROWS][titre.length];
Random random = new Random();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = random.nextFloat() * 100;
}
}
InsertMatToJTable model = new InsertMatToJTable(matrix);
JTable table = new JTable(model);
JScrollPane scroll = new JScrollPane(table);
frame.add(scroll);
frame.pack();
frame.setVisible(true);
}
});
}
}
来源:https://stackoverflow.com/questions/13552377/matrix-into-jtable