2017-06-02 63 views
-2

歡迎來到我的難題;)在Java中我有一個問題..我有我的具體清單實施商店對象如何將對象添加到數據結構中的數組列表?

我能做些什麼來解決這個問題?任何身體都可以幫助我並拯救我的一天?

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 
在應用層

..當我將數據存儲到我的數組這樣

static Alist Amman = new Alist(); 


public static void main(String[] args) { 

    blablablablabla 

Amman.add(x); 

出現錯誤說:

Exception in thread "main" java.lang.NullPointerException 
     at Cleint.main(Cleint.java:93) 
+0

的【什麼是一個NullPointerException,如何解決呢?(可能的複製https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do- i-fix-it) –

+0

我試圖遵守這些問題,至今沒有解決方案 – saifmaher

+0

注意NPE發生在Client.java的第93行。你能張貼那條線嗎? –

回答

0

你可能訪問列表的無效位置,嘗試在方法isFull()中使用(entry.length - 1)。另外,您可以使用> =而不是==,只是爲了安全編程。

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