Given a text file as follow :
10
100
99
99
96
96
92
92
91
88
87
86
Where the first number "10" means that the text file is containing 10 integer and the second number 100 means that all the numbers in the text file doesn't exceed 100.
My objective is to read the text and fill an int data[][]; as follow :
data[0][0]=1 data[0][1]=99
data[1][0]=2 data[1][1]=99
data[2][0]=3 data[2][1]=96
data[3][0]=4 data[3][1]=96
data[4][0]=5 data[4][1]=92
data[5][0]=6 data[5][1]=92
data[6][0]=7 data[6][1]=91
data[7][0]=8 data[7][1]=88
data[8][0]=9 data[8][1]=87
data[9][0]=10 data[9][1]=86
cbin = 100 // bin capacity
nbin = 10 // number of objects
That's means first raw for index and second for item's value or weights .. and int cbin = \\the second text's value and int nbin = \\the first text's value
the problem that i get an Exception in thread
"AWT-EventQueue-0" java.lang.NullPointerException
Here is my code :
public static int[][] data=null; \\ in the first of the document
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f= chooser.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException ex) {
Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
int i=0,j=0;
while (line != null) {
if(i==0){
nbin= (int) Integer.parseInt(""+line);
System.out.println(nbin);
}
else if (i==1) {
cbin=(int) Integer.parseInt(""+line);
System.out.println(cbin);
}
// sb.append(line);
line = br.readLine();
if(i >= 2 && line != null){
data[j][1]=(int) Integer.parseInt(line);
data[j][0]=j;
j++;
}
i++;
}
} catch (IOException ex) {
Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(interface1.class.getName()).log(Level.SEVERE, null, ex);
}
}
Any ideas ??
nbinandcbincorrectly then it stop. Besides theffile refer to the text i mentioned above .