troubleshooting Question

Null pointer exception in unselecting checkbox in Java?

Avatar of Tolgar
Tolgar asked on
12 Comments1 Solution578 ViewsLast Modified:
I am writing a code to unselect all checkboxes of a Tree.

When I click on a button in the GUI, it calls unselectAll method as below:


CheckListTree class is like this. It has getInstance, unselectAll, checkPath, checkItems, and createCheckListTree methods.

When I print "instance" it is not null in line 7, but when I print "tree' in unselectAll method, it returns null in line 13. Therefore, I get this null pointer exception error.

But I don't know why it is null. Can you please take a look at it?

public class CheckListTree {
    private Tree tree;
    public static CheckListTree getInstance()
        CheckListTree instance = new CheckListTree();
        System.out.println("instance: " + instance);
        return instance;
    public void unselectAll (){
        System.out.println("tree: " + tree);
        for (TreeItem item : tree.getItems()) {   
            System.out.println("item: " + item);
            checkItems(item, false);
    public void checkPath(TreeItem item, boolean checked, boolean grayed) {
        if (item == null) return;
        if (grayed) {
            checked = true;
        } else {
            int index = 0;
            TreeItem[] items = item.getItems();
            while (index < items.length) {
                TreeItem child = items[index];
                if (child.getGrayed() || checked != child.getChecked()) {
                    checked = grayed = true;
        checkPath(item.getParentItem(), checked, grayed);

    public void checkItems(TreeItem item, boolean checked) {
        TreeItem[] items = item.getItems();
        for (int i = 0; i < items.length; i++) {
            checkItems(items[i], checked);

    public void createCheckListTree(Composite compTab2, GridData layoutData, List<HashMap<Integer, String>> checkList)  {
            tree = new Tree(compTab2, SWT.BORDER | SWT.CHECK);
            tree.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    if (event.detail == SWT.CHECK) {
                        TreeItem item = (TreeItem) event.item;
                        boolean checked = item.getChecked();
                        checkItems(item, checked);
                        checkPath(item.getParentItem(), checked, false);
            Iterator<HashMap<Integer, String>> checkListIter = checkList.iterator();
            while(checkListIter .hasNext()) {
              HashMap<Integer, String> amap =   (HashMap<Integer, String>) checkListIter .next();
              Set<?> keysetString = (Set<?>) amap .keySet();
              Iterator<?> iter = keysetString.iterator();
              while( iter.hasNext() ) {
                Integer keyVal = (Integer);
                //System.out.println("keyVal " + keyVal);
                String valFromKey = amap.get(keyVal);
                //System.out.println("valFromKey " + valFromKey);
                TreeItem itemI = new TreeItem(tree, SWT.NONE);

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros