2
我只是測試,以發送郵件的GlassFish服務器隊列,以便爲我有這樣的創造JMSResource GlassFish服務器上:發送消息
,現在我已經創建了一個NetBeans IDE中的Java應用程序。這是我的Java文件: -
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
public class MessageSender
{
@Resource(mappedName = "jms/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "jms/Queue")
private static Queue queue;
public void produceMessages()
{
MessageProducer messageProducer;
TextMessage textMessage;
try
{
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(queue);
textMessage = session.createTextMessage();
textMessage.setText("Testing, 1, 2, 3. Can you hear me?");
System.out.println("Sending the following message: "
+ textMessage.getText());
messageProducer.send(textMessage);
messageProducer.close();
session.close();
connection.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new MessageSender().produceMessages();
}
}
現在,當我運行這個簡單的應用程序,然後它給上創建的ConnectionFactory錯誤:
Exception in thread "main" java.lang.NullPointerException
at simplesendamq.MessageSender.produceMessages(MessageSender.java:33)
at simplesendamq.MessageSender.main(MessageSender.java:55)
線33在上面的代碼中是應該是一個線空行(33行發生NullPointerException)。請張貼您的應用程序中的確切代碼。另一件事 - 你把你的課放在什麼樣的項目中?企業應用程序客戶端還是其他? –