以下のメソッドで 2 Date try/catch ブロックに問題があります。このメソッドをヒットすると、null ポインター例外が発生します。
private List<HashMap<String,Object>> getOrderHistoryData(final HttpServletRequest request)
{
List<HashMap<String,Object>> productInfo = new ArrayList<HashMap<String,Object>>();
JsCustomerSession sess = (JsCustomerSession) getRequestHelper().getCustomerSession(request);
JsCustomer customer = (JsCustomer) sess.getShopper().getCustomer();
ExtStore store = (ExtStore) sess.getShopper().getCurrentShoppingCart().getStore();
RemoteOrderHistoryService orderHistoryService = remoteServiceLocator.getOrderHistoryService(store);
DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH);
Calendar cal = Calendar.getInstance();
String defaultEnd = formatter.format(cal.getTime());
log.debug("Your default end string is: " + defaultEnd);
cal.add(Calendar.DATE, -30);
String defaultStart = formatter.format(cal.getTime());
log.debug("Your defaultStart string is: " + defaultStart);
String startStr = ServletRequestUtils.getStringParameter(request, "start", defaultStart);
String endStr = ServletRequestUtils.getStringParameter(request, "end", defaultEnd);
log.debug("Your start string is: " + startStr);
Date end;
try {
end = formatter.parse(endStr);
} catch (ParseException e) {
e.printStackTrace();
end = new Date();
}
String searchString = ServletRequestUtils.getStringParameter(request, "searchString", "").trim();
String show = ServletRequestUtils.getStringParameter(request, "show", "");
String searchType = ServletRequestUtils.getStringParameter(request, "search-filter", "");
if(startStr.equals(defaultStart))
{
if(searchString != "")
{
Calendar newStart = Calendar.getInstance();
newStart.add(Calendar.YEAR, -25);
startStr = formatter.format(newStart.getTime());
}
else
{
startStr = defaultStart;
}
}
if (show == "")
{
show = "all";
}
if(store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR) || store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE))
{
searchString += "show: " + show + "type: " + searchType;
}
Date start;
try {
start = formatter.parse( startStr );
} catch (ParseException e) {
e.printStackTrace();
start = new Date();
}
log.debug("SEARCHING FROM " + start + " TO " + end);
log.debug("SEARCHING FOR " + searchString);
log.debug("SEARCHING TYPE " + searchType);
List<OrderHistoryBean> ohs = orderHistoryService.getOrderHistory(customer, start, end, searchString);
HashMap<String,Object> excelRowData = new HashMap<String,Object>();
if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_ECLIPSE))
{
for(OrderHistoryBean orderHistory : ohs)
{
String shipToFirstName = orderHistory.getShipTo().getFirstName().toString();
String shipToLastName = orderHistory.getShipTo().getLastName().toString();
String shipToFirstAndLastName = shipToFirstName + "" + shipToLastName;
excelRowData.put("Order#", orderHistory.getReferenceNumber());
excelRowData.put("PO#", orderHistory.getPoNumber());
excelRowData.put("Ordered", orderHistory.getPostedDate());
excelRowData.put("Shipped", orderHistory.getShippedDate());
excelRowData.put("Status", orderHistory.getStatus());
excelRowData.put("Ship To", shipToFirstAndLastName);
excelRowData.put("Amount", orderHistory.getAmount());
productInfo.add(excelRowData);
}
}
else if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE))
{
for(OrderHistoryBean orderHistory : ohs)
{
excelRowData.put("Reference#", orderHistory.getOrderNumber());
excelRowData.put("Posted Date", orderHistory.getPostedDate());
excelRowData.put("PO/Job#", orderHistory.getPoNumber());
excelRowData.put("Amount", orderHistory.getAmount());
productInfo.add(excelRowData);
}
}
else if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR))
{
for(OrderHistoryBean orderHistory : ohs)
{
excelRowData.put("Order Number", orderHistory.getOrderNumber());
excelRowData.put("PO Number", orderHistory.getPoNumber());
excelRowData.put("Order Date", orderHistory.getPostedDate());
excelRowData.put("Status", orderHistory.getStatus());
excelRowData.put("Order Total", orderHistory.getAmount());
productInfo.add(excelRowData);
}
}
else if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_NRT))
{
for(OrderHistoryBean orderHistory : ohs)
{
excelRowData.put("Order Number", orderHistory.getOrderNumber());
excelRowData.put("Store", store);
excelRowData.put("Order Date", orderHistory.getPostedDate());
excelRowData.put("Order Total", orderHistory.getAmount());
productInfo.add(excelRowData);
}
}
return productInfo;
}
また、基本的に同じコードが私の Groovy クラスでも問題なく動作します。
private ModelAndView performHistorySearch(final HttpServletRequest request){
JsCustomerSession sess = (JsCustomerSession) getRequestHelper().getCustomerSession(request);
JsCustomer customer = sess.getShopper().getCustomer();
ExtStore store = (ExtStore) sess.getShopper().getCurrentShoppingCart().getStore();
RemoteOrderHistoryService orderHistoryService = remoteServiceLocator.getOrderHistoryService(store);
Map<String, Object> model = new HashMap<String, Object>();
DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
Calendar cal = Calendar.getInstance();
String defaultEnd = formatter.format(cal.getTime());
cal.add(Calendar.DATE, -30);
String defaultStart = formatter.format(cal.getTime());
String startStr = ServletRequestUtils.getStringParameter(request, "start");
Date end = formatter.parse( ServletRequestUtils.getStringParameter(request, "end", defaultEnd) );
String searchString = ServletRequestUtils.getStringParameter(request, "searchString", "").trim();
String show = ServletRequestUtils.getStringParameter(request, "show", "");
String searchType = ServletRequestUtils.getStringParameter(request, "search-filter", "");
if(startStr == null) {
if(searchString != "") {
Calendar newStart = Calendar.getInstance();
newStart.add(Calendar.YEAR, -25);
startStr = formatter.format(newStart.getTime());
} else {
startStr = defaultStart;
}
}
if (show == ""){
show = 'all';
}
if(store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR) || store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE)){
searchString += 'show: ' + show + "type: " + searchType;
}
Date start = formatter.parse( startStr );
log.debug("SEARCHING FROM " + start + ' TO ' + end);
log.debug("SEARCHING FOR " + searchString);
log.debug("SEARCHING TYPE " + searchType);
List<OrderHistoryBean> ohs = orderHistoryService.getOrderHistory(customer, start, end, searchString);
if (store.getPosType().equals(ExtStoreImpl.POS_TYPE_INFOR) || store.getPosType().equals(ExtStoreImpl.POS_TYPE_DATABASE)){
model.put("orders", ohs);
} else {
List<OrderHistoryBean> returnOhs = new ArrayList<OrderHistoryBean>();
ohs.each({oh ->
if(oh.getType() != 'Payment') {
if(show == 'all') {
returnOhs.add(oh);
log.debug('adding ' + oh.getOrderNumber());
} else if (show == 'invoice' && oh.getType() == 'Invoice') {
returnOhs.add(oh);
log.debug('adding ' + oh.getOrderNumber());
} else if (show == 'open' && oh.getType() != 'Invoice') {
returnOhs.add(oh);
log.debug('adding ' + oh.getOrderNumber());
}
}
})
log.debug(returnOhs.size());
model.put("orders", returnOhs);
}
return new ModelAndView(this.getSuccessView(), model);
}
私は立ち往生しています、どんな助けもいただければ幸いです。