Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Intro to Java Web Development with Spark Bells and Whistles Template Reuse and Static Resources

Jeremy Dyck
Jeremy Dyck
12,013 Points

Broke When Adding Blocks

My code broke once I tried adding blocks around everything.

Here is the error code:

ERROR spark.http.matching.GeneralError - com.github.jknack.handlebars.HandlebarsException: /templates/index.hbs:2:7: java.lang.reflect.InaccessibleObjectException: Unable to make public boolean java.util.Collections$EmptyMap.isEmpty() accessible: module java.base does not "opens java.util" to unnamed module @19277269 /templates/index.hbs:2:7

Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make public boolean java.util.Collections$EmptyMap.isEmpty() accessible: module java.base does not "opens java.util" to unnamed module @19277269

index.hbs:

{{#partial "content"}}
    {{#if username}}
        <h1>Welcome {{ username }}!</h1>
    {{ else }}
        <h1>Welcome Students!</h1>
        <form action="/sign-in" method="post">
            <input type="text" placeholder="What's your user name?" name="username">
            <button>Sign In</button>
       </form>
    {{/if}}
{{/partial}}

{{> base.hbs}}

sign-in.hbs

{{#partial "title"}}Signed In!{{/partial}}

{{#partial "content"}}
    <p>Welcome {{ username }}!</p>
{{/partial}}

{{> base.hbs}}

base.hbs

{{#block "header"}}

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>{{#block "title"}}Welcome to Course Ideas{{/block}}</title>
    <link rel="stylesheet" href="/css/main.css">
</head>
<body>
{{/block}}

{{#block "content"}}{{/block}}

{{#block "footer"}}
    </body>
    </html>
{{/block}}

Main.java

package org.example;

import org.example.model.CourseIdeaDAO;
import org.example.model.CourseIdea;
import org.example.model.SimpleCourseIdeaDAO;

import spark.ModelAndView;
import spark.template.handlebars.HandlebarsTemplateEngine;

import java.util.HashMap;
import java.util.Map;

import static spark.Spark.*;

// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
    public static void main(String[] args) {
        staticFileLocation("/public");

        CourseIdeaDAO dao = new SimpleCourseIdeaDAO();

        get("/", (req, res) -> {
            Map<String, String> model = new HashMap<>();
            model.put("username", req.cookie("username"));
            return new ModelAndView(model, "index.hbs");
        }, new HandlebarsTemplateEngine());

        post("/sign-in", (req, res) -> {
            Map<String, String> model = new HashMap<>();
            String username = req.queryParams("username");
            res.cookie("username", username);
            model.put("username", username);
            return new ModelAndView(model, "sign-in.hbs");
        }, new HandlebarsTemplateEngine());


        // Press Alt+Enter with your caret at the highlighted text to see how
        // IntelliJ IDEA suggests fixing it.
        System.out.printf("Hello and welcome!");
    }

}