eelko
asked on
java.lang.NullPointerException in org.apache.soap.transport.TransportMessage.read()
In a simple SOAP-Client (copied some sample code ;-) ) I get a NullPointerException.
java.lang.NullPointerExcep tion
at org.apache.soap.transport. TransportM essage.rea d(Transpor tMessage.j ava:206)
at org.apache.soap.util.net.H TTPUtils.p ost(HTTPUt ils.java:3 35)
at org.apache.soap.transport. http.SOAPH TTPConnect ion.send(S OAPHTTPCon nection.ja va:291)
at org.apache.soap.messaging. Message.se nd(Message .java:123)
at BerthTest.sendSOAPMessage( BerthTest. java:128)
at BerthTest.main(BerthTest.j ava:214)
As you see only the BerthTest class is mine.
What am I doing wrong?
Here the code of sendSOAPMessage():
public void sendSOAPMessage()
{
try
{
// get soap body to include in the SOAP envelope
FileReader fr = new FileReader (m_dataFileName);
javax.xml.parsers.Document Builder xdb = org.apache.soap.util.xml.X MLParserUt ils.getXML DocBuilder ();
org.w3c.dom.Document doc = xdb.parse (new org.xml.sax.InputSource (fr));
if (doc == null)
{
if(m_verbose == 1)
System.err.println("Error while parsing file: " + m_dataFileName);
throw new org.apache.soap.SOAPExcept ion(org.ap ache.soap. Constants. FAULT_CODE _CLIENT, "parsing error");
}
// create a vector for collecting the header elements
Vector headerElements = new Vector();
// Create a header element in a namespace
org.w3c.dom.Element headerElement = doc.createElementNS(URI,"j aws:Messag eHeader");
headerElement.setAttribute NS(URI,"SO AP-ENV:mus tUnderstan d","1");
// Create subnodes within the MessageHeader
org.w3c.dom.Element ele = doc.createElement("From");
org.w3c.dom.Text textNode = doc.createTextNode("Me");
org.w3c.dom.Node tempNode = ele.appendChild(textNode)
tempNode = headerElement.appendChild( ele);
ele = doc.createElement("To");
textNode = doc.createTextNode("You");
tempNode = ele.appendChild(textNode);
tempNode = headerElement.appendChild( ele);
ele = doc.createElement("Message Id");
textNode = doc.createTextNode("9999") ;
tempNode = ele.appendChild(textNode);
tempNode = headerElement.appendChild( ele);
headerElements.add(headerE lement);
// create a vector for collecting the body elements
Vector bodyElements = new Vector();
//parse xml elemnt as soap body element
bodyElements.add(doc.getDo cumentElem ent ());
//Create the SOAP envelope
org.apache.soap.Envelope envelope = new org.apache.soap.Envelope() ;
//Add the SOAP header element to the envelope
org.apache.soap.Header header = new org.apache.soap.Header();
header.setHeaderEntries(he aderElemen ts);
envelope.setHeader(header) ;
//Create the SOAP body element
org.apache.soap.Body body = new org.apache.soap.Body();
body.setBodyEntries(bodyEl ements);
//Add the SOAP body element to the envelope
envelope.setBody(body);
// Build the Message.
org.apache.soap.messaging. Message msg = new org.apache.soap.messaging. Message();
msg.send (new java.net.URL(m_hostURL), URI, envelope); /* line 128! */
System.out.println("Sent SOAP Message with Apache HTTP SOAP Client.");
// receive response from the transport and dump it to the screen
System.out.println("Waitin g for response....");
org.apache.soap.transport. SOAPTransp ort st = msg.getSOAPTransport ();
BufferedReader br = st.receive ();
String line = br.readLine();
if(line == null)
{
System.out.println("HTTP POST was successful. \n");
}
else
{
while (line != null)
{
System.out.println (line);
line = br.readLine();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
java.lang.NullPointerExcep
at org.apache.soap.transport.
at org.apache.soap.util.net.H
at org.apache.soap.transport.
at org.apache.soap.messaging.
at BerthTest.sendSOAPMessage(
at BerthTest.main(BerthTest.j
As you see only the BerthTest class is mine.
What am I doing wrong?
Here the code of sendSOAPMessage():
public void sendSOAPMessage()
{
try
{
// get soap body to include in the SOAP envelope
FileReader fr = new FileReader (m_dataFileName);
javax.xml.parsers.Document
org.w3c.dom.Document doc = xdb.parse (new org.xml.sax.InputSource (fr));
if (doc == null)
{
if(m_verbose == 1)
System.err.println("Error while parsing file: " + m_dataFileName);
throw new org.apache.soap.SOAPExcept
}
// create a vector for collecting the header elements
Vector headerElements = new Vector();
// Create a header element in a namespace
org.w3c.dom.Element headerElement = doc.createElementNS(URI,"j
headerElement.setAttribute
// Create subnodes within the MessageHeader
org.w3c.dom.Element ele = doc.createElement("From");
org.w3c.dom.Text textNode = doc.createTextNode("Me");
org.w3c.dom.Node tempNode = ele.appendChild(textNode)
tempNode = headerElement.appendChild(
ele = doc.createElement("To");
textNode = doc.createTextNode("You");
tempNode = ele.appendChild(textNode);
tempNode = headerElement.appendChild(
ele = doc.createElement("Message
textNode = doc.createTextNode("9999")
tempNode = ele.appendChild(textNode);
tempNode = headerElement.appendChild(
headerElements.add(headerE
// create a vector for collecting the body elements
Vector bodyElements = new Vector();
//parse xml elemnt as soap body element
bodyElements.add(doc.getDo
//Create the SOAP envelope
org.apache.soap.Envelope envelope = new org.apache.soap.Envelope()
//Add the SOAP header element to the envelope
org.apache.soap.Header header = new org.apache.soap.Header();
header.setHeaderEntries(he
envelope.setHeader(header)
//Create the SOAP body element
org.apache.soap.Body body = new org.apache.soap.Body();
body.setBodyEntries(bodyEl
//Add the SOAP body element to the envelope
envelope.setBody(body);
// Build the Message.
org.apache.soap.messaging.
msg.send (new java.net.URL(m_hostURL), URI, envelope); /* line 128! */
System.out.println("Sent SOAP Message with Apache HTTP SOAP Client.");
// receive response from the transport and dump it to the screen
System.out.println("Waitin
org.apache.soap.transport.
BufferedReader br = st.receive ();
String line = br.readLine();
if(line == null)
{
System.out.println("HTTP POST was successful. \n");
}
else
{
while (line != null)
{
System.out.println (line);
line = br.readLine();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
Are you sure that "URI", "enelope" and "m_hostURL" are not null?
ASKER
URI is set to:
private static final String URI = "urn:hdn-berth";
envelope couldn't be null since it is used before
m_hostURL is not null either. It is declared as
private String m_hostURL;
and in my contructor I do:
public BerthTest(String hostURL, String dataFileName, int verbose) throws Exception
{
m_hostURL = hostURL;
m_dataFileName = dataFileName;
m_verbose = verbose;
if(m_verbose)
{
System.out.println("hostUR L = " + m_hostURL);
System.out.println("datafi le = " + m_dataFileName);
}
}
private static final String URI = "urn:hdn-berth";
envelope couldn't be null since it is used before
m_hostURL is not null either. It is declared as
private String m_hostURL;
and in my contructor I do:
public BerthTest(String hostURL, String dataFileName, int verbose) throws Exception
{
m_hostURL = hostURL;
m_dataFileName = dataFileName;
m_verbose = verbose;
if(m_verbose)
{
System.out.println("hostUR
System.out.println("datafi
}
}
What about the "msg"? Are you sure it has created a valid Message object?
ASKER
msg is not null
How can I validate the msg object?
How can I validate the msg object?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The service is up and running.
When I send a HTTP GET I get some response.
When I send a HTTP GET I get some response.
Sorry I can't think of anything else :-(
which is line# 206 in your code.. can you point out the exact line which causes null pointer exception..
actually line # 128
Sorry I didnt the post completely.. you have specified the line#
why dont you just run a set of if() to see which is causing null pointer exception.
OR may be before you call msg.send(), you will have to set some variable in Message class, which will be used by Message class to do SOMETHING. And since that value is not set, Message class will be throwing the null pointer exception. (just a guess..)
OR may be before you call msg.send(), you will have to set some variable in Message class, which will be used by Message class to do SOMETHING. And since that value is not set, Message class will be throwing the null pointer exception. (just a guess..)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Transport mode httpsoap is default but try setting it to http by using setSOAPTransport()
put this "new java.net.URL(m_hostURL), " in an extra line and check for null!
see if your firewall isn´t blocking your vm (logfile of firewall)
put this "new java.net.URL(m_hostURL), " in an extra line and check for null!
see if your firewall isn´t blocking your vm (logfile of firewall)
ASKER
org.apache.soap is a package I use so I do not have the exact code (org.apache.soap.transport .Transport Message.re ad(Transpo rtMessage. java:206))
And girionis triggered me to look in the right direction. Altough my servlet worked correct with HTTP Get, it did not with HTTP Post. The SOAP-server was the problem not my client.
Thanks for your comments
And girionis triggered me to look in the right direction. Altough my servlet worked correct with HTTP Get, it did not with HTTP Post. The SOAP-server was the problem not my client.
Thanks for your comments
Thank you. I am glad it's solved :-)