Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

Matrix report

 
tichakunda gad mazvabo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Report post to moderator
The code is suppose to print matrix report and it complains about NullpointerException please help


public ActionForward matrixReport(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws ReportWizardActionException {

try {
// Get report parameters ...and?? TODO
ActionForward forward = mapping.findForward("simpleSqlReport");
ActionForward newForward = new ActionForward();
StringBuffer forwardUrlPath = createSimpleSQLReportWebParam(
request, forward, form);
newForward.setPath(forwardUrlPath.toString());
Debug.println(this, "<< matrixReport ");
return newForward;

} catch (Exception e) {
String msg = "Exception: " + e.getClass().getName() + ", MSG: "
+ e.getMessage();
Debug.println(this.getClass(), Debug.INFO, msg);
throw new ReportWizardActionException(msg);
}
}

/**
* TODO ...
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward bucketMatrixReport(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws ReportWizardActionException {

HttpSession session = request.getSession();
try {
Debug.println(this, "<< bucketMatrixReport: IN HERE myReport");

ReportWizardForm wizForm = (ReportWizardForm) form;
String fromDate = wizForm.getStartdate();
String toDate = wizForm.getEnddate();
String interval = wizForm.getInterval();
ArrayList bucketDates = createBucketDates(fromDate, toDate,
interval);

// Recover the intial REPORT created by the SimpleSQLReportAction
Report prereport = (Report) session.getAttribute("report");

// refactor the report according to the bucketDates.
Report bucketReport = createBucketReport(bucketDates, prereport,
interval);
//Debug.println(this, prereport.getRecords().get(0).toString());
//Debug.println(this, String.valueOf(bucketReport.getRecords().size()));
//Debug.println(this, bucketReport.getRecords().get(0).toString());

return (mapping.findForward("matrixReportResult"));
} catch (Exception e) {
e.printStackTrace();
String msg = "Exception: " + e.getClass().getName() + ", MSG: "
+ e.getMessage();
Debug.println(this.getClass(), Debug.INFO, msg);
throw new ReportWizardActionException(msg);
}

}

/**
* Method to create bucketDates based on interval selected. These dates are
* the "Start"Dates for each internval
*
* @param fromDate
* @param toDate
* @param interval
* @return bucketDates . The ArrayList of dates
* @throws
*/
private ArrayList createBucketDates(String fromDate, String toDate,
String interval) {// TODO Thando to put int exception handling

ArrayList bucketDates = new ArrayList();

long dailyIncreament = 24 * 60 * 60 * 1000;
long weeklyIncreament = 7 * dailyIncreament;

// create calendar instance usinf fromDate, set time to 0
Timestamp tsFrom = Timestamp.valueOf(fromDate);
Date dtFrom = new Date(tsFrom.getTime());
Calendar calFromDate = new GregorianCalendar();

calFromDate.setTime(dtFrom);
calFromDate.set(Calendar.HOUR, 0);
calFromDate.set(Calendar.MINUTE, 0);
calFromDate.set(Calendar.SECOND, 0);

// create calendar instance using toDate, time set to 0
Timestamp tsTo = Timestamp.valueOf(toDate);
Date dtTo = new Date(tsTo.getTime());
Calendar calToDate = new GregorianCalendar();
calToDate.setTime(dtTo);
calToDate.set(Calendar.HOUR, 0);
calToDate.set(Calendar.MINUTE, 0);
calToDate.set(Calendar.SECOND, 0);

if (interval.equals(AppConstants.INTERVAL_DAILY)) {
// add dates in range into an ArrayList, move a day forward and test
// for equality
do {
bucketDates.add(calFromDate.getTime());
Debug.println(this, calFromDate.getTime().toString());
Debug.println(this, calToDate.getTime().toString());
// TODO Thando, to check the month end scenario's
// TODO 24*60*60


long day = calFromDate.getTimeInMillis() + dailyIncreament;
calFromDate.setTimeInMillis(day);
//calFromDate.roll(calFromDate.DATE, true);
Debug.println(this, "From date " + calFromDate.toString());
Debug.println(this, "To date " + calToDate.toString());

} while (calFromDate.before(calToDate));
}else if(interval.equals(AppConstants.INTERVAL_WEEKLY)){
do {
bucketDates.add(calFromDate.getTime());
Debug.println(this, calFromDate.getTime().toString());
Debug.println(this, calToDate.getTime().toString());

long day = calFromDate.getTimeInMillis() + weeklyIncreament;
calFromDate.setTimeInMillis(day);
//calFromDate.roll(calFromDate.DATE, true);
Debug.println(this, "From date " + calFromDate.toString());
Debug.println(this, "To date " + calToDate.toString());

} while (calFromDate.before(calToDate));
}else if(interval.equals(AppConstants.INTERVAL_MONTHLY)){
do {
bucketDates.add(calFromDate.getTime());
Debug.println(this, calFromDate.getTime().toString());
Debug.println(this, calToDate.getTime().toString());

calFromDate.roll(calFromDate.MONTH, true);
Debug.println(this, "From date " + calFromDate.toString());
Debug.println(this, "To date " + calToDate.toString());

} while (calFromDate.before(calToDate));
}else if(interval.equals(AppConstants.INTERVAL_ANNUALLY)){
do {
bucketDates.add(calFromDate.getTime());
Debug.println(this, calFromDate.getTime().toString());
Debug.println(this, calToDate.getTime().toString());

calFromDate.roll(calFromDate.YEAR, true);
Debug.println(this, "From date " + calFromDate.toString());
Debug.println(this, "To date " + calToDate.toString());

} while (calFromDate.before(calToDate));
}

return bucketDates;
}

/**
* This method takes the previously run "REPORT" object produced by the
* SimpleSQLReportAction here we re-process the Report into the buckets
* selected from the Report Wizard.
*
* @param bucket
* @param report
* @param frequency
* @return Forwards the REPORT object to the HTTP Session for JSP process
*/
private Report createBucketReport(ArrayList bucketDates, Report report, String interval) throws ReportWizardActionException{
Report modifiedReport = new Report();
ArrayList sortedcolumns = new ArrayList();
boolean bucketFlag = true;
boolean newRec = false;
String key = new String();
String prevKey = new String();
String nameCol = new String();
String scoreCol = new String();
String dateCol = new String();
float score = 0f;
float bucketscore = 0f;
int scoreCount = 0;
Timestamp datets=null;
Date recdate=null;
Date currBucketDate=null;
Date beginDate = null;

ReportRecord reportRecord2 = new ReportRecord();
ReportRecord record = null;
TreeMap cells = null;
Iterator iter = null;

Debug.println(this, "Report 1>> " + report);
Debug.println(this, "================== Before generating report 2 ...." );
try {
// Initialise the control values...
String keyIndex =(String)((ReportRecord)report.getRecords().get(1)).getCells().firstKey();
ReportCell keyCell = (ReportCell)((ReportRecord)report.getRecords().get(1)).getCell(keyIndex);
key = (String)keyCell.getFieldVal().toString();

Debug.println(this.getClass().getName(), " Initial Key: " + key);

prevKey = key;
int norecs = report.getRecords().size();

Debug.println(this, "Report 2: No Recs = " + norecs);
for(int i =0; i < norecs; i++){
Debug.println(this, "RECORD >> " + report.getRecords().get(i).toString());
record = (ReportRecord)report.getRecords().get(i);

cells = (TreeMap)record.getCells();
iter = cells.keySet().iterator();

while(iter.hasNext()){
String column_name = (String)iter.next();
Object value = null;
try{
value = (String)((ReportCell)record.getCells().get(column_name)).getFieldVal().toString();
if(column_name.equalsIgnoreCase("fullName")){

key = value.toString();
Debug.println(this, "rec: " + i + ", FullName: " + key);
}

if(column_name.equalsIgnoreCase("scorevalue")){
score = Float.parseFloat(value.toString());
Debug.println(this, "rec: " + i + ", Score: " + score);
}

if(column_name.equalsIgnoreCase("lastupdated")){
datets = Timestamp.valueOf(value.toString());
Date updated = new Date(datets.getTime());
Calendar calUpdated = new GregorianCalendar();
calUpdated.setTime(updated);
// Init the time to 0:0:0
calUpdated.set(Calendar.HOUR, 0);
calUpdated.set(Calendar.MINUTE, 0);
calUpdated.set(Calendar.SECOND, 0);

recdate = calUpdated.getTime();

Debug.println(this," rec: RecDate = "+ updated);
}
}catch (ClassCastException e) {
String msg = "Exception: " + e.getClass().getName() + ", MSG: "
+ e.getMessage();
Debug.println(this.getClass(), Debug.INFO, msg);
throw new ReportWizardActionException(msg);
}
}

//check if currenyt bucket date is null. if it is get the current bucket date using the recod date and the bucketdates ArrayList
//if(currBucketDate == null){
//Debug.println(this, "CurrBucketDate == null");
////begin date might be after the next entities begin date, this must always be set to an entities begin date.
//currBucketDate = getBucketDate(bucketDates, recdate);
//beginDate = currBucketDate;
//}

// If we have changed to next object rec, add the new Rec, and
// continue...
if(!key.equals(prevKey) && bucketFlag == false){
prevKey = key;
String cellKey = currBucketDate.toString();
Object cellVal = new Float(bucketscore/scoreCount);
reportRecord2 = new ReportRecord();
reportRecord2.init(modifiedReport.getSortedColumns());
reportRecord2.addCell(cellKey, cellVal);
modifiedReport.addRecord(reportRecord2);

reportRecord2 = new ReportRecord();

scoreCount =0 ;
bucketscore = 0;
bucketFlag = false;
currBucketDate = beginDate;
newRec = true;
}

if (bucketFlag == true){
// get nextbucketdate if the rec has moved past previous bucket
// date
currBucketDate = getBucketDate(bucketDates, recdate);
bucketFlag = false;
}

//check if we still busy with the same record or have we moved on to the next one
//if(newRec){
//Debug.println(this, "do nothing because we'll be looking at the next record so we don't need to check date in bucket");
//}else{
if (isInBucket(bucketDates, recdate,currBucketDate)){
scoreCount++;
bucketscore += score;
}
else{
String cellKey = currBucketDate.toString();
Object cellVal = new Float(bucketscore/scoreCount);
reportRecord2.init(report.getSortedColumns());
reportRecord2.addCell(cellKey, cellVal);
//this line throws
Debug.println(reportRecord2, "Report 2: Adding new Cell to the report 2 record " );
scoreCount =0 ;
bucketscore = 0;
bucketFlag = true;
i--;//restore the previous record to re-capture TODO: Exercise when really bored - change to Recursion!
}
}
//}
}catch (Exception e) {
String msg = "Exception: " + e.getClass().getName() + ", MSG: "
+ e.getMessage();
Debug.println(this.getClass(), Debug.INFO, msg);
throw new ReportWizardActionException(msg);
}
Debug.println(this, "Report 2: " + modifiedReport);
Debug.println(this, "+++++++++++++++++++++= After generating report 2 ...." );

return modifiedReport;
}

/**
* Simple test to see if the recdate is in the current bucket
* @param bucketDates
* @param recdate
* @param currBucketDate
* @return
*/
private boolean isInBucket(ArrayList bucketDates, Date recdate, Date currBucketDate) throws Exception{
Date recBucketDate = getBucketDate(bucketDates,recdate);

if (recBucketDate.equals(currBucketDate)) {
return true;
}

return false;
}

/**
* Private method to return the START date of the particular bucket given the
* record date and the interval selected
*
* @param bucketDates
* @param recdate
* @param interval
* @return the start Date
*/
private Date getBucketDate(ArrayList bucketDates, Date recdate) throws Exception{
Date startdate = null;
Calendar recordcal = new GregorianCalendar();
Calendar bucketcal = new GregorianCalendar();

try{
Date currDate = null;
Iterator iter = bucketDates.iterator();

recordcal.setTime(recdate);
while (iter.hasNext()){
currDate = (Date)iter.next();
bucketcal.setTime(currDate);

/*Once we find a bucket start date that is less than this records date
* then return this as the new bucket start date
*/

if (recordcal.after(bucketcal) || recdate.equals(currDate)){
startdate = bucketcal.getTime();
continue;
}
}
}catch (Exception e) {
String msg = "Exception: " + e.getClass().getName() + ", MSG: "
+ e.getMessage();
Debug.println(this.getClass(), Debug.INFO, msg);
throw e;
}

return startdate;
}
}
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
No one can solve your problem , unless and until , you help yourself.. Can you post the line number which comes with exception message stating which part of code have problem !!


You defined hundreds of objects in above code, how can one compile and check which code have problem !
 
Shiva Kumar Shedad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Report post to moderator
Have you tried debugging it?

Though i have not looked at your code at all... i am sure that you will find it yourself if you try to use the debug feature of an IDE.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
tichakunda this forum is about SCJP. Please post your problem in Java Beginner or Java Intermediate section.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please Use One Thread Per Question. Three is really not necessary. this one will be quite enough.

Also Carefully Choose One Forum. This has nothing to do with SCJP.
 
    Bookmark Topic Watch Topic
  • New Topic