I created a model with Carrier, CarrierService and CarrierDAO like this:
/**
- I am the Carrier bean object
*/
component accessors=“true”{
// properties
property name=“car_carrier_cd”;
property name=“car_carrier_nm”;
property name=“car_transport_mode”;
property name=“car_awb_prefix”;
// validation
this.constraints = {
car_carrier_cd = {required=true},
car_carrier_nm = {required=true},
car_carrier_mode = {required=true},
car_awb_prefix = {required=false}
};
function init(){
return this;
}
}
/**
- I am the CarrierService Model Object
*/
component singleton accessors=“true”{
// Dependency Injection
property name=“dao” inject=“CarrierDAO”;
property name=“log” inject=“logbox:logger:{this}”;
property name=“populator” inject=“wirebox:populator”;
property name=“wirebox” inject=“wirebox”;
function init(){
return this;
}
/**
- Get all carriers as an array of objects or query
*/
function list(boolean asQuery=false){
var q = dao.getAllCarriers();
log.info(“Retrieve all carriers”, q.recordcount);
if( asQuery ){ return q; }
// convert to objects
var carriers = [];
for(var x=1; x lte q.recordcount; x++){
arrayAppend( carriers, populator.populateFromQuery( wirebox.getInstance(“Carrier”), q, x ) );
}
return carriers;
}
/**
- I am the CarrierDAO Model Object
*/
component singleton accessors=“true”{
// Dependency Injection
property name=“dsn” inject=“coldbox:datasource:g2g”;
/**
- Constructor
*/
function init(){
return this;
}
query function getAllCarriers(){
var q = new Query(datasource="#dsn.name#",sql=“SELECT * FROM tab_carriers”);
return q.execute().getResult();
}
To test the MySQL connection, I could properly list the data from th tab_carriers table above and output
the contents of the table in an array object using writeDump(prc.aCarriers) in the controller below:
/**
- I am the carriers handler
*/
component{
// Dependency Injection
property name=“carrierService” inject=“CarrierService”;
function index(event,rc,prc){
// Get all carriers
prc.aCarriers = CarrierService.list();
//writeDump(prc.aCarriers);
event.setView(“carriers/index”);
}
However, when I want to display the same array output in the View carriers/index like this:
carriers.index
#html.table( data = prc.aCarriers, class=“table table-striped” )#
I get a java.lang.NullPointerException, apparently triggered by the HTMLhelper.cfc.
Any suggestions as to where could be the issue considering the fact that:
- I am not using ORM
- Coldbox was re-inited
- I assume that the models and the datasource are fine since I could pull the data from the MySQL table
into writeDump(prc.aCarriers).