0

I have a problem in passing my xml document to an APi in asmx ,it always showing me error of "Object reference not set to an instance of an object" i m getting the xml from one Api and and made some changes and passing it to another api for results update on production server but i m getting this error

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://orcanos.com/">&lt;Error&gt;&lt;ErrorStatus&gt;91&lt;/ErrorStatus&gt;&lt;ErrorInfo&gt;Object reference not set to an instance of an object.&lt;/ErrorInfo&gt;&lt;ErrorTrace&gt;Record_Execution_Results&lt;/ErrorTrace&gt;&lt;/Error&gt;</string>

Please help me out

This is the code:

 package Program;

    import java.io.BufferedReader;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.StringWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Document;
    import org.xml.sax.SAXException;

    import com.orcanos.QPackServSoapProxy;

    public class Program11 {
     static Document doc;
      public static void main(String argv[]) throws ParserConfigurationException, SAXException, IOException, TransformerException {



        File fXmlFile = new File("C:\\Users\\Anktech\\Desktop\\NewXML2 - Copy.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
         doc = dBuilder.parse(fXmlFile);

        //optional, but recommended
        //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
         String resf = convertDocumentToString(doc);

        System.out.println("----------------------------");
        QPackServSoapProxy ObjectProxy = new QPackServSoapProxy();
        ObjectProxy.setEndpoint("http://199.203.211.68:223/qpack/qpackserv/qpackserv.asmx");
        String result1 = HttpPost("Automation.1", "12345678",resf);
        //String result = ObjectProxy.record_Execution_Results("Automation.1", "12345678", resf);
        System.out.println(result1);


      }//convert to string
      public static String getStringFromDocument() throws TransformerException {

            DOMSource domSource = new DOMSource(doc);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
            return writer.toString();


    }//Http post 
      public static String HttpPost(String username, String password, String xml)
                throws IOException {
            String urlParameters = "user_Name=" + username + "&user_Password="
                    + password + "&sXML=" + xml;
            String request = "http://199.203.211.68:223/qpack/qpackserv/qpackserv.asmx/Record_Execution_Results";
            URL url = new URL(request);
            try {
                System.out.println("111");
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                System.out.println("1112222");
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setInstanceFollowRedirects(false);
                connection.setRequestMethod("POST");
                //connection.setRequestProperty("Content-Type",
                        //"application/x-www-form-urlencoded");
                //connection.setRequestProperty("charset", "utf-8");
                //connection.setRequestProperty("Content-Length",
                        //"" + Integer.toString(urlParameters.getBytes().length));

                connection.setUseCaches(false);
                System.out.println("1133333");

                DataOutputStream wr = new DataOutputStream(
                        connection.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();
                System.out.println("111444");
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
                System.out.println("1115555");

                connection.disconnect();
                // print result

                return response.toString();
            } catch (Exception ex) {
                System.out.println("1116666666666");

                System.out.println(ex.getMessage());
                return "";
            }

    }
      private static String convertDocumentToString(Document doc) {
          TransformerFactory tf = TransformerFactory.newInstance();
          Transformer transformer;
          try {
              transformer = tf.newTransformer();
              // below code to remove XML declaration
              // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
              StringWriter writer = new StringWriter();
              transformer.transform(new DOMSource(doc), new StreamResult(writer));
              String output = writer.getBuffer().toString();
              return output;
          } catch (TransformerException e) {
              e.printStackTrace();
          }

          return null;
      }}
tobias_k
  • 81,265
  • 12
  • 120
  • 179

1 Answers1

0

I've always created my SOAP envelopes manually without any issues:

        String URL;
        String soapAction;
        String soapRequestEnvelope;
        String responseString = "";

        URL = "https://something.something.com/myservice.asmx";

        soapAction = "http://www.testsite.org/DoSomething";

        soapRequestEnvelope = "<?xml version=\"1.0\" encoding=\"utf-16\"?>"+
        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
          "<soap:Body>"+
            "<DoSomething xmlns=\"http://www.testsite.org/\">"+
              "<value1>"+ val1 +"</value1>"+
              "<value2>"+ val2 +"</value2>"+
            "</DoSomething>"+
          "</soap:Body>"+
        "</soap:Envelope>";

        final DefaultHttpClient httpClient=new DefaultHttpClient();

        // request parameters
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 10000);
        HttpConnectionParams.setSoTimeout(params, 15000);

        // set parameter
        HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

        // POST the envelope
        HttpPost httppost = new HttpPost(URL);

        // add headers

        httppost.setHeader("soapaction", soapAction); 

        httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

        try 
        {

        // the entity holds the request  
        HttpEntity entity = new StringEntity(soapRequestEnvelope);   
        httppost.setEntity(entity);  

        // Response handler  
        ResponseHandler<String> rh = new ResponseHandler<String>() {  

        // invoked when client receives response  
        public String handleResponse(HttpResponse response)

        throws ClientProtocolException, IOException
        {  
            //get response entity  
            HttpEntity entity = response.getEntity();  

            //Read the response as byte array  
            StringBuffer out = new StringBuffer();  

            byte[] b = EntityUtils.toByteArray(entity);  

            // write the response byte array to a string buffer
            out.append(new String(b, 0, b.length));

            return out.toString();

        }

        };



        responseString = httpClient.execute(httppost, rh);



        }

        catch (Exception e) {

//          Log.v("exception", "------->" + e.toString());

        }

The entire structure of the envelope can be viewed on the asmx page. Easy as pie, and you have full control. The xml response in this case is responseString.

PaulG
  • 6,920
  • 12
  • 54
  • 98