nloloew
nloloew

Reputation: 21

Unexpected null pointer exception while changing an empty array of generics

I am doing an assignment for which I am forced to store data of an unknown type in an array, and interact with that array. I am testing my code and am getting a null pointer exception when I try to add the first value to the array. The value should be null, but That shouldn't be a problem. I'm not very familiar with generics, but I'm pretty sure that I have a problem with the data not storing primitive data types. How should I fix my code to account for this? The exception comes from the line I have marked near the bottom of my code.

*note: I am testing my code with T is a String. I have not yet tried any other data types. Thanks a lot

public class CircularBuffer<T> {

// properties
private T[] buffer; // the data
private int currentLength; // the current length
private int front; // the index of the logical front of the buffer
private int rear; // the index of the next available place
private int increment;// the increment
private int numFilled = 0;// the number of places filled, defaulted to 0

/**
 * Create a new circular buffer with default length (10) and length
 * increment(10).
 */
public CircularBuffer() {
    T[] buffer = (T[]) new Object[10];
    currentLength = 10;
    increment = 10;
    front = 0;
    rear = 0;
} // end of constructor

/**
 * Create a new circular buffer with a given length and default length
 * increment(10).
 *
 * @param initialLength
 *            The initial length of the array.
 */
public CircularBuffer(int initialLength) {
    currentLength = initialLength;
    T[] buffer = (T[]) new Object[initialLength];
    increment = 10;
    front = 0;
    rear = 0;
} // end of constructor

/**
 * Create a new circular buffer with a given length and length increment.
 *
 * @param initialLength
 *            The initial length of the array.
 * @param initialLength
 *            The initial length of the array.
 */
public CircularBuffer(int initialLength, int lengthInc) {
    currentLength = initialLength;
    T[] buffer = (T[]) new Object[initialLength];
    increment = lengthInc;
    front = 0;
    rear = 0;
} // end of constructor

/**
 * Add a value to the end of the circular buffer.
 *
 * @param value
 *            The value to add.
 *
 * @throws IllegalArgumentException
 *             if value is null.
 */
public void add(T value) throws IllegalArgumentException {
    if (value == null)
        throw new IllegalArgumentException("value is null");
    if (numFilled == currentLength) {
        T[] temp = (T[]) new Object[currentLength + increment];
        for (int n = 0; n < currentLength - 1; n += 1) {
            temp[n] = buffer[(front + n) % currentLength];
        }
        buffer = temp;
        front = 0;
        rear = currentLength;
        currentLength = currentLength + increment;
    }
    buffer[rear] = value;       // getting a null pointer exception here on the buffer[rear] element.
    rear = (rear + 1) % currentLength;
    numFilled += 1; 

} // end of add method

Upvotes: 2

Views: 210

Answers (1)

Fernando Matsumoto
Fernando Matsumoto

Reputation: 2727

The code

T[] buffer = (T[]) new Object[initialLength];

in the constructors creates a local variable buffer and sets it to an array. The field this.buffer stays null and is never assigned. Instead, use

buffer = (T[]) new Object[initialLength];

Upvotes: 2

Related Questions