Accessed Nulls
by in CodeSOD on 2024-05-13"The attached class connects to an Access database," writes Nicolai. That's always a good start for a WTF. Let's take a look.
public class ResultLoader {
private static Logger sysLog = Logger.getLogger(ResultLoader.class);
private static String url = "somePath";
/**
* get the ResultTable from the Access database
*
* @param tableName
* @return
*/
private static Table getResultTable(String tableName) {
try {
// create a new file with the path to the table
File db = new File(url);
// let Jackcess open the file and return a table
return Database.open(db).getTable(tableName);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* load result from DB
*/
public static void loadResult() {
String tableName = "Result";
Table resultTable = getResultTable(tableName);
if (!resultTable.equals(null)) {
Map<Integer, Float> yearConsumption = new HashMap<Integer, Float>();
for (Map<String, Object> row : resultTable) {
/*
* [snip] does something with the table's rows
*/
}
Result result = new Result(00, new Date(), consumptions);
} else {
sysLog.info("There is no data object in the Access Database!");
}
}
}