NullPointer Exception while using docx4j to convert HTML String to Docx - docx4j

I am using docx4j to convert html strings to docx.
Below is the code.
package docx4j;
import org.docx4j.convert.in.xhtml.FormattingOption;
import org.docx4j.convert.in.xhtml.XHTMLImporter;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class ConvertHTMLToDoc {
public static void main(String[] args) throws Docx4JException {
String outputfilepath = "style-example-OUT30.rtf";
String text = getHTMLString();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporter xHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
/* xHTMLImporter.setParagraphFormatting(FormattingOption.CLASS_PLUS_OTHER);
xHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);
*/
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));
/* wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Testing Title");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Subtitle", "Testing Subtitle");
*/
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
}
private static String getHTMLString() {
String text = "<html><head><title></title></head><body>" + "<p class=\"Title\">Testing Title</p>"
+ "<p class=\"Subtitle\">Testing Subtitle</p>" + "</body></html>";
return text;
}
}
When I run this, I get the error at the following line.
wordMLPackage.getMainDocumentPart().getContent().addAll(xHTMLImporter.convert(text, null));
Below is the error message.
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) java.lang.NullPointerException
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.jaxb.NamespacePrefixMapperUtils.setProperty(NamespacePrefixMapperUtils.java:155)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:588)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.XmlUtils.marshaltoString(XmlUtils.java:559)
12:19:54,125 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart.createVirtualStylesForDocDefaults(StyleDefinitionsPart.java:369)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:172)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getStyleTree(MainDocumentPart.java:161)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.stylesToCSS(XHTMLImporterImpl.java:446)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.getRenderer(XHTMLImporterImpl.java:253)
12:19:54,126 ERROR [stderr] (http-/0.0.0.0:8080-2) at org.docx4j.convert.in.xhtml.XHTMLImporterImpl.convert(XHTMLImporterImpl.java:645)
What could be the reason for the null pointer exception?

There are 2 possibly ways to do this problem:
Less elegant way( You need to create this class in your project. Remember that package com.sun.xml.internal.bind.marshaller name is needed for docx4j. You cannot change that):
package com.sun.xml.internal.bind.marshaller;
public abstract class NamespacePrefixMapper {
private static final String[] EMPTY_STRING = new String[0];
public abstract String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix);
public String[] getPreDeclaredNamespaceUris() {
return EMPTY_STRING;
}
public String[] getPreDeclaredNamespaceUris2() {
return EMPTY_STRING;
}
public String[] getContextualNamespaceDecls() {
return EMPTY_STRING;
}
}
More elegant way but i have no idea if it could work on JBOSS 6.2 since it's for version 7:
Docx4j and JBoss 7

Related

Migrating ADAL Filter to MSAL on Linux running Java 11 (maven) - AuthenticationResponseParser.parse() throws exception

in AuthenticationResponseParser.parse(new URI(fullUrl), params);
where params is
Map<String, List<String>>
and is created as
for (String key : httpRequest.getParameterMap().keySet()) {
params.put(key, Collections.singletonList(httpRequest.getParameterMap().get(key)[0]));
}
throws exception
Error: class java.util.Collections$SingletonList cannot be cast to class java.lang.String (java.util.Collections$SingletonList and java.lang.String are in module java.base of loader 'bootstrap')
What could be going wrong?
I am using MSALJ maven dependency com.microsoft.azure, msal4j, 1.13.2
Tried to pass String as params but it is expecting List of strings

Using #Context HttpServletResponse throws ambiguous dependencies org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge

I have this code on my server
#POST
#Path("subscribers")
#Consumes(MediaType.APPLICATION_XML)
public void createBook(SubscriberDTO dto, #Context HttpServletResponse response, #Context UriInfo uriInfo)throws Exception{
System.out.println("Test 1 : "+FILEPATH);
new SubscriberFileStorage().append(dto.toString(),FILEPATH);
String baseURL = uriInfo.getBaseUri().toString();
response.setStatus(HttpServletResponse.SC_CREATED);
response.setHeader(HttpHeaders.LOCATION, baseURL+dto.getTopic()+dto.getUrl());
try { response.flushBuffer(); }catch(Exception e){}
}
I get always the error, when submitting a post request
13-Jan-2023 05:01:56.403 SEVERE [http-nio-9090-exec-59] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [hesge.ch.tp05pubsubhub.HelloApplication] in context with path [/TP05PubSubHub_war_exploded] threw exception [org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001335: Ambiguous dependencies for type Ref with qualifiers #Default Possible dependencies:
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#2a0fe65e,
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#4491cd37] with root cause org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001335: Ambiguous dependencies for type Ref with qualifiers #Default Possible dependencies:
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#2a0fe65e,
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#4491cd37 at org.jboss.weld.bean.builtin.InstanceImpl.checkBeanResolved(InstanceImpl.java:244) at org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:113)
I have checked all dependencies, none works. Any idea ? I really appreciate

Kotlin JNI - Unsatisfied link error when calling external function but not when loading library

C++ method signature:
JNIEXPORT void JNICALL
Java_abc_de_fg_jni_ProxyControls_startProxy(JNIEnv *env, jobject obj);
Loading the library doesn't cause any errors:
package abc.de.fg.jni
class ProxyControls {
init {
System.load("/Users/***/***/lib***.dylib")
println("loaded library")
}
external fun startProxy()
}
But running the startProxy method throws the error:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: 'void abc.de.fg.jni.ProxyControls.startProxy()'
at abc.de.fg.jni.ProxyControls.startProxy(Native Method)
What have I done wrong?
More info:
Throwing an error on JNI_OnLoad crashes the app but throwing an error on startProxy() doesn't change the exception. So JNI_OnLoad is called but startProxy isn't called.
nm output: (filtered)
0000000000001e58 T _JNI_OnLoad
0000000000001ef4 T __Z51Java_abc_de_fg_jni_ProxyControls_startProxyP7JNIEnv_P8_jobject

Play Framework [2.2.0-Java] Custom 404 page

Hi Everyone I'm using play 2.2.0 built with scala 2.10.2 (running java 1.7.0_45)
I'm trying to make a custom Not Found Page but I don't have any success.
This is the code:
#Override
public Promise<SimpleResult> onHandlerNotFound(RequestHeader request) {
return Promise.<SimpleResult>pure(notFound(
views.html.myerrors.page404.render()
));
}
But I get this error
[error] path\app\Global.java:80: error: cannot find symbol
[error] return Promise.<SimpleResult>pure(notFound(
[error] ^
[error] symbol: method notFound(Html)
[error] location: class Global
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code
Does somebody know the reason why and the solution?
Thank you in advance.
notFound is a static method of the play.mvc.Results class.
It is probably not imported in your code. You can try to import it at the beginning of your file :
import static play.mvc.Results.notFound;
...
or import just the Results class if you don't want a static import :
import play.mvc.Results;
...
return Promise.<SimpleResult>pure(Results.notFound(
views.html.myerrors.page404.render()));
...

Runtime exception when functional test running

public class MyTest extends FunctionalTest {
#Test
public void gtest() {
Http.Response response = GET("http://google.com"); // <--- RuntimeException
assertIsOk(response);
assertContentType("text/html", response);
assertCharset("utf-8", response);
}
}
This code throw :
java.lang.RuntimeException: java.util.concurrent.ExecutionException: play.exceptions.UnexpectedException: Unexpected Error
at play.test.FunctionalTest.makeRequest(FunctionalTest.java:299)
at play.test.FunctionalTest.makeRequest(FunctionalTest.java:305)
at play.test.FunctionalTest.GET(FunctionalTest.java:103)
at play.test.FunctionalTest.GET(FunctionalTest.java:57)
at MyTest.gtest(MyTest.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Can u tell why this error occurs ? And how to fix it? Playframework 1.2.4 / Java 1.7.0_02.
You probably just need to increase your execution pool size, which is by default 1 in dev mode (which also means test, by default). You're running out of threads and thus the exception occurs.
Try with a setting like this in your application.conf:
%test.play.pool=2