2

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
nloloew
  • 21
  • 1
  • Post (add to your question by using [edit]) your entire exception, including full stack trace. – PM 77-1 Oct 12 '15 at 00:39
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwin Bolwidt Oct 12 '15 at 00:41
  • You haven't assigned your instance variable `buffer` in any of the constructor. You are only assigning a local variable also called buffer in each of the constructors. – Erwin Bolwidt Oct 12 '15 at 00:45

1 Answers1

2

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];
Fernando Matsumoto
  • 2,697
  • 1
  • 18
  • 24
  • @nloloew If this answer has fixed your problem, please consider [accepting](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it. – Fernando Matsumoto Oct 12 '15 at 01:30