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:
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.
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.
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.
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.
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
}
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!
@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
@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.
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.
@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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.