2

I'm trying to add elements to my ArrayList called activList using:

activList.add(new Fiducial(iD, x, y ));

But I get a NullPointerException on this line when running the code:

int cols = 10, rows = 10;
int rectangleWidth = 100;
int rectangleHeight = 60;

// these are some helper variables which are used
// to create scalable graphical feedback
int k, l, iD;
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
float x, y;

ArrayList<Fiducial> activList  ;


public class Fiducial {
  public int iD;
  public float x;
  public float y;

  public Fiducial(int iD, float x, float y) {
    this.iD = iD;
    this.x = x;
    this.y = y;
  }
}


void draw() {
  // Begin loop for columns
  for ( k = 0; k < cols; k++) {
    // Begin loop for rows
    for ( l = 0; l < rows; l++) {
      fill(255);
      stroke(0);
      rect(k*rectangleWidth, l*rectangleHeight, rectangleWidth, rectangleHeight);
    }
  }


  // This part detects the fiducial markers 
  float obj_size = object_size*scale_factor; 
  float cur_size = cursor_size*scale_factor; 

  ArrayList<TuioObject> tuioObjectList = tuioClient.getTuioObjectList();
  for (int i=0; i<tuioObjectList.size (); i++) {
    TuioObject tobj= tuioObjectList.get(i);
    stroke(0);
    fill(0, 0, 0);
    pushMatrix();
    translate(tobj.getScreenX(width), tobj.getScreenY(height));
    rotate(tobj.getAngle());
    rect(-80, -40, 80, 40);
    popMatrix();
    fill(255);
    x = round(10*tobj.getX ());
    y = round(10*tobj.getY ());
    iD = tobj.getSymbolID();
    activList.add(new Fiducial(iD, x, y ));
    fiducialCoordinates ();
  }
}


void fiducialCoordinates () {
  System.out.println("x= "+ x + " y= " + y + " iD= " + iD );
  System.out.println(activList);
}

I did read this page: What is a NullPointerException, and how do I fix it? (so don't report it as duplicate please)

And what I understood is that I get the error because my ArrayList doesn't have any element. However, I tried to add one right after creating it, like this:

ArrayList<Fiducial> activList  ;
activList.add(new Fiducial(3, 4.0, 5.0 ));

But I get the error: "unexpected token: ( " at the line:

activList.add(new Fiducial(3, 4.0, 5.0 ));

I don't get it, I thought adding an element would prevent it to be Null but I still get an error. How should I modify this ?

Thanks for your help

Community
  • 1
  • 1
Graham Slick
  • 6,692
  • 9
  • 51
  • 87

5 Answers5

10

You never actually instantiate the ArrayList using new. No memory has been allocated for the ArrayList.

Fix it like this:

ArrayList<Fiducial> activList = new ArrayList<Fiducial>();

From the docs:

Instantiation: The new keyword is a Java operator that creates the object.

Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Creating Objects Documentation

Only with primitive types (int, double, etc.) will declaring the variable as you did allocate memory for it. Objects need the new keyword to have memory associated with them.

Clint
  • 900
  • 11
  • 25
4

You must initialize your reference of list, like this:

ArrayList<Fiducial> activList  = new ArrayList<Fiducial>();
activList.add(new Fiducial(3, 4.0, 5.0 ));

You indicate that reference activList will be specify to ArrayList object in heap memory. You may try the other ways to initialize:

// List -> use ArrayList or LinkedList
List<Fiducial> activList  = new ArrayList<Fiducial>();
// Collection -> use List, Set or Queue realizations
Collection<Fiducial> activList  = new ArrayList<Fiducial>();

It will help you make your code more bending.

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
3

Just instantiate it by using the new keyword:

ArrayList<Fiducial> activList  = new ArrayList<Fiducial>();

or in java 7, use diamond operator:

ArrayList<Fiducial> activList  = new ArrayList<>();
Bruno Brs
  • 673
  • 1
  • 6
  • 23
2

This line automatically initializes activList to null, so you need to instatiate it.

ArrayList<Fiducial> activList ;

You need to have it be:

List<Fiducial> activList = new ArrayList<Fudicial>();
Brandon Ling
  • 3,841
  • 6
  • 34
  • 49
2

To add to the other two answers:

You are not getting a null pointer error because your ArrayList is empty.

You can still make a reference to an empty arrayList.

If you couldn't, how else would you initiliaze an arrayList and assign a reference to it?

You were getting the null pointer error because you never actually initialized your ArrayList<Fiducial> with a new ArrayList<Fiducial>();

So the reference is null because it wasn't actually referencing any ArrayList.

SwiftySwift
  • 477
  • 4
  • 13