-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)
的【什么是一个NullPointerException,如何解决呢?(可能的复制https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do- i-fix-it) –
我试图遵守这些问题,至今没有解决方案 – saifmaher
注意NPE发生在Client.java的第93行。你能张贴那条线吗? –