Click here to Skip to main content
15,896,478 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I'm using Eclipse and when I try to build the JMS example included in j2ee tutorial, it return a NullPointerException at this:
connection = connectionFactory.createConnection();

Here's the code of that example:

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.jms.JMSException;
import javax.annotation.Resource;

public class Producer {
    @Resource(lookup = "jms/ConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(lookup = "jms/Queue")
    private static Queue queue;
    @Resource(lookup = "jms/Topic")
    private static Topic topic;

    public static void main(String[] args) {
        final int NUM_MSGS;
        Connection connection = null;

        if ((args.length < 1) || (args.length > 2)) {
            System.err.println(
                    "Program takes one or two arguments: "
                    + "<dest_type> [<number-of-messages>]");
            System.exit(1);
        }

        String destType = args[0];
        System.out.println("Destination type is " + destType);

        if (!(destType.equals("queue") || destType.equals("topic"))) {
            System.err.println("Argument must be \"queue\" or " + "\"topic\"");
            System.exit(1);
        }

        if (args.length == 2) {
            NUM_MSGS = (new Integer(args[1])).intValue();
        } else {
            NUM_MSGS = 1;
        }

        Destination dest = null;

        try {
            if (destType.equals("queue")) {
                dest = (Destination) queue;
            } else {
                dest = (Destination) topic;
            }
        } catch (Exception e) {
            System.err.println("Error setting destination: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        try {
            connection = connectionFactory.createConnection();

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

            MessageProducer producer = session.createProducer(dest);
            TextMessage message = session.createTextMessage();

            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText(
                        "This is message " + (i + 1) + " from producer");
                System.out.println("Sending message: " + message.getText());
                producer.send(message);
            }

            producer.send(session.createMessage());
        } catch (JMSException e) {
            System.err.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}</number-of-messages></dest_type>


if I use jndi, it works fine. This example worked in Netbeans. Can anyone help me to get it work with Eclipse?
Posted
Updated 10-Nov-10 14:59pm
v2

It appears that you have not set up connectionFactory, which is why you get the NullPointerException when you try to use it. It's not straightforward, but start here[^] and go from there.

Peter
Vote for answers, and mark as accepted if they answer your question.
 
Share this answer
 
It appears that you have not set up connectionFactory, which is why you get the NullPointerException when you try to use it. It's not straightforward, but start here[^] and go from there.
 
Share this answer
 
Comments
[no name] 29-Aug-13 9:57am    
The question was asked and answered 3 years ago. There is no real need to keep answering it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900