Vartlok
Vartlok

Reputation: 2589

NullPointerException when add custom taglib in JSP

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?

Upvotes: 9

Views: 7263

Answers (2)

expert
expert

Reputation: 30135

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>

Upvotes: 7

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

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.

Upvotes: 11

Related Questions