I just tried a very simple case to use MailMerger but encountered an exception which I can not understand.
Basically in my template I have 3 merge fields. When I author this template, I switch on the spellcheck and rsid in word, because I want to test if the MailMerger still works in this case.
The result is, if I call VariablePrepare before calling MailMerger, there is always an nullpointer exception from FieldRef.
If I don't call VariablePrepare but call MailMerger directly, the merge result is unexpected - some dirty texts remains after the merged field.
Am I doing something wrong or it's a limitation of VariablePrepare/MailMerger? Please suggest.
- Code: Select all
/**
*
*/
package local.test;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.docx4j.model.datastorage.migration.VariablePrepare;
import org.docx4j.model.fields.merge.DataFieldName;
import org.docx4j.model.fields.merge.MailMerger;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
/**
* @author I034206
*
*/
public class MyTest {
public static void main(String[] args) {
WordprocessingMLPackage docML = null;
try {
docML = WordprocessingMLPackage.load(new FileInputStream(new File("C:/ide/m0.docx")));
VariablePrepare.prepare(docML);
List<Map<DataFieldName, String>> data = new ArrayList<Map<DataFieldName, String>>();
Map<DataFieldName, String> map = new HashMap<DataFieldName, String>();
map.put(new DataFieldName("bb_name"), "mybbname");
map.put(new DataFieldName("bb_id"), "mybbid");
map.put(new DataFieldName("activity_desc"), "myactivity");
data.add(map);
MailMerger.performMerge(docML, map, true);
docML.save(new File("C:/ide/result.docx"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
- Code: Select all
java.lang.NullPointerException
at org.docx4j.model.fields.FieldRef.getInstr(FieldRef.java:311)
at org.docx4j.model.fields.merge.MailMerger.performOnInstance(MailMerger.java:468)
at org.docx4j.model.fields.merge.MailMerger.performMerge(MailMerger.java:365)
at local.test.MyTest.main(MyTest.java:39)
Thanks in advance & Best Regards,
Daniel