flink-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From subash basnet <yasub...@gmail.com>
Subject how to get rid of null pointer exception in collection in DataStream
Date Wed, 13 Jul 2016 16:45:23 GMT
Hello all,

I need to collect the centroids to find out the nearest center for each
DataStream<Point> points = newDataStream.map(new getPoints());
DataStream<Centroid> *centroids* = newCentroidDataStream.map(new
ConnectedIterativeStreams<Point, Centroid> loop =
DataStream<Centroid> newCentroids = loop.flatMap(new
SelectNearestCenter()).map(new CountAppender()).keyBy(0)
.reduce(new CentroidAccumulator()).map(new CentroidAverager());
DataStream<Centroid> finalCentroids =

But I am getting null pointer exception for collection of centroids when
trying to add a centroid in *flatMap2. *Below is my code, how could I get
rid of this null pointer exception, any other ideas.

public static final class SelectNearestCenter implements
CoFlatMapFunction<Point, Centroid, Tuple2<String, Point>> {
private Collection<Centroid> *centroids*;
public void flatMap1(Point p, Collector<Tuple2<String, Point>> out) throws
Exception {
if (centroids != null) {
if (centroids.size() > 19) {
// let's assume minimum size 20 for now
for (Centroid centroid : *centroids*) {
                    // emit a new record with the center id and the data
out.collect(new Tuple2<String, Point>(closestCentroidId, p));

public void flatMap2(Centroid value, Collector<Tuple2<String, Point>> out)
throws Exception {

The instatation as below is not allowed. So it always throws null pointer
private Collection<Centroid> *centroids = * new Collection<Centroid>();

Best Regards,
Subash Basnet

View raw message