InsertImage throws NullPointerException using Java | EasyMock

Hello,

I have trouble understanding a problem I faced when I wrote unit tests using EasyMock.
I have Aspose-words 16.2 for Java (jdk16).

Here is my unit test :

> // PREPARE

DocumentBuilder documentBuilder = null;
documentBuilder = new DocumentBuilder();
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
byte[] data = dstStream.toByteArray();
Document doc = new Document();
doc.save(dstStream, SaveFormat.PDF);

	EasyMock.expect(documentBuilder.insertImage(data)).andThrow(new Exception());
	EasyMock.replay(documentBuilder);

// ACT
try {
AsposeUtils.insertImageWithShadow(documentBuilder, data);
} catch (Exception e) {
// ASSERT
}

I have this stacktrace :

java.lang.NullPointerException
at com.aspose.words.zzX.zzY(Unknown Source)
at com.aspose.words.zzX.zzZ(Unknown Source)
at com.aspose.words.ShapeBase.zzF(Unknown Source)
at com.aspose.words.DocumentBuilder.insertImage(Unknown Source)
at com.aspose.words.DocumentBuilder.insertImage(Unknown Source)
at com.aspose.words.DocumentBuilder.insertImage(Unknown Source)

at
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Can you help me ?
I tried a more recent version (20.9) but the stacktrace remains.
Thank you very much.

PS : This is the code of AsposeUtils :

public static void insertImageWithShadow(DocumentBuilder builder, byte[] data) throws Exception {
try {
Shape shape = builder.insertImage(data);

        if (shape.hasImage()) {
            shape.getImageData().getBorders().setLineStyle(LineStyle.SINGLE);
            shape.getImageData().getBorders().setColor(Color.BLACK);
            shape.getImageData().getBorders().setLineWidth(1d);
            shape.getImageData().getBorders().setShadow(true);
        }
        if (shape.getMarkupLanguage() == ShapeMarkupLanguage.DML) {
            Stroke stroke = shape.getStroke();
            stroke.setOn(true);
            stroke.setWeight(1d);
            stroke.setColor(Color.BLACK);
            stroke.setEndCap(EndCap.SQUARE);
            stroke.setLineStyle(ShapeLineStyle.SINGLE);
        }
	} catch (Exception e){
		throw new Exception("Erreur survenue lors de l'insertion de l'image du document par AsposeUtils");
	}
}

@Houlabalou

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document and image files.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hello,

The input was what is written in the test :

ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
byte[] data = dstStream.toByteArray();

There was no other document, I just wanted to test if the code from Aspose threw Exception but apparently not.

@Houlabalou

You are inserting the zero length bytes data in DocumentBuilder.insertImage method. Please insert the correct image data to avoid the exception.