Skip to content

NullPointerException when using undeclared prefixes #391

Closed
@AdrianSorop

Description

@AdrianSorop

Here's a quick sample to reproduce the issue:

String css = 
        "prefix|div {\n" + 
        "    display:block;\n" + 
        "}";
    
    Reader reader = new StringReader(css);
    ApplContext applContext = new ApplContext("en");
    applContext.setCssVersion(CssVersion.CSS3.toString());
    applContext.setProfile(CssProfile.NONE.toString());
    applContext.setMedium("all");
    applContext.setWarningLevel(2);
    
    StyleSheetParser sheetParser = new StyleSheetParser(applContext);
    sheetParser.parseStyleSheet(applContext, reader, new URL("file:fake.css"));

The following error appears in console:

java.lang.NullPointerException
	at org.w3c.css.util.ApplContext.isNamespaceDefined(ApplContext.java:596)
	at org.w3c.css.parser.analyzer.CssParser.type_selector(CssParser.java:5042)
	at org.w3c.css.parser.analyzer.CssParser.compound_selector(CssParser.java:4754)
	at org.w3c.css.parser.analyzer.CssParser.complex_selector(CssParser.java:4648)
	at org.w3c.css.parser.analyzer.CssParser.selector_list(CssParser.java:4055)
	at org.w3c.css.parser.analyzer.CssParser.ruleSet(CssParser.java:4127)
	at org.w3c.css.parser.analyzer.CssParser.afterImportDeclaration(CssParser.java:819)
	at org.w3c.css.parser.analyzer.CssParser.parserUnit(CssParser.java:611)
	at org.w3c.css.parser.CssFouffa.parseStyle(CssFouffa.java:379)
	at org.w3c.css.css.StyleSheetParser.parseStyleElement(StyleSheetParser.java:329)
	at org.w3c.css.css.StyleSheetParser.parseStyleSheet(StyleSheetParser.java:428)

Reproduces on latest release and on main branch.
The org.w3c.css.util.ApplContext.namespaces map is null.

The quick fix can be something like:
HashMap<String,String> nsdefs = Optional.ofNullable(namespaces).map(t -> t.get(url)).orElse(null);

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions