title: send message to activemq in mvc spring give null pointer exception
body: <p>I am try to execute sample send and recive message to/from activemq by mvc spring But when I Run the project I get this error :</p>
  
  <pre><code>java.lang.NullPointerException
      com.sarf.jms.MessageProducerBean.sendMessage(MessageProducerBean.java:24)
      com.sarf.testactimq.HomeController.home(HomeController.java:42)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:497)
      org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
  </code></pre>
  
  <p>My source code  :</p>
  
  <p>MessageObject.java :</p>
  
  <pre><code>package com.sarf.data; 
  
  public class MessageObject {
    private String mailId;
    private String message;
  
   public MessageObject(){}; 
   public MessageObject(String mailId, String message) {
    super();
    this.mailId = mailId;
    this.message = message;
   }
   public String getMailId() {
    return mailId;
   }
   public void setMailId(String mailId) {
    this.mailId = mailId;
   }
   public String getMessage() {
    return message;
   }
   public void setMessage(String message) {
    this.message = message;
   }
  }
  </code></pre>
  
  <p>MessageConsumerBean.java :</p>
  
  <pre><code>package com.sarf.jms;
  
  import javax.jms.Destination;
  import javax.jms.JMSException;
  import javax.jms.MapMessage;
  
  import org.springframework.jms.core.JmsTemplate;
  import org.springframework.jms.support.JmsUtils;
  import com.sarf.data.MessageObject;
  
  public class MessageConsumerBean{
  
       private JmsTemplate jmsTemplate;
       private Destination destination;
  
       public void setJmsTemplate(JmsTemplate jmsTemplate) {
           this.jmsTemplate = jmsTemplate;
       }
       public void setDestination(Destination destination) {
           this.destination = destination;
       }
  
       public MessageObject receiveMessage() {
        MapMessage message = (MapMessage) jmsTemplate.receive(destination);
        try {
             MessageObject messageObj = new MessageObject();
             messageObj.setMailId(message.getString("mailId"));
             messageObj.setMessage(message.getString("message"));
             return messageObj;
             } catch (JMSException e) {
               throw JmsUtils.convertJmsAccessException(e);
             }
         }
      }
  </code></pre>
  
  <p>MessageProducerBean.java :</p>
  
  <pre><code>package com.sarf.jms;
  import javax.jms.Destination;
  import javax.jms.JMSException;
  import javax.jms.MapMessage;
  import javax.jms.Message;
  import javax.jms.Session;
  
  import org.springframework.jms.core.JmsTemplate;
  import org.springframework.jms.core.MessageCreator;
  import com.sarf.data.MessageObject;
  
  public class MessageProducerBean {
  
       //JMS Template object
       private JmsTemplate jmsTemplate;
       private Destination destination;
       public void setJmsTemplate(JmsTemplate jmsTemplate) {
           this.jmsTemplate = jmsTemplate;
       }
       public void setDestination(Destination destination) {
           this.destination = destination;
       }
       public void sendMessage(final MessageObject messageObj) {
        jmsTemplate.send(destination, new MessageCreator() {
        public Message createMessage(Session session) throws JMSException {
              MapMessage message = session.createMapMessage();
              message.setString("mailId", messageObj.getMailId());
              message.setString("message", messageObj.getMessage());
              return message;
             }
        }); //send method close here
       }//method ends here 
      }
  </code></pre>
  
  <p>Homecontroller :</p>
  
  <pre><code>package com.sarf.testactimq;
  
  import java.text.DateFormat;
  
  import java.util.Date;
  import java.util.Locale;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import com.sarf.data.MessageObject;
  import com.sarf.jms.MessageProducerBean;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import com.sarf.jms.*;
  
  /**
   * Handles requests for the application home page.
   */
  @Controller
  public class HomeController {
  
      private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  
      /**
       * Simply selects the home view to render by returning its name.
       */
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String home(Locale locale, Model model) {
          logger.info("Welcome home! The client locale is {}.", locale);
  
          Date date = new Date();
          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
  
          String formattedDate = dateFormat.format(date);
  
          model.addAttribute("serverTime", formattedDate );
           MessageProducerBean mp = new MessageProducerBean();
           mp.sendMessage(new MessageObject("1234", "Test Message"));
              /*
           MessageConsumerBean mc = new MessageConsumerBean();
                  MessageObject messageObj = mc.receiveMessage();
                  System.out.println("Message from " + 
                   messageObj.getMailId() + " received");
          */
          return "home";
      }
  
  }
  </code></pre>
  
  <p>servlete_context.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:beans="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
      &lt;annotation-driven /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
      &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  
               &lt;beans:bean id="connectionFactory"
          class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
          &lt;beans:property name="brokerURL" value="tcp://localhost:61616" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="mailDestination"
          class="org.apache.activemq.command.ActiveMQQueue"&gt;
          &lt;beans:constructor-arg value="mail.queue" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="jmsTemplate"
          class="org.springframework.jms.core.JmsTemplate"&gt;
          &lt;beans:property name="connectionFactory" ref="connectionFactory" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="producer"
          class="com.sarf.jms.MessageProducerBean"&gt;
          &lt;beans:property name="destination" ref="mailDestination" /&gt;
          &lt;beans:property name="jmsTemplate" ref="jmsTemplate" /&gt;
      &lt;/beans:bean&gt;
       &lt;beans:bean id="consumer"
          class="com.sarf.jms.MessageConsumerBean"&gt;
          &lt;beans:property name="destination" ref="mailDestination" /&gt;
          &lt;beans:property name="jmsTemplate" ref="jmsTemplate" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;context:component-scan base-package="com.sarf.testactimq" /&gt;
  
  
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>root_context.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
      &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
               &lt;bean id="connectionFactory"
          class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
          &lt;property name="brokerURL" value="tcp://localhost:61616" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="mailDestination"
          class="org.apache.activemq.command.ActiveMQQueue"&gt;
          &lt;constructor-arg value="mail.queue" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jmsTemplate"
          class="org.springframework.jms.core.JmsTemplate"&gt;
          &lt;property name="connectionFactory" ref="connectionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="producer"
          class="com.sarf.jms.MessageProducerBean"&gt;
          &lt;property name="destination" ref="mailDestination" /&gt;
          &lt;property name="jmsTemplate" ref="jmsTemplate" /&gt;
      &lt;/bean&gt;
       &lt;bean id="consumer"
          class="com.sarf.jms.MessageConsumerBean"&gt;
          &lt;property name="destination" ref="mailDestination" /&gt;
          &lt;property name="jmsTemplate" ref="jmsTemplate" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>pom.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.sarf&lt;/groupId&gt;
      &lt;artifactId&gt;testactimq&lt;/artifactId&gt;
      &lt;name&gt;testmessage&lt;/name&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0.0-BUILD-SNAPSHOT&lt;/version&gt;
      &lt;properties&gt;
          &lt;java-version&gt;1.6&lt;/java-version&gt;
          &lt;org.springframework-version&gt;3.1.1.RELEASE&lt;/org.springframework-version&gt;
          &lt;org.aspectj-version&gt;1.6.10&lt;/org.aspectj-version&gt;
          &lt;org.slf4j-version&gt;1.6.6&lt;/org.slf4j-version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
  
      &lt;dependency&gt;
        &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
        &lt;artifactId&gt;activemq-all&lt;/artifactId&gt;
        &lt;version&gt;5.13.3&lt;/version&gt;
     &lt;/dependency&gt;
          &lt;!-- Spring --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;!-- Exclude Commons Logging in favor of SLF4j --&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;commons-logging&lt;/groupId&gt;
                      &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
                   &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- AspectJ --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
              &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
              &lt;version&gt;${org.aspectj-version}&lt;/version&gt;
          &lt;/dependency&gt;   
  
          &lt;!-- Logging --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
              &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
              &lt;version&gt;${org.slf4j-version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.15&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;javax.mail&lt;/groupId&gt;
                      &lt;artifactId&gt;mail&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;javax.jms&lt;/groupId&gt;
                      &lt;artifactId&gt;jms&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;com.sun.jdmk&lt;/groupId&gt;
                      &lt;artifactId&gt;jmxtools&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;com.sun.jmx&lt;/groupId&gt;
                      &lt;artifactId&gt;jmxri&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- @Inject --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.inject&lt;/groupId&gt;
              &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
              &lt;version&gt;1&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Servlet --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.1&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Test --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;4.7&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;        
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-eclipse-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.9&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;additionalProjectnatures&gt;
                          &lt;projectnature&gt;org.springframework.ide.eclipse.core.springnature&lt;/projectnature&gt;
                      &lt;/additionalProjectnatures&gt;
                      &lt;additionalBuildcommands&gt;
                          &lt;buildcommand&gt;org.springframework.ide.eclipse.core.springbuilder&lt;/buildcommand&gt;
                      &lt;/additionalBuildcommands&gt;
                      &lt;downloadSources&gt;true&lt;/downloadSources&gt;
                      &lt;downloadJavadocs&gt;true&lt;/downloadJavadocs&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.5.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                      &lt;compilerArgument&gt;-Xlint:all&lt;/compilerArgument&gt;
                      &lt;showWarnings&gt;true&lt;/showWarnings&gt;
                      &lt;showDeprecation&gt;true&lt;/showDeprecation&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
                  &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;1.2.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;mainClass&gt;org.test.int1.Main&lt;/mainClass&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>the error in run time caused by call the methode sendMessage which define in MessageProducerBean  called in my controller , I run my activemq as image shown below :
  <a href="https://i.stack.imgur.com/T0MNM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T0MNM.png" alt="enter image description here"></a>
  Note :I am using spring suite tool with tc server and activemq version 5.13.3 
   so what wrong I am doing any help ? </p>