We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

NullPointerException when encrypting documents from different threads

Hi,


When I try to encrypt a pdf document from different threads, it works only the first time, all next times, it fails with a nullpointerexception. Code to reproduce:

import com.aspose.pdf.CryptoAlgorithm;
import com.aspose.pdf.Document;

import java.io.IOException;

public class SetPermissions {

public static void main(String[] args) throws IOException, InterruptedException {

Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(“Thread 1 started”);
createPdf(1);
System.out.println(“Thread 1 done”);
} catch (Exception ex) {
System.err.println("Thread 1: " + ex.getMessage());
ex.printStackTrace();
}
}
});
t1.start();
t1.join();
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(“Thread 2 started”);
createPdf(2);
System.out.println(“Thread 2 done”);
} catch (Exception ex) {
System.err.println("Thread 2: " + ex.getMessage());
ex.printStackTrace();
}
}
}).start();
}

private static void createPdf(int threadId) {
Document doc = new Document(“LoremIpsum.pdf”);
doc.encrypt(“user”, “owner”, 0, CryptoAlgorithm.RC4x128);
doc.save(“test_thread_” + threadId + “.pdf”);
}
}

output:
Thread 1 started
Thread 1 done
Thread 2 started
Thread 2: null
java.lang.NullPointerException
at com.aspose.pdf.ADocument.endOperation(Unknown Source)
at com.aspose.pdf.ADocument.encrypt(Unknown Source)
at com.aspose.pdf.Document.encrypt(Unknown Source)
at com.aspose.pdf.ADocument.encrypt(Unknown Source)
at com.aspose.pdf.Document.encrypt(Unknown Source)
at test.aspose.pdf.SetPermissions.createPdf(SetPermissions.java:48)
at test.aspose.pdf.SetPermissions.access$000(SetPermissions.java:9)
at test.aspose.pdf.SetPermissions$2.run(SetPermissions.java:36)
at java.lang.Thread.run(Thread.java:744)


Is there a way to work around this?

Best regards,
Steven.

Hi Steven,


Thanks for contacting support.

I have tested the scenario and I am able to
notice the same problem. For the sake of correction, I have logged this problem
as PDFNEWJAVA-35619 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.

Hi,


this issue seems to be solved in v11.3.

Best regards,
Steven.

Hi Steven,


Thanks for the acknowledgement.

We are glad to hear that your problem is resolved in latest release. Please continue using our API’s and in the event of any further query, please feel free to contact.