Tree displaying an icon for each leaf throws NPE

Looking in the forums I found this
previous post
. Using that as a baseline I’m trying to add icons for my leaf items in the tree.

Everything goes fine until I try to set the ThemeResource to the item, I get a java.lang.NullPointerException com.vaadin.data.util.IndexedContainer$IndexedContainerProperty.setValue(IndexedContainer.java:889)

Here’s my code:


//Set Icon
item.getItemProperty("icon").setValue(
getIcon(temp.getDesignation().substring(temp.getDesignation().lastIndexOf(".") + 1,
                            temp.getDesignation().length())));

The parameter of the geticon method ends up being something like ‘pdf’. Here’s the code of the getIcon method:

protected ThemeResource getIcon(String extension) throws IOException {
        WebApplicationContext context = (WebApplicationContext) getContext();
        File iconsFolder = new File(context.getHttpSession().getServletContext().getRealPath("/VAADIN/themes/xinco") + System.getProperty("file.separator") + "icons");
        if (!iconsFolder.exists()) {
            //Create it
            iconsFolder.mkdirs();
        }
        File icon = new File(iconsFolder.getAbsolutePath()
                + System.getProperty("file.separator") + extension + ".png");
        if (!icon.exists()) {
            Image image = iconToImage(xfm.getIcon(extension));
            BufferedImage buffered = new BufferedImage(
                    image.getWidth(null),
                    image.getHeight(null),
                    BufferedImage.TYPE_INT_RGB ) ;
            Graphics2D g = buffered.createGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            if (extension.indexOf('.') > -1) {
                extension = extension.substring(extension.lastIndexOf('.') + 1,
                        extension.length());
            }
            ImageIO.write(buffered, "PNG", icon);
        }
        ThemeResource resource = new ThemeResource("icons/" + extension + ".png");
        getMainWindow().showNotification(resource.getResourceId());
        return resource;
    }

I even see the file being created in the correct folder. Is the problem related to this being created dynamically? I have other icons there (but are created from the start) I have no problems getting access to:

new ThemeResource("img/blueCubsIcon16x16.GIF");

My guess is that ThemeResource might be the wrong type for this situation. Any idea?

I tried with a slightly modified version of your code since not having that method in line 11 of your example. (I instead just wrote some text to Graphics2D), but it works fine as an icon for the leaf item. So maybe the image creation fails somehow?

The issue was not in the way I created the icon, was in how I created the tree:

Here’s the relevant code for future reference:

Tree creation


                xincoTreeContainer = new HierarchicalContainer();
                xincoTreeContainer.addContainerProperty("caption", String.class, null);
                //Add icon support
                xincoTreeContainer.addContainerProperty("icon", Resource.class, null);
                xincoTree = new Tree(getResource().getString("menu.repository"));
                xincoTree.setContainerDataSource(xincoTreeContainer);
                xincoTree.setDebugId("xinco_tree");
                xincoTree.setItemIconPropertyId("icon");

Adding nodes:


            String id = "node-" + subnode.getId();
            Item item = xincoTreeContainer.addItem(id);
            item.getItemProperty("caption").setValue(subnode.getDesignation());
            // Set it to be a child.
            xincoTreeContainer.setParent(id, parent);
            //Allow to have children
            xincoTreeContainer.setChildrenAllowed(id, true);

Adding leafs:


            //Add childen data
            String id = "data-" + temp.getId();
            Item item = xincoTreeContainer.addItem(id);
            item.getItemProperty("caption").setValue(temp.getDesignation());
            // Set it to be a child.
            xincoTreeContainer.setParent(id, parent);
            // Set as leaves
            xincoTreeContainer.setChildrenAllowed(id, false);
            String name = temp.getXincoAddAttributes().get(0).getAttribVarchar();
            try {
                //Set Icon
                switch (temp.getXincoCoreDataType().getId()) {
                    case 1://File
                    //Fall through
                    case 5://Rendering
                        if (name != null
                                && name.contains(".")
                                && name.substring(temp.getDesignation().lastIndexOf(".") + 1,
                                temp.getDesignation().length()).length() >= 3) {
                            item.getItemProperty("icon").setValue(
                                    getIcon(name.substring(temp.getDesignation().lastIndexOf(".") + 1,
                                    temp.getDesignation().length())));
                        }
                        break;
                    case 2://Text
                        item.getItemProperty("icon").setValue(getIcon("txt"));
                        break;
                    case 3://URL
                        item.getItemProperty("icon").setValue(getIcon("html"));
                        break;
                    case 4://Contact
                        item.getItemProperty("icon").setValue(new ThemeResource("icons/contact.gif"));
                        break;
                }
            } catch (IOException ex) {
                Logger.getLogger(Xinco.class.getName()).log(Level.SEVERE, null, ex);
            }