ArrayList's add() function not recognized

edited May 2016 in Library Questions

The following code throws an error at line 49 (revisions.add(new Revision(dbo))) saying that the function add() does not exist. Why? Has it got something to do with the fact that it's within a try-catch? Thanks in advance for your help.

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH);

HashMap<String,Contributor> contributors = new HashMap<String,Contributor>();
ArrayList<Revision> revisions = new ArrayList<Revision>();
float revisionCount;

void setup() {
  background(0);
  size(displayWidth, displayHeight);
  noStroke();

  MongoClient mongoClient = null;
  try {
    mongoClient = new MongoClient();
  } 
  catch (Exception e) {
  }

  DB db = mongoClient.getDB("wikipedia");
  DBCollection revisions = db.getCollection("revisions");

  revisionCount = (float) revisions.count();
  revisionCount *= 10;

  float xScale = width / revisionCount;
  float yScale = height / 42183.0;
  translate(0, (1-yScale) * height);
  scale(xScale, yScale);

  DBCursor cursor = revisions.find();
  try {
    while (cursor.hasNext()) {
      DBObject dbo = cursor.next();
      revisions.add(new Revision(dbo));
    }
  } 
  finally {
    cursor.close();
    println("Done");
  }
}

void draw() {
  background(0);
  for (Revision rev : revisions) {
    rev.render();
  }
}

Answers

  • edited March 2014 Answer ✓

    You declare revisions at the global scope, but then you override it for the local scope (setup()) with a DBCollection of the same name! You must change the name of one of these variables to avoid a name conflict.

  • Thanks, calsign. I feel stupid now.

Sign In or Register to comment.