简体   繁体   English

使用BIRT时面临NullPointerException

[英]Facing NullPointerException while using BIRT

I am using BIRT APIs in a java program.My code is : 我在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();

 }
}

But i am facing NullPointerException while creating the report. 但是我在创建报告时面临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)

Please help me regarding this... my project deadline has been reached... 请就此帮我...我的项目期限已到...

Appreciate your Reply 感谢您的回覆

Thanks in Advance 提前致谢

I faced a similar problem and got out of it reading Report Engine API documentation page . 我遇到了类似的问题,但在阅读Report Engine API文档页面后就摆脱了它。

Some fundamental things are: 一些基本的事情是:

  • place in your project ReportEngine folder , that you can find inside birt-runtime download: it's all the engine that you need to process .rptdesign files and get the report as output 放置在项目的ReportEngine文件夹中 ,您可以在birt运行时下载中找到它:这是处理.rptdesign文件并获取报告作为输出所需要的所有引擎
  • add to the Java build path all the jars in ReportEngine/lib 将ReportEngine / lib中的所有jar添加到Java构建路径
  • add the necessary jars with drivers to connect to your DBs in ReportEngine/plugins/org.eclipse.birt.report.data.oda.jdbc_xxx (no need to add them to the Java build path) 在ReportEngine / plugins / org.eclipse.birt.report.data.oda.jdbc_xxx中添加带有驱动程序的必要jar,以连接到您的数据库(无需将其添加到Java构建路径)

A sample code to produce an HTML report is: 生成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);
    }
}

Not sure if this will help but your offending line is: 不知道这是否有帮助,但是您的冒犯行是:

IReportEngine engine = factory.createReportEngine(conf);

We no longer code our own web applications for BIRT (we use a web application provided by another part of the company) but digging through our old code found two differences between what we had and what you have. 我们不再为BIRT编写我们自己的Web应用程序(我们使用公司另一部分提供的Web应用程序),但是深入研究我们的旧代码发现,我们拥有的东西与您拥有的东西有两个区别。 Whether one of them is a fix, you'll have to check for yourself. 其中之一是否是修复程序,您必须自己检查。 All care, no responsibility :-) 一切照料,没有责任:-)

The differences are: 不同之处在于:

After setting the log config engine home but before starting the platform, we also set up an HTML emitter and the platform context: 在设置日志配置引擎主目录之后但在启动平台之前,我们还设置了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 );

Keep in mind we were running in a servlet, not as part of a standalone application. 请记住,我们在servlet中运行,而不是作为独立应用程序的一部分运行。 So you'll probably need: 因此,您可能需要:

IPlatformContext context = new PlatformFileContext( svrContext );

for the platform context. 用于平台上下文。 Give that a shot and see if it works. 试一试,看看是否有效。 Somehow I doubt it since PlatformFileContext is the default. 某种程度上我对此表示怀疑,因为PlatformFileContext是默认设置。 The emitter might be something to consider though. 发射器也许是值得考虑的东西。

The only other possibility I can suggest is to actually get the source code for BIRT (your particular build) and have a look at the offending lines in the stack trace. 我建议的唯一其他可能性是实际获取BIRT源代码(您的特定内部版本) ,并查看堆栈跟踪中有问题的行。 You should hopefully be able to figure out which parameter may be causing the problem. 您应该应该能够找出可能引起问题的参数。

For example, the final line ReportEngine.java:784 is part of: 例如,最后一行ReportEngine.java:784是以下内容的一部分:

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

so it's almost certainly that the document passed in was null. 因此几乎可以肯定的是,传入的document为空。 You'll need to follow that back through the various layers to try and find out what's happening. 您需要在各个层次上都遵循该步骤,以尝试找出正在发生的事情。

This may well be difficult in which case you may be better off just raising a bug report and letting the experts handle it. 这可能很困难,在这种情况下,您最好只提出一个错误报告并让专家进行处理,否则会更好。 Or hassle Jason Weathersby directly if you can get your grubby little hands on his email address :-) 如果您可以将肮脏的小手放在他的电子邮件地址中,也可以直接给Jason Weathersby麻烦:-)


As an aside, you don't need those hideous escaped \\ characters in your paths. 顺便说一句,您不需要在路径中使用那些令人讨厌的转义\\字符。 Java is perfectly fine in dealing with (for example): Java非常适合处理(例如):

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

I also faced this problem that 我也面对这个问题

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

I did everything that "bluish" described but the error was still present. 我做了“蓝色”描述的所有操作,但错误仍然存​​在。 I solved this problem by downloading and reinstalling the ReportEngine from here. 我通过从此处下载并重新安装ReportEngine解决了此问题

In my case, the configuration folder was not present in my plugin. 就我而言,插件中不存在配置文件夹。 After adding the configuration folder to my plugin, everything was fine. 将配置文件夹添加到我的插件后,一切都很好。

I know this question is pretty old, but I ran into this issue today and it took me a long time to figure out what the cause was. 我知道这个问题已经很老了,但是今天我遇到了这个问题,花了我很长时间才弄清楚是什么原因。 The cause was I was including some birt / eclipse dependencies on my classpath. 原因是我在类路径中包含了一些birt / eclipse依赖项。 For whatever reason, this causes this strange error. 无论出于何种原因,这都会导致此奇怪的错误。 I am using maven and include a couple of custom emitters. 我正在使用Maven,并包括几个自定义发射器。 My solution was to mark my eclipse dependencies as "provided" in my custom emitter poms. 我的解决方案是在我的自定义发射器poms中将Eclipse依赖项标记为“提供”。 This kept all the eclipse cruft from getting into the classpath, instead relying on BIRT's OSGI loading to handle all that. 这样就避免了所有的日食残渣进入类路径,而是依靠BIRT的OSGI加载来处理所有这些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM