Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

JList getSelectedValue returning null

Thread view: 
soup_or_power@yahoo.com - 30 Dec 2005 17:12 GMT
Can anyone tell me why a JList based on the following model returns
null?
Thanks!

public class KeyValueListModel extends AbstractListModel {

    private Vector data = null;

    public KeyValueListModel() {
        data = new Vector();
    }

    public KeyValueListModel(Vector data) {
        System.out.println("hello10");
        if (null == data) {
            throw new NullPointerException("Null Pointer Exception in data");
        }

        this.data = data;
    }

    /**
    * Returns the size of the Vector
    *
    * @return int
    */
    public int getSize() {
        System.out.println("hello9");
        return data.size();
    }

    /**
    * Returns the KeyValue at the index passed.
    *
    * @param int index
    * @return Object
    */
    public Object getElementAt(int index) {
        System.out.println("keyvaluelistmodel get element at
index=***************" + index);
        Object o=null;
        try {
            o=data.elementAt(index);
        } catch (Exception ex) {
            //System.out.println("Index out of bounds" + ex.toString());
        }
        return o;
    }

    /**
    * Returns the index of the passed KeyValue.
    *
    * @param KeyValue obj
    * @return int
    */
    public int getIndexOf(Object obj) {

        for(Enumeration e = data.elements(); e.hasMoreElements();) {
            Object temp = e.nextElement();
            if( obj.toString().equals(temp.toString()) ) {
                return data.indexOf(temp);
            }
        }
        return -1;
    }

    /**
    * Returns the index of Id the passed Object.
    *
    * @param Object obj
    * @return int
    */
    public int getIndexOfId(Object obj) {
System.out.println("hello1");
        int count = 0;

        for(Enumeration e = data.elements(); e.hasMoreElements();) {
            KeyValue temp = (KeyValue) e.nextElement();
            if( temp.getKey().equals(obj) ) {
                return count;
            }
            count++;
        }
        return -1;
    }

    public int[] getIndexOfIdList(Vector objList) {
        System.out.println("hello2");
        if(objList == null) {
            throw new NullPointerException("Object List is null in
getIndexOfIdList");
        }

        int[] indices = new int[objList.size()];

        int count = 0;
        for(Enumeration e = objList.elements(); e.hasMoreElements();) {
            indices[count] = getIndexOfId(e.nextElement());
            count++;
        }

        return indices;
    }

    /**
    * Sets the list data
    *
    * @param Vector data
    */
    public void setData(Vector data) {
        System.out.println("hello7");
        if (null == data) {
            throw new NullPointerException("Null Pointer Exception in data");
        }
        this.data = data;
        fireContentsChanged(this,0,data.size()-1);
    }

    /**
    * Gets the list data
    *
    * @return Vector
    */
    public Vector getData() {
        System.out.println("hello8");
        return data;
    }

    /**
    * Add an object to the Model
    *
    * @param KeyValue obj
    */
    public void addData(Object obj) {
        System.out.println("hello3");
        if(null == obj) {
            throw new NullPointerException("Null Pointer Exception in addData");
        }
        if(null == data) {
            data = new Vector();
        }

        if(!(data.contains(obj))) {
            data.add(obj);

            if(data.size() > 1)
                fireIntervalAdded(this, data.size()-2, data.size()-1);
            else if (data.size() > 0)
                fireIntervalAdded(this, 0, data.size()-1);
            else {
                fireIntervalAdded(this, 0, 0);
                System.out.println("In KeyValueListModel Data size 0!");
            }
        }
    }

    public void removeData(Object obj) {
        System.out.println("hello4");
        if(null == obj) {
            throw new NullPointerException("Null Pointer Exception in addData");
        }

        if(data != null && data.contains(obj)) {
            data.remove(obj);

            if(data.size() > 0)
                fireIntervalRemoved(this, 0, data.size()-1);
            //else
            //    fireIntervalRemoved(this, 0, 0);
        }
    }

    public void changePosition(int index, int relativePosition) {
        System.out.println("hello5");
        if(relativePosition == 0 || index+relativePosition >= data.size() ||
                        index+relativePosition < 0 ) {
            return;
        }
        else if(index < 1 && relativePosition < 0) {
            return;
        }
        else if(index >= data.size()) {
            return;
        }
        else if(index == data.size()-1 && relativePosition > 0) {
            return;
        }

        KeyValue curPosition = (KeyValue) data.elementAt(index);

        if(relativePosition > 0) {
            for( int i=index; i < index+relativePosition; i++) {
                data.setElementAt(data.elementAt(i+1),i);
            }

        }
        else {
            for( int i=index; i > index+relativePosition; i--) {
                data.setElementAt(data.elementAt(i-1),i);
            }
        }

        data.setElementAt(curPosition,index+relativePosition);
        fireContentsChanged(this,0,data.size()-1);
    }

    /**
    * Method to return Selected KeyValue Objects
    */
    public Vector getSelectedKeyValueObjects(Vector keyIdVector){
        System.out.println("hello6");
        Vector selectedKeyValueObjects = new Vector();
        if(keyIdVector == null || keyIdVector.isEmpty()){
            return selectedKeyValueObjects;
        }

        for(Enumeration e=keyIdVector.elements();e.hasMoreElements();){
            Integer key= (Integer)e.nextElement();
            int i = getIndexOfId(key);
            selectedKeyValueObjects.add(getData().elementAt(i));
        }
               return selectedKeyValueObjects;
    }
}
IchBin - 30 Dec 2005 22:51 GMT
> Can anyone tell me why a JList based on the following model returns
> null?
> Thanks!

[snip code]

>         for(Enumeration e = data.elements(); e.hasMoreElements();) {
>             KeyValue temp = (KeyValue) e.nextElement();
[quoted text clipped - 5 lines]
>         return -1;
>     }

[snip code]

>         KeyValue curPosition = (KeyValue) data.elementAt(index);

[snip code]

What is KeyValue's type. What is it..
Signature


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)

ricky.clarkson@gmail.com - 31 Dec 2005 13:04 GMT
This is really poor code, and running it through a code checking
utility such as CheckStyle would probably give the answer:

Object o=null;
               try {
                       o=data.elementAt(index);
               } catch (Exception ex) {
                       //System.out.println("Index out of bounds" +
ex.toString());
               }
               return o;

That looks like a good candidate for returning null.  You are
swallowing all possible exceptions.  DON'T.

http://findbugs.sourceforge.net/bugDescriptions.html#REC_CATCH_EXCEPTION
and
http://checkstyle.sourceforge.net/config_coding.html#IllegalCatch
soup_or_power@yahoo.com - 31 Dec 2005 17:11 GMT
> This is really poor code, and running it through a code checking
> utility such as CheckStyle would probably give the answer:
[quoted text clipped - 14 lines]
> and
> http://checkstyle.sourceforge.net/config_coding.html#IllegalCatch

Many thanks! I'll look into CheckStyle. Why should the index go out of
bounds? And what value should be returned to the caller if the index is
out of bounds? The problem is:
- say there are 3 elements to begin with in the JList
- by some method you delete the last element of the JList
- call to JList's getSelectedValue tries to return the 3rd element
(index = 2)
I tried to use setSelectedIndex method of JList with argument 0. But
that
doesn't seem to work. Any other ideas are welcome.

Thanks again.
Menno Holscher - 31 Dec 2005 15:46 GMT
> Can anyone tell me why a JList based on the following model returns
> null?
> Thanks!

> private Vector data = null;
>
[quoted text clipped - 6 lines]
> this.data = data;
> }

The other posters on the thread had sensible comments. I think the key is in
the constructor above. "data" is initialized to null and if it is found to
be null in your constructor (which it always will be) it throws always when
you use this.
Signature

Groeten van/regards

Menno



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.