Search code examples
javanullpointerexceptionweblogicjdeveloperoracle-adf

Getting NullPointerException with row.setAttribute on Oracle ADF


I'm now trying to learn Oracle ADF and I'm getting a NullPointerException while running the following code on a Java bean. Can you help me figure out what I'm doing wrong please? This is being invoked from a button on a JSPX page.

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;
}

According to the Stack trace, the error occurs on the first row.setAttribute() line. Also, I'm using the latest version of JDeveloper with the integrated WebLogic server.

Best regards,

Pedro


Solution

  • Ok, so here's how I figured out how to get around this:

    First, I asked jDeveloper to generate a class for the Application Module. In that class, I added the following methods:

    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();
            }
    
        }
    

    These methods are being called by a managed bean that is connected to two buttons on the page. This managed bean has the following methods. I'll post just one of them as only the method names change:

    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;
    }
    

    I know this is not rocket science or anything but it took a while for me to get there... Basically, your answers helped me a lot to go and investigate what was happening. The cause? Poor design! ADF is supposed to be organized... Thank you everyone! :D