Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

warning: [rawtypes] found raw type: DefaultListModel

I'm getting this warning from the compiler which does not make sense at all (at least to me). It basically wants me to assign a Type to DefaultListModel which is a object type by itself! I'm getting heaps of this warnings all through my code!

C:\Documents and Settings\...\filename.java:345:warning: [rawtypes] found raw type: DefaultListModel
DefaultListModel lm = (DefaultListModel) jList_DataSetList.getModel();
missing type arguments for generic class DefaultListModel<E>
where E is a type-variable:
E extends Object declared in class DefaultListModel

This is another one which I have no idea where is comes from!

C:\Documents and Settings\...\filename.java:897: warning: [rawtypes] found raw type: JList
private javax.swing.JList jList_DataSetList;
missing type arguments for generic class JList<E>
where E is a type-variable:
E extends Object declared in class JList

Thanks in advance

like image 317
Sam Avatar asked Oct 19 '11 14:10

Sam


2 Answers

Since Java 7, DefaultListModel is a generic type, like List, Set, etc. It expects a type : DefaultListModel<SomeClass> instead of the raw DefaultListModel.

This allows to work in a more type-safe way, because you won't be able to insert a String into a list model which is supposed to contain Integer instances. And you won't have to cast to Integer when getting an element from the model.

The same is true for JList which is also now a JList of Strings or a JList of Integers, instead of being a raw JList.

Read the tutorial about generics, and have a look at the javadoc of DefaultListModel.

like image 58
JB Nizet Avatar answered Oct 13 '22 10:10

JB Nizet


Try @SuppressWarnings("rawtypes")

This is a particularly awful default when working with reflection or when working with interfaced methods where the exact type of the generic object is supposed to be hidden. I started changing my Class references to Class when I remembered @Suppress.

like image 26
user1454473 Avatar answered Oct 13 '22 11:10

user1454473