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