Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 366516

Summary: [Xtend] Flattening null entries leads to a NullPointerException
Product: [Modeling] TMF Reporter: Oliver L <oliver.libutzki>
Component: XtextAssignee: Project Inbox <tmf.xtext-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: sebastian.zarnekow
Version: 2.2.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
Xtend class none

Description Oliver L CLA 2011-12-13 06:47:49 EST
Build Identifier: 

Just look at the attached example. Because one Foo instance doesn't have any children (= null) the call of the size method on the flattened list leads to a NullPointerException.

In 2.0.1 this code returned "SIZE: 2". Looks like the null-valued properties are not ignored by the flatten method anymore.


Reproducible: Always

Steps to Reproduce:
1. Use the attached Xtend class
2. Run as Java Application
Comment 1 Oliver L CLA 2011-12-13 06:48:14 EST
Created attachment 208308 [details]
Xtend class
Comment 2 Sebastian Zarnekow CLA 2011-12-13 07:04:25 EST
Works as defined:

/**
 * [..]
 * The methods of the returned iterable may throw {@code NullPointerException} if any of the input iterators are null.
 * [..]
 */
public static final <T> Iterable<T> flatten(Iterable<? extends Iterable<? extends T>> inputs) {
	return Iterables.concat(inputs);
}
Comment 3 Sebastian Zarnekow CLA 2011-12-13 07:11:08 EST
Btw: The NPE was thrown with 2.0.1, too.
Comment 4 Sebastian Zarnekow CLA 2011-12-13 07:12:00 EST
package org.xtext.example.mydsl

import java.util.ArrayList
import java.util.List

class Sample {
	List<Sample> children
	
	def test() {
		val parents = new ArrayList<Sample>()
		
		val parent1 = new Sample()
		parent1.children = newArrayList(new Sample(), new Sample())
		parents += new Sample()
		
		val allChildren = parents.map(parent | parent.children).flatten
		val size = allChildren.size
		println("Size: " + size)
		
	}
	
}


package org.xtext.example.mydsl;

public class Main {

	public static void main(String[] args) {
		new Sample().test();
	}
	
}

Exception in thread "main" java.lang.NullPointerException
	at com.google.common.collect.Iterables$3.apply(Iterables.java:422)
	at com.google.common.collect.Iterables$3.apply(Iterables.java:420)
	at com.google.common.collect.Iterators$8.next(Iterators.java:697)
	at com.google.common.collect.Iterators$5.hasNext(Iterators.java:505)
	at com.google.common.collect.Iterators.size(Iterators.java:122)
	at com.google.common.collect.Iterables.size(Iterables.java:73)
	at org.eclipse.xtext.xbase.lib.IterableExtensions.size(IterableExtensions.java:473)
	at org.xtext.example.mydsl.Sample.test(Sample.java:39)
Comment 5 Oliver L CLA 2011-12-13 07:41:42 EST
Sorry,

I have to admit I didn't execute this test case with 2.0.1 as I don't have such an environment available at the moment.

I just know that I didn't get this NullPointerException before updating from 2.0.1 to 2.2.. 

Obviously the flatten implementation has not changed but the way the Iterable is filled... anyway I have to do some further investigations why there are null values in 2.2.