I am learning Java. I have some code where I tried to call file names on directory and make it models to show table on main
Code :
import java.io.File;
import java.util.StringTokenizer;
import javax.swing.table.*;
public class SheetList { directory
public static DefaultTableModel load(){
File file = new File("sheet.");
File list[] = file.listFiles(); //load file list
String col[] = new String[2];
String colNames[] = {"Title", "Artist"};
DefaultTableModel model = new DefaultTableModel(colNames, 0);
for(int cnt = 0; cnt < list.length; cnt++){ // ERROR
String name = list[cnt].getName();
if(list[cnt].isFile()){
StringTokenizer stok = new StringTokenizer(name, " - ");
String token = stok.nextToken();
col[0] = token;
token = stok.nextToken();
col[1] = token;
model.addRow(col);
}
}
return model;
}
}
but a problem occurs on the marked line
Thread [main] (Suspended (exception NullPointerException))
SheetList.load() line: 12
Main.main(String[]) line: 14