8 Replies Latest reply on Feb 17, 2003 2:02 PM by Didi

    Soap message creation with an attachment

    patrick Novice

      Can someone give me a working example of the creation of a soap message with an example.

      I get a null pointer every time.

      Working with the axis stuff.

      Here is some code that does not work

      import java.io.BufferedReader;
      import java.io.ByteArrayInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.io.IOException;

      import javax.xml.soap.AttachmentPart;
      import javax.xml.soap.MessageFactory;
      import javax.xml.soap.SOAPEnvelope;
      import javax.xml.soap.SOAPException;
      import javax.xml.soap.SOAPMessage;

      import org.apache.axis.soap.MessageFactoryImpl;

      public class Test1 {

      public static void main(String[] args)
      {

      MessageFactory fac = new MessageFactoryImpl() ;

      SOAPMessage msg;
      try {
      msg = fac.createMessage();
      SOAPEnvelope nEnv= msg.getSOAPPart().getEnvelope();


      String pdfFileName = "e:\\tmp\\test.pdf";
      FileReader fr;
      fr = new FileReader(pdfFileName);
      BufferedReader buffr = new BufferedReader(fr);
      String sPdf="";
      String line = null;
      try {
      line = buffr.readLine();
      } catch (IOException e) {
      System.err.println(e.toString());
      }
      while(line!=null)
      {
      sPdf += line;
      try {
      line = buffr.readLine();
      } catch (IOException e) {
      System.err.println(e.toString());
      }
      }


      byte[] pdfData = sPdf.getBytes();
      ByteArrayInputStream stream = new ByteArrayInputStream(pdfData);

      //CREATE ATTACHMENT ADD THE STREAM AS CONTENT
      AttachmentPart attPDF = msg.createAttachmentPart();
      attPDF.setContent(stream, "application/pdf");
      msg.addAttachmentPart(attPDF);
      } catch (SOAPException e) {
      System.err.println(e.toString());
      } catch (FileNotFoundException e) {
      System.err.println(e.toString());
      }




      }
      }


      Get a

      java.lang.NullPointerException
      at org.apache.axis.Message.createAttachmentPart(Message.java:612)
      at Test1.main(Test1.java:62)
      Exception in thread "main"