JList getSelectedValue returning null

S

soup_or_power

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;
}
}
 
I

IchBin

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();
if( temp.getKey().equals(obj) ) {
return count;
}
count++;
}
return -1;
}
[snip code]

KeyValue curPosition = (KeyValue) data.elementAt(index);
[snip code]

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


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-)
 
R

ricky.clarkson

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
 
M

Menno Holscher

Can anyone tell me why a JList based on the following model returns
null?
Thanks!
private Vector data = null;

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

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.
 
S

soup_or_power

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


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top