-1

I am trying to use primefaces drag and drop on a UIComponent but the DragDropEvent returns NullPointerException. What am I probably doing wrong.

@Named(value = "dynaComponentController")
@RequestScoped

public class DynaComponentController {

private UIComponent component = new HtmlPanelGroup();
private boolean checker = false;


public void onComponentDrop(DragDropEvent event) {
    component = event.getComponent();
    int i = component.getChildCount();
    if(i > 0){
        checker = true;
    }
}

My View is

<h:form prependId="false">
        <h:panelGroup id="drop" layout="block" style="height:150px; width:300px;" styleClass="ui-widget-content">
            <p> Drop Here </p>
            <p:droppable for="drop" onDrop="#{dynaComponentController.onComponentDrop(event)}"/>
        </h:panelGroup>

        <br />

        <h:panelGroup id="drag" layout="block" style="height:150px;width:300px;" binding="#{dynaComponentController.component}">
            <h:outputLabel value="Drag Me" rendered="#{dynaComponentController.checker == false}"/>
            <h:panelGroup rendered="#{dynaComponentController.checker == true}">
                <h:inputTextarea id="complainID" />
            </h:panelGroup>
            <p:draggable for="drag"/>
        </h:panelGroup>

Stacktrace

java.lang.NullPointerException

at org.morph.bean.DynaComponentController.onComponentDrop(DynaComponentController.java:38)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at javax.el.ELUtil.invokeMethod(ELUtil.java:308)

at javax.el.BeanELResolver.invoke(BeanELResolver.ja

Please the idea to drag a panel onto another panel using primefaces drag and drop feature. On-drop, I want the inputTextArea to be displayed. Please if there is a better way to go about this I will appreciate it. Thanks.

Cocoa Butter
  • 265
  • 2
  • 4
  • 13

1 Answers1

2

Simple. onDrop expects a client-side callback. You can only use a JavaScript function here. Placing a Bean method here will just call it (with an undefined "event", thus null) just the same as if you would have placed #{dynaComponentController.onComponentDrop(event)} outside of a tag.

You might consider calling a bean method via a p:remoteCommand and wrap that call in a javascript function you call from onDrop.

Syren Baran
  • 444
  • 2
  • 8
  • Thank. Please do you have any recommendation for the latter part of the question.(display inputtextarea ondrop) – Cocoa Butter Apr 15 '15 at 21:16
  • Hmm, why dont you just handle the event completely on client side? E.g. remove the `rendered` attribute and add a `style="display:none"` and an id instead. Then just do something like `onDrop="$('#theid').css('display','inline');"` . – Syren Baran Apr 16 '15 at 07:21