NullPointerException Occurs When Opening and Saving a PPTX in Java (3394)

Hello,

I have an issue opening and saving a presentation, both with 25.3 and 25.4 versions of the SLides library, it throws

java.lang.NullPointerException: Cannot invoke "com.aspose.slides.internal.kn.nr.ew()" because "<local7>" is null

Here is the test code used:

@Test
public void aspose() throws Exception {
    String sourceFilePath = "aspose.pptx";
    File tempTargetFile = File.createTempFile("target", ".pptx");

    Presentation presentation = null;

    int loadFormat = PresentationFormatHelper
            .getLoadFormat(new File(getClass()
                    .getClassLoader()
                    .getResource(sourceFilePath)
                    .getFile()));

    try (InputStream testStream = getClass().getClassLoader().getResourceAsStream(sourceFilePath)) {
        presentation = new com.aspose.slides.Presentation(testStream);

        try (OutputStream fileOutputStream = new FileOutputStream(tempTargetFile.getAbsolutePath())) {
            presentation.save(fileOutputStream, PresentationFormatHelper.getSaveFormat(loadFormat));
        } catch (Exception e) {
            fail("There is problem saving presentation.\n" + e.toString());

            throw new RuntimeException(e);
        }
    } catch (Exception e) {
        fail("There is problem saving presentation.\n" + e.toString());

        e.printStackTrace();
    } finally {
        if (tempTargetFile != null && tempTargetFile.exists()) {
            FileUtils.forceDelete(tempTargetFile);
        }

        if (presentation != null) {
            presentation.dispose();
        }
    }
}

Test file on Lime Wire (expires in one week).

Kind regards,
Zeljko

@zpredojevic

It seems you are encountering a NullPointerException when trying to open and save a presentation using the Aspose.Slides library. This issue can occur for various reasons, including problems with the input file or the way the presentation is being handled in your code.

Here are a few suggestions to help you troubleshoot and resolve the issue:

  1. Check the Input File: Ensure that the aspose.pptx file is correctly located in your resources and is not corrupted. You can try opening the file manually to verify its integrity.

  2. Load Format: Make sure that the load format is being determined correctly. You can add some debug statements to log the value of loadFormat to ensure it corresponds to the expected format.

  3. Error Handling: In your current implementation, the error handling could be improved. Instead of failing immediately, consider logging the exception details to understand what might be causing the NullPointerException.

  4. Resource Management: Ensure that all resources are being managed properly. You are already using try-with-resources for streams, which is good. Just make sure that the presentation object is not null before calling methods on it.

  5. Example Code: Here’s a simplified version of how you might structure your code to save a presentation, which you can compare with your implementation:

    try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(sourceFilePath)) {
        if (inputStream == null) {
            throw new FileNotFoundException("File not found: " + sourceFilePath);
        }
        Presentation presentation = new Presentation(inputStream);
        try (OutputStream outputStream = new FileOutputStream(tempTargetFile)) {
            presentation.save(outputStream, SaveFormat.Pptx);
        }
    } catch (Exception e) {
        e.printStackTrace(); // Log the exception for debugging
    }
    
  6. Library Version: Ensure that you are using the latest version of the Aspose.Slides library, as bugs are often fixed in newer releases.

If the issue persists after trying these suggestions, consider reaching out to the Aspose support team with detailed information about the error and the context in which it occurs.

If you have any further questions or need additional assistance, feel free to ask!

@zpredojevic,
Unfortunately, I was unable to reproduce the error you described.

Since your code example is not compilable, I used the following code instead:

Presentation presentation = new Presentation("aspose.pptx");
presentation.save("output.pptx", SaveFormat.Pptx);
presentation.dispose();

If the issue persists, please share the following additional information:

  • standalone code example to reproduce the error
  • OS version on which the code was executed
  • JDK target version in your application project

@andrey.potapov Sorry about that, I forgot to remove al internal stuff.

Here is the code example:

import com.aspose.slides.LoadFormat;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationFactory;
import com.aspose.slides.SaveFormat;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import static org.junit.Assert.fail;

public class AsopseTest{
    @Test
    public void aspose() throws Exception {
        String sourceFilePath = "aspose.pptx";
        File tempTargetFile = File.createTempFile("target", ".pptx");

        Presentation presentation = null;

        int loadFormat = new PresentationFactory().getPresentationInfo(
                new File(getClass()
                        .getClassLoader()
                        .getResource(sourceFilePath)
                        .getFile()).getAbsolutePath()).getLoadFormat();

        try (InputStream testStream = getClass().getClassLoader().getResourceAsStream(sourceFilePath)) {
            presentation = new com.aspose.slides.Presentation(testStream);

            try (OutputStream fileOutputStream = new FileOutputStream(tempTargetFile.getAbsolutePath())) {
                presentation.save(fileOutputStream, getSaveFormat(loadFormat));
            } catch (Exception e) {
                fail("There is problem saving presentation.\n" + e.toString());

                throw new RuntimeException(e);
            }
        } catch (Exception e) {
            fail("There is problem saving presentation.\n" + e.toString());

            e.printStackTrace();
        } finally {
            if (tempTargetFile != null && tempTargetFile.exists()) {
                FileUtils.forceDelete(tempTargetFile);
            }

            if (presentation != null) {
                presentation.dispose();
            }
        }
    }

    public static int getSaveFormat(int loadFormat) {

        switch (loadFormat) {
            case LoadFormat.Ppt: {
                return SaveFormat.Ppt;
            }
            case LoadFormat.Pptx: {
                return SaveFormat.Pptx;
            }
            case LoadFormat.Ppsx: {
                return SaveFormat.Ppsx;
            }
            case LoadFormat.Ppsm: {
                return SaveFormat.Ppsm;
            }
            case LoadFormat.Pptm: {
                return SaveFormat.Pptm;
            }
            case LoadFormat.Potx: {
                return SaveFormat.Potx;
            }
            case LoadFormat.Potm: {
                return SaveFormat.Potm;
            }
            default:
                throw new RuntimeException(
                        String.format("Unsupported presentation format: %d", loadFormat)); //$NON-NLS-1$
        }
    }
}

OS info:

zpredojevic@zOMEN17:~$ uname -a
Linux zOMEN17 6.8.0-57-generic #59~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Mar 19 17:07:41 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Java info:

zpredojevic@zOMEN17:~$ java --version
openjdk 17.0.6 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)

JDK target version in maven project:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>17</source>
		<target>17</target>
		<encoding>UTF8</encoding>
	</configuration>
</plugin>

Slides version used:
25.3 and
25.4

Best regards,
Zeljko

@zpredojevic,
Thank you for the details.I’ve reproduced the NullPointerException when executing the code example in the environment you specified.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): SLIDESJAVA-39658

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@zpredojevic,
Our developers have investigated the issue. The problem occurs when trying to calculate the size of mathematical text and is related to the absence of the Cambria Math font on the system. No suitable replacement could be found.

To resolve the issue, please install the Cambria Math font on the system or use the FontsLoader to load it.

FontsLoader.loadExternalFonts(new String[] { "path_to_fonts" });

Custom PowerPoint Font in Java|Aspose.Slides Documentation

Thank you Andrey, font addition does solve the issue.
Is there a chance to improve this exception? It would be easier both for you and us if it is a specific exception with a precise message that will tell us what exactly is missing.

Kind regards,
Zeljko

@zpredojevic
We have already created ticket SLIDESJAVA-39660 to improve the exception and error message for such cases. The issue is scheduled to be resolved in Aspose.Slides for Java 25.5, which will be published in the second half of May. You will be notified once the update is available.

Great, thank you!

Best, Zeljko

@zpredojevic,
Thank you for using Aspose.Slides.