Null pointer exception for multiple ListModelArrays

asked 2014-08-25 16:33:35 +0800

agazerboy gravatar image agazerboy
1 1

updated 2014-08-26 06:14:08 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8

I am running following test-case of the ZK:

<?xml version="1.0" encoding="UTF-8"?>




        Tue Jan 10 10:01:05 TST 2012, Created by benbai

Copyright (C) 2012 Potix Corporation. All Rights Reserved.

        import models.*;

        ListModelArray model = ListModelArrays.getModel(ListModelArrays.MULTIPLE);
        ListModelArray model2 = ListModelArrays.getModel(ListModelArrays.MULTIPLE_AND_CLONEABLE);

        int cnt = 0;
        int elemcnt = 0;

        public void checkEqualSelection (String idOne, String idTwo, Label msg) {
            Listbox lbOne = (Listbox)msg.getPage().getFellow(idOne);
            Listbox lbTwo = (Listbox)msg.getPage().getFellow(idTwo);
            Set s1 = lbOne.getModel().getSelection();
            Set s2 = lbTwo.getModel().getSelection();
            boolean matched = false;
            for (Object o : s1) {
                for (Object o2 : s2) {
                    if (o.equals(o2)) {
                        matched = true;
                if (!matched) {
                matched = false;
        public void showSelection (String idOne, Label msg) {
            Listbox lbOne = (Listbox)msg.getPage().getFellow(idOne);
            Set s1 = lbOne.getModel().getSelection();
            StringBuilder sb = new StringBuilder("");
            boolean matched = false;
            for (Object o : s1) {
        <div>1. There are 3 Listbox below.</div>
        <div>2. For first two Listbox, their select status will sync automatically after you select item.</div>
        <div>3. Select data 10 and data 11 of third Listbox.</div>
        <div>4. Click clone and 'clone by serialization', then two Listboxes should be created and also select data 10 and data 11.</div>
        <div>5. Hold Ctrl then Select data 212 of third Listbox, data 213 of fourth and data 214 of fifth, the select status of last three listbox should not sync.</div>
        <div>6. Click clone and 'clone by serialization', you should see two Listboxes created and each Listbox after fifth Listbox select data 10, 11 and 212.</div>
    <custom-attributes org.zkoss.zul.listbox.rod="false" />
        <listbox id="lbxOne" height="150px" width="140px" model="${model}" onSelect="" checkmark="true" />
        <listbox id="lbxTwo" height="150px" width="140px" model="${model}" onSelect="" checkmark="true" />
        <listbox id="lbxThree" height="150px" width="140px" model="${model2}" onSelect="" checkmark="true" />
        <textbox id="tbOne" value="box one" />
        <textbox id="tbTwo" value="box two" />
        <button id="btnOne" label="check equal selection" onClick='checkEqualSelection(tbOne.getValue(), tbTwo.getValue(), msg);' />
        <button id="btnFour" label="show selection" onClick='showSelection(tbOne.getValue(), msg);' />
        <label id="msg" />
    <div height="10px"></div>
    <button id="btnTwo" label="clone">
        <attribute name="onClick">
            Listbox lbx = lbxThree.clone();
            lbx.setId("lbxThree_clone" + cnt++);
    <button id="btnThree" label="Clone by Serialization">
        <attribute name="onClick"><![CDATA[{
            import java.io.*;
            ByteArrayOutputStream boa = new ByteArrayOutputStream();
            new ObjectOutputStream(boa).writeObject(lbxThree);
            byte[] bs = boa.toByteArray();
            Object n = new ObjectInputStream(new ByteArrayInputStream(bs)).readObject();
            n.setId("lbxThree_serialize" + cnt++);
    <hbox id="cloneThreeArea" />

It gives the Null pointer exception at:

  ListModelArray model = ListModelArrays.getModel(ListModelArrays.MULTIPLE);

It works fine if I change the model to MULTIPLE_AND_CLONEABLE, CLONEABLE, or DEFAULT.

Here is the ListModelArrays Class

package models;

import java.util.Comparator;

import org.zkoss.zul.ListModelArray;

public class ListModelArrays {
    public static final int DEFAULT = 0;
    public static final int MULTIPLE = 1;
    public static final int CLONEABLE = 2;
    public static final int MULTIPLE_AND_CLONEABLE = 3;

    private static final int defaultAmount = 300;

    public static ListModelArray getModel(int config,int items) {
        ListModelArray model = null;
        if (config == CLONEABLE || config == MULTIPLE_AND_CLONEABLE)
            //model = (ListModelArray)org.zkoss.zktest.util.Serializations.toCloneableListModelAraay(getItems(items));
            model = new ListModelArray(getItems(items));
        if(config == MULTIPLE || config == MULTIPLE_AND_CLONEABLE)
        return model;

    public static ListModelArray getModel(int config) {
        return getModel(config,defaultAmount);
    public static Comparator getRowComparator(boolean asc) {
        return new MyRowComparator(asc);
    private static class MyRowComparator implements Comparator, java.io.Serializable {
        boolean _asc;
        public MyRowComparator(boolean asc) {
            _asc = asc;
        public int compare(Object o1, Object o2) {
            int i1 = ListModelLists.getNumberFromData((String)o1);
            int i2 = ListModelLists.getNumberFromData((String)o2);
            if (_asc && i1 > i2 || !_asc && i1 < i2)
                return 1;
            return -1;

    private static String[] getItems(int num) {
        String[] Items = new String[num];
        for (int i = 0; i < num; i++) {
            Items[i] = new String("data "+i);
        return Items;

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-08-26 13:03:20 +0800

Alecs gravatar image Alecs
103 5

Take a look at your class ListModelArrays.

if (config == CLONEABLE || config == MULTIPLE_AND_CLONEABLE)
    model = new ListModelArray(getItems(items));
if(config == MULTIPLE || config == MULTIPLE_AND_CLONEABLE)

You initialize model only if config is CLONEABLE or MULTIPLE_AND CLONEABLE. Then in the second if, when config == MULTIPLE, you incur in a NPE.

Fix your code there and it will work

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2014-08-25 16:33:35 +0800

Seen: 15 times

Last updated: Aug 26 '14

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More