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
3
  • Post (add to your question by using edit) your entire exception, including full stack trace. Commented Oct 12, 2015 at 0:39
  • Possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Oct 12, 2015 at 0: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. Commented Oct 12, 2015 at 0:45

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

@nloloew If this answer has fixed your problem, please consider accepting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.