-1

Am trying to run a simple JMS app, sadly got NullPointException . following are the links to the files of the app :

1. Producer.java

2. SynchConsumer.java

Tools used : Eclipse & GlassFish

following is the exception

Exception in thread "main" java.lang.NullPointerException
at coreservlets.Producer.main(Producer.java:96)

Any flash put into this shall be appreciated .

Rehme
  • 323
  • 3
  • 6
  • 20
  • why did my question get downvoted ????? – Rehme Aug 24 '13 at 09:28
  • No idea - might be because you linked to the code instead of posting it. Anyway, looks like your DI configuration is not being picked up. – mikołak Aug 24 '13 at 09:30
  • I answered just because I have 5 minutes, but you get downvoted because your question is very bad asked. – Gianmarco Aug 24 '13 at 09:33
  • It sounds guys downvoting my question hold a lot against me or something :D – Rehme Aug 24 '13 at 09:35
  • Downvotes are not personal, they are directed at the question itself. People have probably downvoted this question because it is unclear and incomplete. You haven't even included your code as part of the question. – Cody Gray - on strike Aug 24 '13 at 09:39
  • Backed up my question with links to the code . did not want my question to look like an article . – Rehme Aug 24 '13 at 09:46
  • Try this answer: [SO answer how to setup JMS client in standalone][1] [1]: http://stackoverflow.com/a/32568038/912829 – ACV Sep 14 '15 at 15:02

2 Answers2

2
connectionFactory.createConnection();

connectionFactory, even if static, is never initialized. For this reason it is null and you get the null pointer when you try to access the method inside it.

Two way of fixing.

If the method is static, don't declare the var, just call:

ConnectionFactory.createConnection();

If the method is not static (strange, for a class that is static), you have to do something like:

private ConnctionFactory connectionFactory = new ConnectionFactory();
Gianmarco
  • 2,536
  • 25
  • 57
2

If you run your stand-alone application without any container around it, the @Resource injections will not work. Instead you have to do manual JNDI lookups for both the connection factory and the queue/topic:

Example:

    final Properties initialContextProperties = new Properties();

    final String factory = "jms/ConnectionFactory";
    final String queueName = "jms/Queue";

    //

    final InitialContext ic = new InitialContext(initialContextProperties);

    final QueueConnectionFactory qcf = (QueueConnectionFactory) ic
            .lookup(factory);
    final Queue queue = (Queue) ic.lookup(queueName);

As for the configuration of the InitialContext, look here: Glassfish V3.x and remote standalone client

Community
  • 1
  • 1
Beryllium
  • 12,808
  • 10
  • 56
  • 86