Hilfe bei Informatik Hausaufgaben

Hallo Java Community,
Ich komme gerade bei meinen Informatik Hausaufgaben nicht weiter.
Das Programm gibt die ganze Zeit eine NullPointerException Fehlermeldung aus.

Der Quelltext lautet:

package deutsche.bahn;

import java.util.Random;

/**
*
*/
public class Strecke {
Stack<Integer> gleis1= new Stack();
Stack<Integer> gleis2= new Stack();
Stack<Integer> gleis3= new Stack();
Random r = new Random();

public void sortieren(int panzahl){
for(int i=0;i<panzahl;i++){
gleis1.push(r.nextInt(20));
System.out.println(gleis1.top());
}

while(!gleis1.isEmpty()){
while(gleis1.top()>gleis3.top()||gleis3.isEmpty()){
gleis3.push(gleis1.top());
gleis1.pop();
}

//Falls nun es nicht mehr stimmt
while(!gleis1.isEmpty()&&gleis3.top()>gleis1.top()){
gleis2.push(gleis3.top());
gleis3.pop();
}
}


while(!gleis2.isEmpty())
{
while(gleis2.top()>gleis3.top()||gleis3.isEmpty()){
gleis3.push(gleis2.top());
gleis2.pop();
}

//Falls nun es nicht mehr stimmt
while(gleis2.top()<gleis3.top()&&!gleis2.isEmpty()){
gleis1.push(gleis3.top());
gleis3.pop();
}
}

while(!gleis1.isEmpty()){
while(gleis3.top()<gleis1.top()||gleis3.isEmpty()){ //Solange die Reihenfolge stimmt.
gleis3.push(gleis1.top()); //Von Gleis 1 auf Gleis 3
gleis1.pop(); //Aus Gleis 1 gelöscht
}

//Falls nun es nicht mehr stimmt
while(!gleis1.isEmpty()&&gleis3.top()>gleis1.top()){
gleis2.push(gleis3.top());
gleis3.pop();
}
}

}

}
Vielen Dank schon mal jetzt.
 
@mihe7 laut der Ausgabe:
at deutsche.bahn.Strecke.sortieren(Strecke.java:27)
at deutsche.bahn.DeutscheBahn.main(DeutscheBahn.java:21)
ich finde jedoch da nicht wo dort eine NullPointerException seien soll.
 
Kann es sein, dass weiter oben im Stacktrace noch etwas von Stack.java steht?

EDIT: mal andersrum gefragt, was ist das für eine Stack-Klasse?
 
Java:
        for(int i=0;i<panzahl;i++){
            gleis1.push(r.nextInt(20));
            System.out.println(gleis1.top());
        }
oder könnte es etwas mit der zweiten Zeile zu tun haben?
 
Die Steck Klasse:
Java:
public class Stack<ContentTyp> {
    private StackNode head;
   
    private class StackNode {
    private ContentTyp content;
    private StackNode next;
        /**
         * @return the Name
         */
        public ContentTyp getContent() {
            return content;
        }

        /**
         * @param Name the Name to set
         */
        public void setContent(ContentTyp content) {
            this.content = content;
        }

        /**
         * @return the next
         */
        public StackNode getNext() {
            return next;
        }

        /**
         * @param next the next to set
         */
        public void setNext(StackNode next) {
            this.next = next;
        }
       
            public StackNode(ContentTyp pName){
            content=pName;
            next = null;
            }
   
    }
    public Stack (){
        head = null;
    }
    boolean isEmpty(){
        return head==null;
    }
    public void push(ContentTyp pContent){
        StackNode x = new StackNode(pContent);
        if (isEmpty()) {
            head = x;
    }
        else {
            x.setNext(head);
            head = x;
        }   
    }
    public void pop() {
       /* if (isEmpty()) {
            throw new RuntimeException("Die Warteschlange ist leer!");
        }
       */
        if(!isEmpty())head = head.getNext();
    }
    public ContentTyp top(){
        if(isEmpty()){
            return null;
        }
        else{
           
            return head.getContent();
        }
    }
}
 
Das steht im Stacktrace:
Java:
Exception in thread "main" java.lang.NullPointerException
    at deutsche.bahn.Strecke.sortieren(Strecke.java:33)
    at deutsche.bahn.DeutscheBahn.main(DeutscheBahn.java:21)
C:\Users\?\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
 
Ah, ja. Zwei Anmerkungen:

1. Du musst das "Arbeitsgleis" und das "Abstellgleis" ggf. mehrfach wechseln (= dynamisch).
2. Deine Initialisierung sorgt mit hoher Wahrscheinlichkeit dafür, dass eine Zugnummer mehrfach vorkommt. Das darf entweder nicht sein oder Du musst den Vergleich entsprechend anpassen.
 
Ah, ja. Zwei Anmerkungen:

1. Du musst das "Arbeitsgleis" und das "Abstellgleis" ggf. mehrfach wechseln (= dynamisch).
2. Deine Initialisierung sorgt mit hoher Wahrscheinlichkeit dafür, dass eine Zugnummer mehrfach vorkommt. Das darf entweder nicht sein oder Du musst den Vergleich entsprechend anpassen.
Oke vielen Dank dir schon mal.
Jedoch selbst nach dem ich diese beiden Punkte berücksichtigt habe stürzt das Programm mit einem NullPointerException ab.
 

Neue Themen


Zurück
Oben