Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: passing java objects to ActiveMQ(JMS)

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default passing java objects to ActiveMQ(JMS)

    Hi,

    I'm new to this jms. i have written a program to pass java objects to the JMS but i'm getting error as Exception in thread "main" java.lang.NullPointerException, When i run producer.java programme. So please let me know whats wrong in my program.

    Here is the code..

    ******************************************
    TestPro.java
    ******************************************

    package com.test.msg;

    import java.io.Serializable;

    public class TestPro implements Serializable
    {
    private String msg;

    public String getMsg() {
    return msg;
    }

    public void setMsg(String msg) {
    this.msg = msg;
    }
    }

    **************************************
    producer.java
    ***************************************

    package com.test.producer;

    import javax.jms.*;

    import org.apache.activemq.ActiveMQConnectionFactory;

    import com.test.msg.TestPro;

    public class Producer {

    private static String url = "tcp://localhost:5003";
    public static Connection connection;
    private static String TEST_QUEUE = "TEST";

    public static MessageProducer producer;
    public Session session;
    String TEST_MESSAGE;

    public static void main(String[] args) throws JMSException
    {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
    connection = connectionFactory.createConnection();
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination destination = session.createQueue(TEST_QUEUE);
    producer = session.createProducer(destination);
    //TextMessage message = session.createTextMessage("Hello Rohith WelCome To ActiveMQ");
    Producer produce = new Producer();
    TestPro testPro = new TestPro();
    testPro.setMsg("Hello......");
    produce.sendData(testPro);

    }

    public void sendData(TestPro message) throws JMSException
    {
    ObjectMessage objectMessage = session.createObjectMessage();
    objectMessage.setObject(message);
    objectMessage.setJMSCorrelationID("ID:"+ TEST_MESSAGE);
    producer.send(objectMessage);
    System.out.println("Send Message" +(String)objectMessage.getObject());
    connection.close();
    }
    }
    *************************************************
    cunsumer.java
    ***********************************************

    package com.test.consumer;

    import javax.jms.*;

    import org.apache.activemq.ActiveMQConnectionFactory;

    public class Consumer {

    private static String url = "tcp://localhost:5003";
    public static Connection connection;
    private static String TEST_QUEUE = "TEST_QUEUE";

    public static MessageConsumer consumer;

    public static void main(String arg[]) throws JMSException
    {
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(TEST_QUEUE);
    consumer = session.createConsumer(destination);
    /*Message message = consumer.receive();

    if(message instanceof TextMessage)
    {
    TextMessage textMessage = (TextMessage)message;
    System.out.println("Received Message " + textMessage.getText());
    }*/
    Consumer consume = new Consumer();
    consume.onMessage(message)
    }

    public void onMessage(Message message) throws JMSException
    {
    ObjectMessage objectMessage = (ObjectMessage) message;
    System.out.println("Receive Message: " + (String)objectMessage.getObject());
    connection.close();
    }

    }
    ************************************************** ***
    In consuner.java how to make main method to access onMessage menthod... Please help........


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: passing java objects to ActiveMQ(JMS)

    getting error as Exception in thread "main" java.lang.NullPointerException
    The full text of the error message gives the line number where the exception occurred. Look at that line and find the variable that has a null value. Then backtrack in your code to find out why that variable does not have a valid non null value.
    If you can not tell which variable is null, use the println statement to print out the values of all the variables used on the line with the error.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. New to Java: Arry of Objects
    By Jack Hammered in forum Collections and Generics
    Replies: 8
    Last Post: January 5th, 2012, 07:17 AM
  2. Java help, passing/filtering a array
    By jack55655 in forum Collections and Generics
    Replies: 21
    Last Post: September 3rd, 2011, 11:43 AM
  3. java question on objects
    By joe98 in forum Threads
    Replies: 2
    Last Post: April 12th, 2011, 03:54 PM
  4. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 03:19 PM
  5. Passing objects as a constructor parameter
    By derky in forum Object Oriented Programming
    Replies: 2
    Last Post: October 27th, 2009, 04:31 AM