Link to home
Start Free TrialLog in
Avatar of eelko
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.NullPointerException
      at org.apache.soap.transport.TransportMessage.read(TransportMessage.java:206)
      at org.apache.soap.util.net.HTTPUtils.post(HTTPUtils.java:335)
      at org.apache.soap.transport.http.SOAPHTTPConnection.send(SOAPHTTPConnection.java:291)
      at org.apache.soap.messaging.Message.send(Message.java:123)
      at BerthTest.sendSOAPMessage(BerthTest.java:128)
      at BerthTest.main(BerthTest.java: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.DocumentBuilder xdb = org.apache.soap.util.xml.XMLParserUtils.getXMLDocBuilder();
            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.SOAPException(org.apache.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,"jaws:MessageHeader");

            headerElement.setAttributeNS(URI,"SOAP-ENV:mustUnderstand","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("MessageId");
            textNode = doc.createTextNode("9999");
            tempNode = ele.appendChild(textNode);

            tempNode = headerElement.appendChild(ele);

            headerElements.add(headerElement);

            // create a vector for collecting the body elements
            Vector bodyElements = new Vector();
            //parse xml elemnt as soap body element
            bodyElements.add(doc.getDocumentElement ());

            //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(headerElements);
            envelope.setHeader(header);

            //Create the SOAP body element
            org.apache.soap.Body body = new org.apache.soap.Body();
            body.setBodyEntries(bodyElements);
            //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("Waiting for response....");
            org.apache.soap.transport.SOAPTransport 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();
        }
    }


Avatar of girionis
girionis
Flag of Greece image

 Are you sure that "URI", "enelope" and "m_hostURL" are not null?
Avatar of eelko
eelko

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("hostURL  = " + m_hostURL);
            System.out.println("datafile = " + m_dataFileName);
        }
    }
 What about the "msg"? Are you sure it has created a valid Message object?
Avatar of eelko

ASKER

msg is not null
How can I validate the msg object?
SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eelko

ASKER

The service is up and running.
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..)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
Avatar of eelko

ASKER

org.apache.soap is a package I use so I do not have the exact code (org.apache.soap.transport.TransportMessage.read(TransportMessage.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
 Thank you. I am glad it's solved :-)