我正在使用ZK Charts在我的 ZK 框架中构建 HighCharts。在模型 Java 文件中,他们使用静态块在模型中加载硬编码值。例如
static {
model = new DefaultCategoryModel();
model.setValue("John", "Apples", new Integer(5));
model.setValue("John", "Oranges", new Integer(3));
model.setValue("John", "Pears", new Integer(4));
model.setValue("John", "Grapes", new Integer(7));
model.setValue("John", "Bananas", new Integer(2));
model.setValue("Jane", "Apples", new Integer(2));
model.setValue("Jane", "Oranges", new Integer(2));
model.setValue("Jane", "Pears", new Integer(3));
model.setValue("Jane", "Grapes", new Integer(2));
model.setValue("Jane", "Bananas", new Integer(1));
model.setValue("Joe", "Apples", new Integer(3));
model.setValue("Joe", "Oranges", new Integer(4));
model.setValue("Joe", "Pears", new Integer(4));
model.setValue("Joe", "Grapes", new Integer(2));
model.setValue("Joe", "Bananas", new Integer(5));
}
我的图表很好,但有默认值。现在我想让它动态化。我有几项服务可以为我提供库存水平的产品。这个想法是我想使用这些图表显示低库存警报。
我试图在静态块中调用这些服务,但它抛出空指针异常。
public class BarStackedData
{
private static CategoryModel model;
@Autowired
private static DefaultCustomCommerceCartService defaultCustomCommerceCartService;
@Autowired
private static ProductService productService;
static
{
model = new DefaultCategoryModel();
model.setValue(
"Low Stock",
"Puma T-Shirts",
new Long(defaultCustomCommerceCartService.getCustomAvailableStockLevel(productService.getProductForCode("30023506"),
null)));
model.setValue(
"Low Stock",
"Socks",
new Long(defaultCustomCommerceCartService.getCustomAvailableStockLevel(productService.getProductForCode("30023206"),
null)));
....... more code .....
}
public static CategoryModel getCategoryModel()
{
return model;
}
我知道静态块是一个静态初始化器。它将在加载类时执行。但是有没有办法让它动态或者我怎么能用动态值显示这对图表。
日志 :
SEVERE: Servlet.service() for servlet [zkLoader] in context with path [/backoffice] threw exception [Servlet java.lang.NullPointerException
at com.rfn.model.BarStackedData.<clinit>(BarStackedData.java:30)
at com.rfn.widgets.BarStackedComposerController.doAfterCompose(BarStackedComposerController.java:22)
at com.rfn.widgets.BarStackedComposerController.doAfterCompose(BarStackedComposerController.java:1)