【问题标题】:Getting NullPointerException with row.setAttribute on Oracle ADF在 Oracle ADF 上使用 row.setAttribute 获取 NullPointerException
【发布时间】:2012-10-12 16:49:19
【问题描述】:

我现在正在尝试学习 Oracle ADF,但在 Java bean 上运行以下代码时遇到了 NullPointerException。 你能帮我弄清楚我做错了什么吗?这是从 JSPX 页面上的按钮调用的。

public String cb1_action() {
    try{
        BindingContext bindingctx = BindingContext.getCurrent();
        BindingContainer bindings = bindingctx.getCurrentBindingsEntry();
        DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
        DCIteratorBinding iter = bindingsImpl.findIteratorBinding("ViewObj1Iterator");


        Row row = iter.getCurrentRow();

        row.setAttribute("Id", 123);
        row.setAttribute("Nome", "Pedro Teste");

    }
    catch(Exception e) {
        System.out.println("Excepcao em: ");
        e.printStackTrace();
    }

    return null;
}

根据堆栈跟踪,错误发生在第一行。setAttribute() 行。 另外,我正在使用带有集成 WebLogic 服务器的最新版本的 JDeveloper。

最好的问候,

佩德罗

【问题讨论】:

  • row 可能是null(即getCurrentRow() 返回null)!
  • 调试看看你是否得到了 -> bindingsImpl.findIteratorBinding("ViewObj1Iterator");
  • 通常情况下,如果您做错了什么并且您需要检查您的想法是否存在问题。但这可能会导致系统设计不佳。

标签: java nullpointerexception weblogic jdeveloper oracle-adf


【解决方案1】:
Row row = iter.getCurrentRow();
if(row  != null){
    row.setAttribute("Id", 123);
    row.setAttribute("Nome", "Pedro Teste"); //name?
}

【讨论】:

  • 这将阻止异常被抛出,但它仍然不能解决我的问题......
【解决方案2】:

您收到错误的信息

row.setAttribute("Id", 123);

让我认为您尝试更改行的主键属性,这是不允许的。不确定这一点,因为你没有提到你得到的错误。

【讨论】:

  • 不.. 那不是问题。我试过没有主键(只是 ROWID),它也不能正常工作。不过谢谢你的提示。
【解决方案3】:

好的,这就是我想出如何解决这个问题的方法:

首先,我让 jDeveloper 为应用程序模块生成一个类。 在那个类中,我添加了以下方法:

public void testEntityObject()
{
    System.out.println("Let's try our Entity Object...");

    try
    {
        EntityDefImpl entity = TesteEOImpl.getDefinitionObject();
        TesteEOImpl ti = (TesteEOImpl)entity.createInstance2(getDBTransaction(), null);
        ti.setId(new BigDecimal(123));
        ti.setNome("Entity Object test...");

        getDBTransaction().commit();

        System.out.println("Looks good :-)");
    }
    catch(Exception e) 
    {
        System.out.println("It seems something went wrong :-(");
        e.printStackTrace();
    }
}

public void testViewObject() {
    System.out.println("Let's try our View Object...");

            ViewObjectImpl vo = this.getTeste1();

        try{
            Row row = vo.createRow();
            row.setAttribute("Id", 234);
            row.setAttribute("Nome", "VO test");
            vo.insertRow(row);
            getDBTransaction().commit();
            System.out.println("Looks good :-)")
        }
        catch(Exception e) {
            System.out.println("It seems something went wrong :-(");
            e.printStackTrace();
        }

    }

这些方法由连接到页面上两个按钮的托管 bean 调用。此托管 bean 具有以下方法。我将只发布其中一个,因为只有方法名称发生了变化:

public String cb1_action() {

    try{
    FacesContext fctx = FacesContext.getCurrentInstance(); 
    BindingContext bindingContext = BindingContext.getCurrent();
    DCDataControl dc = bindingContext.findDataControl("AppModuleAMDataControl");
    AppModuleAMImpl am = (AppModuleAMImpl)dc.getDataProvider();


    am.criarTesteComEntityObject();
    }
    catch(Exception e) {
        e.printStackTrace();
    }

    return null;
}

我知道这不是火箭科学或任何东西,但我花了一段时间才到达那里...... 基本上,您的回答对我调查发生的事情有很大帮助。原因?糟糕的设计! ADF应该是有组织的...... 谢谢大家! :D

【讨论】:

    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多