Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone PLEASE tell me why I receive NullpoinetException on this?

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

Here is my servlet code:
The problem is that when onMessage invokes I receive NullPointer Exception. Even if I leave this methos blank I still receive this Exception! Can anyone please tell me why?

import java.io.*;
import java.util.*;
import javax.transaction.*;
import javax.naming.*;
import javax.jms.*;
import javax.rmi.PortableRemoteObject;
import javax.servlet.*;
import javax.servlet.http.*;

public class TopicReceiveInTxServlet extends HttpServlet implements MessageListener
{
private final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
private final static String JMS_FACTORY="jms.qaTopicConnectionFactory";
private final static String TOPIC="jms.qaTopic";

private TopicConnectionFactory tconFactory;
private TopicConnection tcon;
private TopicSession tsession;
private TopicSubscriber tsubscriber;
private Topic topic;
private boolean quit = false;
private HttpServletResponse globalResult;

public void doGet(HttpServletRequest request, HttpServletResponse result) throws ServletException, IOException
{
try
{
globalResult = result;
result.setContentType("text/html");
InitialContext ic = getInitialContext("t3://localhost:7001");
init(ic, TOPIC);
System.out.println("JMS Ready To Receive Topic Messages (To quit, send a \"quit\" message).");

// Wait until a "quit" message has been received.
synchronized(this)
{
while (! this.quit)
{
try
{
this.wait();
}
catch (InterruptedException ie)
{
}
}
}
this.close();
}
catch(Exception e)
{
System.out.println("Exception");
}
}

private static InitialContext getInitialContext(String url) throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}

public void close() throws JMSException
{
tsubscriber.close();
tsession.close();
tcon.close();
}

public void init(Context ctx, String topicName) throws NamingException, JMSException
{
tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup(JMS_FACTORY),
TopicConnectionFactory.class);
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
tsubscriber = tsession.createSubscriber(topic);
tsubscriber.setMessageListener(this);
tcon.start();
}

public void onMessage(Message msg)
{

try
{
String msgText = "";
PrintWriter out = globalResult.getWriter();

if (msg != null)
{
if (msg instanceof TextMessage)
{
System.out.println("here");
//msgText = ((TextMessage)msg).getText();
System.out.println("JMS Topic TextMessage Message Received: " + msgText + "\n");
out.println("JMS Topic TextMessage Received: " + msgText + "<BR>");
}
else if (msg instanceof MapMessage)
{
Enumeration mapNames = ((MapMessage)msg).getMapNames();
String name = (String) mapNames.nextElement();
System.out.println("JMS Topic MapMessage Received: " + name + " \n");
out.println("JMS Topic MapMessage Received: " + name + " <BR>");
}
else if (msg instanceof ObjectMessage)
{
Hashtable numbers = (Hashtable) ((ObjectMessage)msg).getObject();
System.out.println("JMS Topic ObjectMessage Received: " + numbers.toString() + "\n");
out.println("JMS Topic ObjectMessage Received: " + numbers.toString() + "<BR>");
}
else if (msg instanceof BytesMessage)
{
byte byteArray[] = {1,2,3,4,5};
int numBytes = ((BytesMessage)msg).readBytes(byteArray);
System.out.println("JMS Topic BytesMessage Received: " + numBytes + " received \n");
out.println("JMS Topic BytesMessage Received: " + numBytes + " bytes received <BR>");
}
else if (msg instanceof StreamMessage)
{
String s = ((StreamMessage)msg).readString();
System.out.println("JMS Topic StreamMessage Received: " + s + "\n");
out.println("JMS Topic StreamMessage Received: " + s + "<BR>");
}
else if (msg instanceof Message)
{
System.out.println("An empty JMS Topic Message Received: " + "\n");
System.out.println("It's priority: " + msg.getJMSPriority() + "\n");
System.out.println("It's message ID: " + msg.getJMSMessageID() + "\n");
out.println("An empty JMS Topic Message Received: " + "<BR>");
out.println("It's priority:" + msg.getJMSPriority() + "<BR>");
out.println("It's message ID:" + msg.getJMSMessageID() + "<BR>");
}

if (msgText.equalsIgnoreCase("quit"))
{
synchronized(this)
{
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
}
}
catch (JMSException jmse)
{
jmse.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}

}

 
It might help us if you were to post the stack trace, aswell as the line that the NPE is being thrown on - please highlight the line.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top