-2

welcome to my hard question ;) in java i have a problem .. i have my specific list implementation to Store objects

what i can do to solve this problem ? any body can help me and save my day ?

public class Alist implements ListInterface {

      private Object entry[];   // array of list entries
     static public int length;  // current number of entries in list
      private static final int MAX_SIZE = 50; // max length of list
public Alist()
      { length=0;
        entry= new Object [ MAX_SIZE ];
      }// end default constructor

public Alist ( int maxSize)
      { length=0;
        entry= new Object[ maxSize ];
      }// end user constructor

public boolean add ( Object newEntry)
      {
        Boolean isSuccessful =true;
        if(!isFull())
        {
          entry [length]=newEntry;
          length++;
        }
       else
        isSuccessful =false;
      return isSuccessful;
      }//end add

public boolean add (int newPosition, Object newEntry)
      {  boolean isSuccessful =true;
          if( (! isFull() && newPosition >=0)&&(newPosition<=length))
            { if (newPosition < length)      // optional
                makeRoom (newPosition);// private method that helps to shift items
                                                                   //in order to make space for the new entry;
              entry [newPosition]=newEntry;
              length++;
            }
           else
            isSuccessful =false;
          return isSuccessful;
     }//end add

private void makeRoom (int newPosition)
       {  for (int index=length; index> newPosition; index--)
             entry [index]=entry[index-1];
       }//end makeRoom

public Object remove (int givenPosition)
      {    Object result=null; // returned value
           if(! isEmpty() &&(givenPosition >=0)&&(givenPosition<length))
              {    result= entry [givenPosition];
                   if (givenPosition <length-1)     // optional
                     removeGap (givenPosition); // private method that helps to shift
                                                                          // items in order to remove space
                                                                          // after removing the entry;
                    length--;
              }
          return result;
       }//end remove

private void removeGap (int givenPosition)
        {  for (int index= givenPosition; index <length-1;index++)
              entry [index]=entry[index+1];
         }//end removeGap

public boolean replace (int givenPosition, Object newEntry)
      { boolean isSuccessful = true;
         if (! isEmpty() &&(givenPosition >= 0) && (givenPosition <  length))
            entry [givenPosition] = newEntry;
        else
           isSuccessful = false;
        return isSuccessful;
       } // end replace

public Object getEntry (int givenPosition)
      {
        Object result = null; //  result to return
        if (! isEmpty() &&(givenPosition >= 0) && (givenPosition < length))
           result = entry [givenPosition];
        return result;
       } // end getEntry

public boolean contains (Object anEntry)
      { boolean found = false;
        for (int index = 0; !found && (index < length); index++){
          if (anEntry.equals (entry [index]))
            found = true;
       } // end for
       return found;
      } // end contains

public int getLength()
      { return length;
       }//end getLength

public void clear()
      {   length=0;
      }

public boolean isEmpty()
      {    boolean itEmpty =false;
           if (length ==0)                        // return length==0;
             itEmpty =true;
           return itEmpty;
       }//end isEmpty

public boolean isFull()
      { boolean itFull =false;           // return length = = entry. length;
        if (length== entry. length)
          itFull =true;
        return itFull;
      }// end isFull

 public void display()
       {  for (int index=0;index< length; index++)
          System.out.println (entry [index]);
        }
}//end of class AList

in Application Level .. when i store data to my Array like this

static Alist Amman = new Alist();


public static void main(String[] args) {

   blablablablabla

 Amman.add(x);

An error appears say :

Exception in thread "main" java.lang.NullPointerException
        at Cleint.main(Cleint.java:93)
saifmaher
  • 1
  • 1

1 Answers1

0

You are probably accessing an invalid position of your list, try to use (entry.length - 1) in method isFull(). In addition, you can use >= instead of ==, just to do safe programming.

public boolean isFull()
  {         
    return length >= (entry.length - 1);
  }