Witam, napisałem prosty stos, i niestety otrzymuje NullPointerException...
Co przeoczyłem, czego nie zainicjowałem:
public class MojStos {
Object tab[];
int counter;
public MojStos(int wielkosc){
Object tab[] = new Object[wielkosc];
counter = 0;
}
public void push(Object o)throws StosPrzepelnionyException{
if(tab.length>counter){
tab[counter++]=o;
} else {
throw new StosPrzepelnionyException();
}
}
public Object pop() throws StosPustyException{
if(counter<=0){
throw new StosPustyException();
} else {
return tab[--counter];
}
}
public void show(){
/*for(Object item : tab){
System.out.println(tab[item]);
}*/
System.out.println("===============");
for(int i=0;i<=tab.length;i++){
System.out.println(tab[i]);
}
System.out.println("===============");
}
}
Klasa Main
public class Main {
public static void main(String[] args) {
MojStos ms = new MojStos(3);
try {
ms.push(new Integer(8));
} catch (StosPrzepelnionyException e) {
e.printStackTrace();
}
}
}
adaszewski95filemonczyk