0

my addFront method I believe is the reason everythings breaking. The isEmpty() method is built but i'm not sure if that's a reason it's breaking or not.

public class ExpandableArrayBuffer implements ExpandableBuffer {
     private static final int DEFAULT_CAPACITY = 10;
    String[] elements; 

    int size = 0;
    int rear = 0;
    int front = 0; 

    //constructor
      public ExpandableArrayBuffer(int size) {
            this.size = size;   
        // Make it that size

    }
      public ExpandableArrayBuffer(){
       this(DEFAULT_CAPACITY);
      }

    @Override
    public boolean isEmpty() {

        return front == rear; 

        }

    @Override
    public void addFront(String s) {

        if(front<0||front>elements.length){
           int temp=0;
           front=temp;

       }
        if(isEmpty()){
           elements[0]=s;
        }else{

       s=elements[front];
       }
       front--;
       size++;
       }



    @Override
    public void addRear(String s) {
     int avail = (front + size) % elements.length;
        elements[avail] = s;
        size++;
    }

    @Override
    public String removeFront() {
     String answer = elements[front];
     elements[front] = null; 
     front = (front + 1) % elements.length;
     size--;
     return answer;
    }

    @Override
    public String removeRear() {
        String keysersoze = elements [rear];
        elements[rear] = null;
        rear = (rear + size) % elements.length;
        size--;
        return keysersoze;
    }

    public int size(){
        return size;
    }
}

public interface ExpandableBuffer {

    /**
     * Returns true if this buffer contains no elements.
     * @return true if this buffer contains no elements
     */
    boolean isEmpty();

    /**
     * Adds the specified string to the front of this buffer.
     * @param s string to be added to this buffer
     */
    void addFront(String s);

    /**
     * Adds the specified string to the rear of this buffer.
     * @param s string to be added to this buffer
     */
    void addRear(String s);

    /**
     * Removes and returns the string at the front of this buffer.
     * @return the string at the front of this buffer
     */
    String removeFront();

    /**
     * Removes and returns the string at the rear of this buffer.
     * @return the string at the rear of this buffer
     */
    String removeRear();

    /**
     * Returns a string representation of this buffer. The string representation
     * consists of a list of this buffer's Strings in order from front to rear,
     * enclosed in square brackets ("[]"). Adjacent Strings are
     * separated by the characters ", " (comma and space). The letter
     * "R" should appear to the left to indicate the rear of the buffer and the
     * letter "F" should appear to the right to indicate the front of the
     * buffer. Fore example, a buffer containing the strings "A", "B", and "C"
     * would be represented as "R[A, B, C]F".
     *
     * @return a string representation of this buffer
     */    
    String toString();
}

here's a main class to run and test the other classes

  public class CBufferApp {
    static String message;
    static ExpandableBuffer buffer;

    public static void main(String[] args) {
        buffer = new ExpandableArrayBuffer();
        message = " 1) Initial buffer";
        print();

        buffer.addFront("A");
        message = " 2) Add A to front";
        print();

        buffer.addFront("B");
        message = " 3) Add B to front";
        print();

        buffer.addFront("C");
        message = " 4) Add C to front";
        print();

        buffer.removeRear();
        message = " 5) Remove rear";
        print();

        buffer.removeRear();
        message = " 6) Remove rear";
        print();

        buffer.removeRear();
        message = " 7) Remove rear";
        print();

        buffer.addFront("D");
        message = " 8) Add D to front";
        print();

        buffer.addFront("E");
        message = " 9) Add E to front";
        print();

        buffer.removeRear();
        message = "10) Remove rear";
        print();

        buffer.removeRear();
        message = "11) Remove rear";
        print();

        buffer.addRear("F");
        message = "12) Add F to rear";
        print();

        buffer.addRear("G");
        message = "13) Add G to rear";
        print();

        buffer.addFront("H");
        message = "14) Add H to front";
        print();

        buffer.addFront("I");
        message = "15) Add I to front";
        print();

        buffer.addFront("J");
        message = "16) Add J to front";
        print();

        buffer.addFront("K");
        message = "17) Add K to front";
        print();

        buffer.addRear("L");
        message = "18) Add L to rear";
        print();

        buffer.addRear("M");
        message = "19) Add M to rear";
        print();

        buffer.addRear("N");
        message = "20) Add N to rear";
        print();

        buffer.addRear("O");
        message = "21) Add O to rear";
        print();

        buffer.addRear("P");
        message = "22) Add P to rear";
        print();
    }

    private static void print(){
        String emptyMessage = "";
        if(buffer.isEmpty()){
            emptyMessage = "Empty";
        }
        System.out.printf("%-23s %-7s", message, emptyMessage);
        System.out.println(buffer);
    }

}
Frightlin
  • 161
  • 1
  • 2
  • 14
  • This sounds like a perfect opportunity to learn to use a debugger. – NPE Oct 03 '14 at 05:59
  • Describe problem. Post loke "fix my code" is offtopic here. – talex Oct 03 '14 at 06:00
  • @NPE my debugger on netbeans says Exception in thread "main" java.lang.NullPointerException at ExpandableArrayBuffer.addFront(ExpandableArrayBuffer.java:34) at CBufferApp.main(CBufferApp.java:10) with line 34 meaning addFront and line 10 meaning the isEmpty() method – Frightlin Oct 03 '14 at 06:20
  • Step through the code, use breakpoints, look at variable values etc to figure out where things are going wrong. – NPE Oct 03 '14 at 06:25

1 Answers1

0

As others say: use a debugger, but in this case looking at the code it is quite obvious what is wrong, to understand this:

  1. Arrays are treated as objects, hence defining elements as a variable of the type String[] makes it a variable that can reference an array of Strings
  2. when reference variables are defined as attributes of a class like your elements they are initialized with the null pointer
  3. all references to elements.length of elements refer to the length of the array that elements reference to
  4. since nowhere in your code you actually create an array of strings like new String[5] and assign this to elements, all references to elements.length will result in a NullPointerException
GerritCap
  • 1,606
  • 10
  • 9