views:

94

answers:

4

I've used Arrays.asList dozens if not hundreds of times without problem. All of a sudden previously compiling code is failing to compile after switching to NetBeans 6.9 from 6.8. Here's a few lines in question:

Node n = new NickNode(4,5);
Node m = new NonLocatableNode();
Node subclass = new NickSubclassNode();

List<Node> nodes = Arrays.asList(n,m,subclass);

The subclasses of node are not important; they compile fine. The line that gives me an error is the Arrays.asList line. I get the error

alt text

I have no idea where it's getting anything about a HelpCtx.Provider[]... Does anyone see anything wrong with this snippet?

Replacing the asList line with

List<Node> theNodes = new LinkedList<Node>();
theNodes.add(n);
theNodes.add(m);
theNodes.add(subclass);

works fine. But I prefer the shorter syntax of Arrays.asList

+2  A: 

Your error graphic is not showing up for me but it looks like a generics problem. Perhaps a compiler warning was switched into a compiler error when you moved from netbeans 6.8 to 6.9?

Try declaring your List as ...

List<? extends Node> nodes = Arrays.asList(n, m, subclass);

The wildcard syntax specifies that the list contains Nodes and anything that inherits from Node.

Tansir1
Did not fix the problem - get the exact same error.
I82Much
A: 

it should be:

List<? extends Node> nodes = Arrays.asList(n,m,subclass);

bear in mind that :

List<Sub Class> is not a sub class for List<Parent Class>

they are different classes.

mohammad shamsi
n, m and subclass are all declared as a `Node`. The runtime type is completely irrelevant.
Mark Peters
@Mark: n, m and subclass are all declared as a Node but List<subclass> is not a List<Node>. that's way you need to have ? extends Node as generic type.
mohammad shamsi
You're right that, but in this case it doesn't matter, since asList will return a List<Node>, not a List<SubClassOfNode>.
Mark Peters
+2  A: 

Yep you are right this is the bug in NetBeans 6.9 which is already reported.So hopefully it will get resolved soon. You can see that bug report here

Rupeshit
please post the link where the error is reported.
Rakesh Juyal
Rakesh,I posted bug link.
Rupeshit
Pardon my ignorance but that bug report seems to be completely unrelated. I'm not getting a null pointer exception at all.
I82Much
+1  A: 

Try this

List<Node> nodes = Arrays.<Node>asList(n,m,subclass);    
Dany
That worked. I stumbled onto that syntax in the past (it is truly bizarre), but have *never* had to use it in the context of an Arrays.asList declaration. Usually the type inferencing works fine.
I82Much