【问题标题】:Facing NullPointerException while using BIRT使用 BIRT 时遇到 NullPointerException
【发布时间】:2010-03-09 11:36:29
【问题描述】:

我在 java 程序中使用 BIRT API。我的代码是:

package com.tecnotree.mdx.product.utils;

import java.util.HashMap;
import java.util.logging.Level;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.ReportEngine;


public class ExampleReport {

 public static void main(String[] args) {
  // Variables used to control BIRT Engine instance

  ReportEngine eng = null;
  IReportRunnable design = null;
  IRunAndRenderTask task = null;
  HTMLRenderOption renderContext = null;
  HashMap contextMap = null;
  HTMLRenderOption options = null;
  final EngineConfig conf = new EngineConfig();
  conf
    .setEngineHome("C:\\birt-runtime-2_5_2\\birt-runtime-2_5_2\\ReportEngine");
  System.out.println("conf " + conf.getBIRTHome());

  conf.setLogConfig(null, Level.FINE);
  try {
   Platform.startup(conf);
  } catch (BirtException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }


  IReportEngineFactory factory = (IReportEngineFactory) Platform
    .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
  System.out.println("Factory : " + factory.toString());
  System.out.println(conf.toString());
  IReportEngine engine = factory.createReportEngine(conf);
  System.out.println("Engine : " + engine);

  try {
   design = eng
     .openReportDesign("C:\\birt-runtime-2_5_2\\birt-runtime-2_5_2\\ReportEngine\\samples\\hello_world.rptdesign");
  } catch (Exception e) {
   System.err
     .println("An error occured during the opening of the report file!");
   e.printStackTrace();
   System.exit(-1);
  }
  task = eng.createRunAndRenderTask(design);
  renderContext = new HTMLRenderOption();
  renderContext.setImageDirectory("image");
  contextMap = new HashMap();
  contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
    renderContext);
  task.setAppContext(contextMap);

  options = new HTMLRenderOption();
  options.setOutputFileName("c:/temp/output.html");
  options.setOutputFormat("html");
  task.setRenderOption(options);
  try {
   task.run();
  } catch (Exception e) {
   System.err.println("An error occured while running the report!");
   e.printStackTrace();
   System.exit(-1);
  }
  System.out.println("All went well. Closing program!");
  eng.destroy();

 }
}

但我在创建报告时面临 NullPointerException。

STACKTRACE :
Exception in thread "main" java.lang.NullPointerException
 at org.eclipse.birt.report.engine.api.impl.ReportEngine$EngineExtensionManager.<init>(ReportEngine.java:784)
 at org.eclipse.birt.report.engine.api.impl.ReportEngine.<init>(ReportEngine.java:109)
 at org.eclipse.birt.report.engine.api.impl.ReportEngineFactory$1.run(ReportEngineFactory.java:18)
 at org.eclipse.birt.report.engine.api.impl.ReportEngineFactory$1.run(ReportEngineFactory.java:1)
 at java.security.AccessController.doPrivileged(Native Method)
 at org.eclipse.birt.report.engine.api.impl.ReportEngineFactory.createReportEngine(ReportEngineFactory.java:14)
 at com.tecnotree.mdx.product.utils.ExampleReport.main(ExampleReport.java:47)

请帮我解决这个问题...我的项目截止日期已到...

感谢您的回复

提前致谢

【问题讨论】:

    标签: java nullpointerexception birt


    【解决方案1】:

    我遇到了类似的问题,并通过阅读 Report Engine API documentation page 摆脱了它。

    一些基本的东西是:

    • 放置在您的项目ReportEngine 文件夹中,您可以在 birt-runtime 下载中找到该文件夹​​:这是您处理 .rptdesign 文件并获取报告作为输出所需的全部引擎
    • 将 ReportEngine/lib 中的所有 jar 添加到 Java 构建路径
    • 在 ReportEngine/plugins/org.eclipse.birt.report.data.oda.jdbc_xxx 中添加必要的 jar 和驱动程序以连接到您的数据库(无需将它们添加到 Java 构建路径)

    生成 HTML 报告的示例代码如下:

    import java.util.HashMap;
    import java.util.logging.Level;
    import org.eclipse.birt.core.exception.BirtException;
    import org.eclipse.birt.core.framework.Platform;
    import org.eclipse.birt.report.engine.api.*;
    
    /**This class reads an .rptdesign file, 
     * and gives as output a report in HTML format 
     */
    public class BirtEngineDesktop {
    
        public static void main(String[] args) {
            // Variables used to control BIRT Engine instance
            String reportName = "first_report.rptdesign";
            IReportEngine engine = null;
            IReportRunnable design = null;
            IRunAndRenderTask task = null;
            HTMLRenderOption renderContext = null;
            HashMap contextMap = null;
            HTMLRenderOption options = null;
            final EngineConfig conf = new EngineConfig();
            conf.setEngineHome("ReportEngine");
            System.out.println("BIRT_HOME: " + conf.getBIRTHome());
            conf.setLogConfig(null, Level.FINE);
    
            try {
                Platform.startup(conf);
            } catch (BirtException e1) {
                e1.printStackTrace();
            }
    
            IReportEngineFactory factory = (IReportEngineFactory) Platform
                    .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
            System.out.println("Factory: " + factory.toString());
            System.out.println(conf.toString());
            engine = factory.createReportEngine(conf);
            System.out.println("Engine: " + engine);
    
            try {
                design = engine.openReportDesign("report/" + reportName); //report is needed in this folder
            } catch (Exception e) {
                System.err.println("An error occured during the opening of the report file!");
                e.printStackTrace();
                System.exit(-1);
            }
            task = engine.createRunAndRenderTask(design);
            renderContext = new HTMLRenderOption();
            renderContext.setImageDirectory("images");
            contextMap = new HashMap();
            contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT,
                    renderContext);
            task.setAppContext(contextMap);
    
            //Set parameter values and validate, if the report requires it
            //task.setParameterValue("Years", 2.0);
            //task.validateParameters();
    
            options = new HTMLRenderOption();
            options.setOutputFileName("output/" + reportName + ".html"); //output HTML will go to this folder
            options.setOutputFormat("html");
            task.setRenderOption(options);
            try {
                task.run();
            } catch (Exception e) {
                System.err.println("An error occured while running the report!");
                e.printStackTrace();
                System.exit(-1);
            }
            System.out.println("All went well. Closing program!");
            engine.destroy();
            System.exit(0);
        }
    }
    

    【讨论】:

      【解决方案2】:

      不确定这是否会有所帮助,但您的问题是:

      IReportEngine engine = factory.createReportEngine(conf);
      

      我们不再为 BIRT 编写自己的 Web 应用程序(我们使用公司另一部分提供的 Web 应用程序),但通过挖掘我们的旧代码发现我们拥有的和您拥有的之间有两个不同之处。无论其中一个是修复程序,您都必须自己检查。全心全意,不负责任:-)

      区别在于:

      在设置日志配置引擎主页后但在启动平台之前,我们还设置了一个 HTML 发射器和平台上下文:

      HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
      emitterConfig.setActionHandler (new HTMLActionHandler());
      HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
      emitterConfig.setImageHandler (imageHandler);
      conf.getEmitterConfigs().put ("html", emitterConfig);
      
      IPlatformContext context = new PlatformServletContext( svrContext );
      conf.setPlatformContext( context );
      

      请记住,我们是在 servlet 中运行的,而不是作为独立应用程序的一部分。所以你可能需要:

      IPlatformContext context = new PlatformFileContext( svrContext );
      

      用于平台上下文。试一试,看看它是否有效。不知何故,我怀疑它,因为 PlatformFileContext 是默认值。不过,发射器可能需要考虑。

      我可以建议的唯一其他可能性是实际获取source code for BIRT (your particular build) 并查看堆栈跟踪中的违规行。希望您能够找出可能导致问题的参数。

      例如,最后一行 ReportEngine.java:784 是:

      void cacheOpenedDocument( ReportDocumentReader document ) {
          synchronized ( openedDocuments ) {
              LinkedEntry<ReportDocumentReader> entry
                  = openedDocuments.add( document );
              document.setEngineCacheEntry( entry ); // << line 784
          }
      }
      

      所以几乎可以肯定传入的document 为空。您需要通过各个层来跟踪它,以尝试找出正在发生的事情。

      这可能很困难,在这种情况下,您最好只使用raising a bug report 并让专家处理它。或者直接找 Jason Weathersby 麻烦,如果你能得到他的电子邮件地址你肮脏的小手:-)


      顺便说一句,您的路径中不需要那些可怕的转义 \ 字符。 Java在处理(例如)方面非常好:

      conf.setEngineHome("C:/birt-runtime-2_5_2/ReportEngine");
      

      【讨论】:

        【解决方案3】:

        我知道这个问题已经很老了,但是我今天遇到了这个问题,我花了很长时间才弄清楚原因是什么。原因是我在类路径中包含了一些 birt / eclipse 依赖项。无论出于何种原因,这都会导致这个奇怪的错误。我正在使用 Maven 并包含几个自定义发射器。我的解决方案是在我的自定义发射器 pom 中将我的 Eclipse 依赖项标记为“已提供”。这使所有的 Eclipse 杂物无法进入类路径,而是依靠 BIRT 的 OSGI 加载来处理所有这些。

        【讨论】:

          【解决方案4】:

          我也遇到过这个问题

          IReportEngine engine = factory.createReportEngine(conf);
          //was null everytime.
          

          我做了所有“偏蓝”描述的事情,但错误仍然存​​在。 我通过从here. 下载并重新安装 ReportEngine 解决了这个问题

          就我而言,我的插件中不存在配置文件夹。将配置文件夹添加到我的插件后,一切都很好。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-11-06
            • 2015-07-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-08
            • 2017-02-08
            • 1970-01-01
            相关资源
            最近更新 更多