I have a DAO which makes use of an xml file as a pseudo-database. This is my DAO Class:
package com.bootcamp.webapps.DAOs;
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.apache.log4j.Logger;
import com.bootcamp.webapps.beans.DragonBean;
import com.bootcamp.webapps.beans.DragonDatabaseBean;
public class DragonDAO {
private Logger logger = Logger.getLogger(DragonDAO.class);
private List<DragonBean> dragonsList;
private DragonDatabaseBean dragonDatabase;
public DragonDAO() {
}
private void getDragonsFromDatabase() {
try {
File file = new File("dragonDatabase.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(DragonDatabaseBean.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
dragonDatabase = (DragonDatabaseBean) jaxbUnmarshaller.unmarshal(file);
dragonsList = dragonDatabase.getDragonsList();
System.out.println("dragonListSize: " + dragonsList.size());
} catch (JAXBException e) {
logger.debug("ERROR: " + e.getMessage());
} catch (Exception e) {
logger.debug("ERROR: " + e.getMessage());
}
}
public Logger getLogger() {
return logger;
}
public List<DragonBean> getDragonsList() {
getDragonsFromDatabase();
return dragonsList;
}
}
What's weird is that when I use a main method and make use of getDragonsFromDatabase() method (after adding the static keyword to the necessary places in my code), it seems to work. My log4j just shows this message though:
2016-01-04 11:05:27 DEBUG DragonDAO:34 - ERROR: null