1

I need to create a producer and consumer of IBM MQ using the binding file. The producer is working fine, but I am facing issues with the consumer. I am getting NullPointerException while consuming data from IBM MQ using binding file in java. Below is my code:

  try {


            String MYCF_LOOKUP_NAME = "file:C:/IBM MQ/JDA-client-jars/JDA-client-jars/JDA_TEST";

            Hashtable hashtableEnvironment = new Hashtable();

            hashtableEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,
                    "com.sun.jndi.fscontext.RefFSContextFactory");

            hashtableEnvironment
                    .put(
                            Context.PROVIDER_URL,
                            "file:C:/IBM MQ/JDA-client-jars/JDA-client-jars");

            InitialContext initialContext = new InitialContext(hashtableEnvironment);

            MQQueueConnectionFactory cf = (MQQueueConnectionFactory) initialContext
                    .lookup(MYCF_LOOKUP_NAME);      

            MQConnection connection = (MQConnection) cf.createConnection("SVCtestmq2","***********");

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

            MQQueue queue = (MQQueue) initialContext.lookup("TESTQ.FOR.JDA");      

            MQMessageConsumer consumer = (MQMessageConsumer) session.createConsumer(queue);
            Message str = consumer.receive(10000);        

            connection.close();
            initialContext.close();

        } catch (NamingException namingexception) {
            namingexception.printStackTrace();
        }

Below is the exception I am getting:

java.lang.NullPointerException
    at com.sams.logistics.jndi.jndiContext.HelloWorldMessageJNDI(jndiContext.java:79)
    at com.sams.logistics.jndi.jndiContext.main(jndiContext.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
JoshMc
  • 10,239
  • 2
  • 19
  • 38
  • Hi P.J.Meisch, Sorry it did not resolve my error. –  Jan 05 '20 at 02:13
  • did you debug your code and check at line 79 which variable is null? – P.J.Meisch Jan 05 '20 at 09:42
  • You issue is that in all your code, you are making the assumption that each invocation returning a connection, session, consumer, queue etc. is successful. You need to check that they are actually returned. This will let you know which line is returning the null. That will give an insight as to why and how to fix. – chughts Jan 06 '20 at 11:31
  • Hi @chughts, I added a try catch and debugged the code as well. I am getting an object at consumer. But str is coming as null. –  Jan 06 '20 at 15:38
  • Referring to the javadoc - https://www.ibm.com/support/knowledgecenter/SSFKSJ_9.1.0/com.ibm.mq.javadoc.doc/WMQJMSClasses/com/ibm/mq/jms/MQMessageConsumer.html#receive-long- : "This call blocks until a message arrives, the timeout expires, or this message consumer is closed. A timeout of zero never expires, and the call blocks indefinitely. ... Returns: ... null if the timeout expires ..." Which means that in your case the call expires after 10 seconds without finding a message. What you need to ask is why it found no message on the queue. Is your code actually looking at the right place? – chughts Jan 07 '20 at 11:00

0 Answers0