Menu

NullPointerException by translate

2016-05-17
2016-05-20
  • Dmitry Kras

    Dmitry Kras - 2016-05-17

    Hi!

    I'm getting NullPointerException by creating a button:

    MessageBox.createError().withCaption(caption).withHtmlMessage(msg).withOkButton().open();

    java.lang.NullPointerException
    at de.steinwedel.messagebox.i18n.ButtonCaptionFactory.translate(ButtonCaptionFactory.java:39)
    at de.steinwedel.messagebox.MessageBox.withButton(MessageBox.java:601)
    at de.steinwedel.messagebox.MessageBox.withOkButton(MessageBox.java:633)
    at de.steinwedel.messagebox.MessageBox.withOkButton(MessageBox.java:621)

    As I cann see, you are trying to get Locale from Session:

    Object value = VaadinService.getCurrentRequest().getWrappedSession().getAttribute(LANGUAGE_SESSION_KEY);

    but in my case VaadinService.getCurrentRequest() is Null, because I want to show Message from Thread.

    Is it possible to change this code or at least add a check?

     
  • Dieter Steinwedel

    I think, moving the "try {" from line 45 to line 35 in the class ButtonCaptionFactory should solve your problem. If you have a better idea, tell me. I will build a new release after you have answered.

     
  • Dmitry Kras

    Dmitry Kras - 2016-05-19

    This will help to solve the NullPointer problem, but will not help to initialize the language.
    I'm not a Vaadin expert, but why not to use VaadinSession.getCurrent() instead of VaadinService.getCurrentRequest().getWrappedSession()?

    Actually I'm not sure if the Problem is not in my code, probably, you can give me a hint?

    I'm starting my thread using UI.getCurrent().access:

    UI.getCurrent().access(new Runnable() {
    
        @Override
        public void run() {
            final TaskRunWorkThread thread = new TaskRunWorkThread();
            thread.start();
            UI.getCurrent().setPollInterval(500);
        }
    });
    

    According the documentation, VaadinService.getCurrentRequest() should not be null in the child thread, but not in my case (in the main thread it is not null). At the same time VaadinSession.getCurrent() is not null in both threads.

     
  • Dieter Steinwedel

    You are right, using VaadinSession.getCurrent() is better. I will create a new release at weekend.

    BTW, https://vaadin.com/api/7.5.0/com/vaadin/ui/UI.html#access%28java.lang.Runnable%29 specifies, that VaadinService.getCurrentRequest() is undefined inside the Runnable. Therefore you get the NPE.

     
  • Dieter Steinwedel

    New version is released. Can you give me a feedback?

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.