Unable to follow J6EE wiki - NullPointerException

Hello To All of You,

I am trying to follow this tutorial on the
vaadin wiki
for creating a Vaadin JEE6 application. I am going for Option 2 here and watched the
videocast
mentioned at the end of the page.

I followed the steps exactly as described in the video. I also have netbeans (6.9.1) and I am using GlassFish v 3.1. The app is deployed without a problem. But as soon as I hit the Button
Say Hello
Glassfish shows the following error:


INFO: Portable JNDI names for EJB HelloBean : [java:global/Vaadin2/HelloBean, java:global/Vaadin2/HelloBean!com.example.vaadin.services.HelloBean]

INFO: WEB0671: Loading application [Vaadin2]
 at 
[/Vaadin2]
INFO: Vaadin2 wurde erfolgreich bereitgestellt in 1.687 Millisekunden.
SCHWERWIEGEND: =================================================================
Vaadin is running in DEBUG MODE.
Add productionMode=true to web.xml to disable debug features.
To show debug window, add ?debug to your application URL.
=================================================================
SCHWERWIEGEND: com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.NullPointerException
        at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
        at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1107)
        at com.vaadin.ui.Button.fireClick(Button.java:341)
        at com.vaadin.ui.Button.changeVariables(Button.java:177)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1060)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:561)
        at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:260)
        at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:438)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
        at java.lang.Thread.run(Thread.java:717)
Caused by: java.lang.NullPointerException
        at com.example.vaadin.Vaadin2$1.buttonClick(Vaadin2.java:40)
        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:613)
        at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:487)
        ... 34 more

I don’t know what I am doing wrong. Can someone please help me here?

This is my Vaadin-Class:


package com.example.vaadin;           

import com.example.vaadin.services.HelloBean;
import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.data.*;
import com.vaadin.ui.Button.ClickEvent;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
/** 
 *
 * @author 
 * @version 
 */
@SessionScoped
public class Vaadin2 extends Application {

    @EJB
    HelloBean helloBean;

    @Override
    public void init() {
	Window mainWindow = new Window("Vaadin2");

        Label label = new Label("Hello Vaadin user");
        final TextField tf = new TextField();
        final Button bu = new Button("Say Hello");
        final Label msg = new Label();

        bu.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                msg.setValue(helloBean.sayHello(tf.getValue().toString()));
            }
        });

        mainWindow.addComponent(label);
	mainWindow.addComponent(tf);
	mainWindow.addComponent(bu);
	mainWindow.addComponent(msg);


        setMainWindow(mainWindow);

    }

}

This is my Servlet:


package com.example.vaadin;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author
 */
@WebServlet(urlPatterns="/*")
public class Vaadin2Servlet extends AbstractApplicationServlet {

    @Inject
    Instance<Vaadin2> application;

    @Override
    protected Application getNewApplication(HttpServletRequest request) throws ServletException {
        return application.get();
    }

    @Override
    protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
        return Vaadin2.class;
    }

}

And thats my SessionBean:


package com.example.vaadin.services;

import javax.ejb.Stateless;

/**
 *
 * @author
 */
@Stateless
public class HelloBean {

    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
 
}

Thanks
Kurt

No one?

I tried the whole day but I can not find out what’s wrong.

Maybe someone can send me a complete war-file, with such an example (which is working on his or her’s application server) which I then can deploy on my server. This way I might find out if my GlassFish has a wrong configuration.

I hate bagging. But please try to help me.

Thanks.

Hi Kurt!

I think you need to implement @Local interface in your Bean or use annotation @LocalBean.


@Local
public interface LocalHello{
        public String sayHello(String name);
}

and


@Stateless
public class HelloBean impements LocalHello{
        public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}

usage



@SessionScoped
public class Vaadin2 extends Application {

    @EJB
    LocalHello helloBean;

or


@Stateless
@LocalBean
public class HelloBean {
        public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}

Andris,

thank you very much for your answer. I really appreciate it.

I tried it with the Local Interface as you said. But still I get the same error.

But I found something else out:

When I go for Option 1 in the
wiki
(Using Stateful Session) I get the same error.

BUT

when I delete the generated web.xml file (I am using the Vaadin Plugin for NetBeans) the thing works!!!

So I deleted the generated web.xml file in my Context and Dependency Example (Option 2 in the wiki) as well. But when doing this my browser is showing me a blank page (no error message). When I hit reload I get a HTTP Status 500


HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException

root cause

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1 logs.

So I guess this has something to do with GlassFish. But I can not find out what it is.

I am sure this can have a thousand reasons. However any help (even wild guesses) is really appreciated.

Kurt

It’s easy. Download
Example

That archive contains full example(2 way) how to use EJB and Vaadin UI

I used Netbeans 7.0 and Glassfish 3.1

Andris,

you are my hero!!! I really mean it.

Your example works like a charm. Both the @EJB Annotation and the Standard Lookup. I downloaded it, unpacked it, deployed it and it worked!

I still haven’t found out what is wrong with my code. But now I know for sure that I am doing something wrong and that it is not GlassFish. As soon as I have found out I will post it here.

I can’t say thank you enough for all the effort you put in this.

All the best
Kurt

Ok, I found out what was wrong. I don’t even want to say it as what I did is a bit stupid. Anyways here we go:

I took the wiki a little bit to literally. It says to create an empty beans.xml file. So I copied the given code in the wiki and pasted it into my file…

#!xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- Namespace declarations omitted -->
</beans>

…and never thought about it.

While investigating Andris example and comparing it with mine I saw no difference. But then I checked the beans.xml file and saw my mistake. After altering it to:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

my example from above works.

Sorry for wasting your time and thanks again to Andris who’s help was very useful for me.

Kurt

Hi Kurt! I am glad that I helped you :slight_smile:

Oh yes, this empty beans.xml seems to be a common trap.
Did personally step on it several times being aware on this, both cost me a 1/2 hr of “wtf” brain overloading :slight_smile:

@Dimitri

Glad I am not the only one. Although it took me much, much longer and the excellent help from Andris till I found out. I would have been so glad if it had take only 1/2 hr. :smiley:

I think it took me longer than you to solve this problem… I had stopped working on my project for weeks
and today I finally found the solution. The Wiki entry should definitley be changed to include the correct beans.xml :wink:

Made my Day!

Sorry that you have had troubles with the wiki page. Apparently we’re having problems with the urls within the namespace declarations - extraneous square brackets is inserted in the page, even if none have been written. I guess that was the original reason to omit the namespace declarations (I’m not the original author).

I’ll see what we can do about this, but I’m not making any promises :frowning:

Hi, Andris!

This file not avaible now. :frowning:
Can you repeat upload your example?

Thank you. Best regards!

Hi Yuriy!
New example url