I need to implement a few custom functions for JSP page and for this I need to create custom taglib. It should be simple action, but I got a problem on first step when I just add empty taglib. I got a exception:
java.lang.NullPointerException
at org.apache.tomcat.util.descriptor.tld.TldResourcePath.hashCode(TldResourcePath.java:156)
at java.util.HashMap.hash(HashMap.java:338)
at java.util.HashMap.get(HashMap.java:556)
at org.apache.jasper.compiler.TldCache.getTaglibXml(TldCache.java:95)
at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:179)
at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:411)
at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:469)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1428)
at org.apache.jasper.compiler.Parser.parse(Parser.java:139)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
at org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
...
My custom tld(placed in webapp/custom.tld):
<?xml version="1.0">
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>MyLibrary</short-name>
<uri>myTagLib</uri>
</taglib>
JSP:
<%@ taglib prefix="myTag" uri="myTagLib" %>
Also, I tried to add some function in custom.tld, but without any changes.
Can someone help me? What am i doing wrong?
The NullPointerException can be avoided using checks and preventive techniques like the following: Making sure an object is initialized properly by adding a null check before referencing its methods or properties. Using Apache Commons StringUtils for String operations e.g. using StringUtils.
How to avoid the NullPointerException? To avoid the NullPointerException, we must ensure that all the objects are initialized properly, before you use them. When we declare a reference variable, we must verify that object is not null, before we request a method or a field from the objects.
NullPointerException is an unchecked exception and extends RuntimeException class.
The 'reason' that catching NullPointerException is considered a bad practice is not because you're supposed to let it bubble up when something goes wrong! Saying any exception is 'best left unhandled' based solely on its type seems like a bad idea. A NPE is considered the result of a programming error.
You need to move your custom.tld
file somewhere inside the /WEB-INF
directory for the container to find and map it to the value provided at /<taglib>/<uri>
.
If for some reason you can't, you should add a <taglib>
mapping to your web.xml file.
<jsp-config>
<taglib>
<taglib-uri>myTagLib<taglib-uri>
<taglib-location>/webportal/custom.tld<taglib-location>
</taglib>
</jsp-config>
The <taglib-location>
is specified relative to your web application root. Modify, if required.
I've spent 4 hours on fixing this. I've switched from Tomcat 6 to Tomcat 8 and started getting same NPE. I ended up finding that the problem was in importing my taglib
<%@ taglib uri="/includes/tt.tld" prefix="tt" %>
Turned out that Tomcat 8 Jasper fails on imports with relative path. I changed it to
<%@ taglib uri="myTagLib" prefix="tt" %>
and defined taglib in my web.xml
as
<jsp-config>
<taglib>
<taglib-uri>myTagLib</taglib-uri>
<taglib-location>/includes/tt.tld</taglib-location>
</taglib>
</jsp-config>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With