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/"><Error><ErrorStatus>91</ErrorStatus><ErrorInfo>Object reference not set to an instance of an object.</ErrorInfo><ErrorTrace>Record_Execution_Results</ErrorTrace></Error></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;
}}