title: How to create PPTX file using power-point-template using Apache POI
body: <p>I want to create power-point presentation using power-point-template(which may be already exists or may be generated by poi.) for creating power point template file which have a background image in the slides, I write the following code which creates template file which opens in open-office but giving error to opening in Microsoft-power-point.</p>
  <p>The Code is</p>
  <pre><code>private static void generatePOTX() throws IOException, FileNotFoundException {
       String imgPathStr = System.getProperty(&quot;user.dir&quot;) + &quot;/src/resources/images/TestSameChnl_001_t.jpeg&quot;;
       File imgFile = new File(imgPathStr);
       File potxFile = new File(System.getProperty(&quot;user.dir&quot;) + &quot;/src/resources/Examples/layout.potx&quot;);
       FileOutputStream out = new FileOutputStream(potxFile);
       HSLFSlideShow ppt = new HSLFSlideShow();
       HSLFSlide slide = ppt.createSlide();
       slide.setFollowMasterBackground(false);
       HSLFFill fill = slide.getBackground().getFill();
       HSLFPictureData pd = ppt.addPicture(imgFile,    PictureData.PictureType.JPEG);
       fill.setFillType(HSLFFill.FILL_PICTURE);
       fill.setPictureData(pd);  
       ppt.write(out);
       out.close();
  }
  </code></pre>
  <p>After that I tried to create a PPT file using the generated POTX file but
  But it's giving error. I am trying bellow code for this.
  And the code is</p>
  <pre><code>private static void GeneratePPTXUsingPOTX() throws FileNotFoundException, IOException {
          File imgFile = new File(System.getProperty(&quot;user.dir&quot;)+&quot;/src/resources/images/TestSameChnl_001_t.jpeg&quot;);
          File potx_File = new File(System.getProperty(&quot;user.dir&quot;) + &quot;/src/resources/Examples/layout.potx&quot; );
          File pptx_File = new File(System.getProperty(&quot;user.dir&quot;) + &quot;/src/resources/Examples/PPTWithTemplate.pptx&quot; );
          File movieFile = new File(System.getProperty(&quot;user.dir&quot;) + &quot;/src/resources/movie/Dummy.mp4&quot;);
          FileInputStream ins = new FileInputStream(potx_File);
          FileOutputStream out = new FileOutputStream(pptx_File);
          
          HSLFSlideShow ppt = new HSLFSlideShow(ins);
          List&lt;HSLFSlide&gt; slideList = ppt.getSlides();
          int movieIdx = ppt.addMovie(movieFile.getAbsolutePath(), MovieShape.MOVIE_MPEG);
          HSLFPictureData pictureData = ppt.addPicture(imgFile, PictureData.PictureType.JPEG);
          MovieShape shape = new MovieShape(movieIdx, pictureData);
          shape.setAnchor(new java.awt.Rectangle(300,225,420,280));
          slideList.get(0).addShape(shape);
          shape.setAutoPlay(true);
          ppt.write(out);
          out.close();
      }
  </code></pre>
  <p>And the exception which is coming is as fallows:</p>
  <p>java.lang.NullPointerException
  at org.apache.poi.hslf.usermodel.HSLFPictureShape.afterInsert(HSLFPictureShape.java:185)
  at org.apache.poi.hslf.usermodel.HSLFSheet.addShape(HSLFSheet.java:189)</p>