NullPointerException in publishing a bytemessage
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Friday, November 7, 2003 02:08 AM

I am trying to send a file to another computer by publishing the file in a topic as a bytemessage. Here is my code: /* Set up needed connection, session, publisher and message */ TopicConnection connection = conFactory.createTopicConnection(username,password); TopicSession fileSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic fileTopic = (Topic)jndi.lookup("fileTopic"); TopicPublisher filesender = fileSession.createPublisher(fileTopic); ByteMessage outMessage = fileSession.createBytesMessage(); /* Send file as bytemessage by publishing to topic */ public void sendFileOut(String filename) throws JMSException { try { File input   More>>

I am trying to send a file to another computer by publishing the file in a topic as a bytemessage. Here is my code:



/* Set up needed connection, session, publisher and message */

TopicConnection connection = conFactory.createTopicConnection(username,password);


TopicSession fileSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);


Topic fileTopic = (Topic)jndi.lookup("fileTopic");


TopicPublisher filesender = fileSession.createPublisher(fileTopic);


ByteMessage outMessage = fileSession.createBytesMessage();



/* Send file as bytemessage by publishing to topic */


public void sendFileOut(String filename) throws JMSException {



try {



File inputFile = new File(filename);


FileInputStream in = new FileInputStream(inputFile);


byte result[] = new byte[(int)inputFile.length()];


in.read(result);



System.out.println("send length :"+result.length);


System.out.println("send:" + result);



outMessage.writeBytes(result);


System.out.println("outMessage after writeBytes:" + outMessage);


filesender.publish(outMessage);



} catch (FileNotFoundException e) {
System.out.println("File not found");

}

catch (IOException e) {
e.printStackTrace();
}



System.out.println("File transfer successful");
}




What happens is that there occurs a NullPointerException at this line:
filesender.publish(outMessage);
I assume this means that outMessage is null.



I know that the byte array result contains a value because its values are printed when I use System.out.println. But after the line:
outMessage.writeBytes(result);
it seems that the bytes were not written to the stream, that is why outMessage remains NULL.



What could be causing this problem? Are there steps I may have missed in setting up the bytemessage? Help would be greatly appreciated!!

   <<Less

Re: NullPointerException in publishing a bytemessage

Posted By:   Pep_Grifell  
Posted On:   Thursday, December 4, 2003 05:55 AM

probaly you are not reading the file content. have a look at http://forum.java.sun.com/thread.jsp?forum=31&thread=459253 to see how you can read the file.
About | Sitemap | Contact