Auf Thema antworten

großartig, immerhin jetzt keine direkten fehlermeldungen im programm :) ich danke dir soooooooo sehr!

Du hast mir wirklich in vielen Themen die Türen geöffnet, ich kann dir nicht genug danken! Das war sehr sehr nett und ich weiß deine Hilfe sehr zu schätzen!


Aaaaber :)


Wenn ich das Programm starte, bekomme ich leider immernoch eine NullPointerException :noe:


Das ist die Klasse jetzt überarbeitet wurde und die kürzesten Wege in die Matrix aufzeigt:


[code=Java]


public class Solution09<N extends INode, E extends IEdge<N>, G extends IGraph< N, E >> extends APSP<N, G> {



   

   

    private Solution08 solutionGraph;

   

    INode[] nLocations;

   


   

    public Solution09(){

        super();

       

        solutionGraph = new Solution08();

        nLocations = null;

    }



    public boolean computeAPSP(G scenario, Vector<N> locations) {


        int intDik, intDkj, intNew;


       

        if ((solutionGraph != null) && (testLocations(scenario, locations))) {

            // save order of location

            Vector<N> strLocations = locations;

            int LocAnz = strLocations.size();


            initMatrices(scenario);


            // run all iterations

            for (int k = 0; k < LocAnz; k++)

                for (int i = 0; i < LocAnz; i++)

                    for (int j = 0; j < LocAnz; j++) {

                        intDik = intMatrixD[i][k];

                        intDkj = intMatrixD[k][j];


         // check for connection between these locations

if ((intDik < Integer.MAX_VALUE)&&(intDkj < Integer.MAX_VALUE)) {

        

 // compute duration using location k


intNew = intDik + intDkj;


if (intMatrixD[i][j] > intNew) {


intMatrixD[i][j] = intNew;

intMatrixT[i][j] = intMatrixT[k][j];

                            }

                        }

            }


            // computation was successful

            return true;

        }


        // something was wrong

        return false;       

    }


       


    public Vector<N> getShortestPath(N location1, N location2) {


            Vector<N> vecPath;

            int Loc1;

            int Loc2;

           

           

           

            if ((location1 != null) && (location2 != null)){

               

                Loc1 = getLocationIndex(location1);

                Loc2 = getLocationIndex(location2);

               

                if ((Loc1 != -1) && (Loc2 != -1) && (Loc1 != Loc2)) {

                   

                    vecPath = new Vector<N>();

                   

                    do {

                        vecPath.add(0, (N) nLocations[Loc2]);

                        Loc2 = intMatrixT[Loc1][Loc2];

                    } while (Loc2 != Loc1);

                   

                    vecPath.add(0, (N) nLocations[Loc1]);

                   

                   

                   

                   

                    return vecPath;

                       

                    }

                }       

        return null;

    }


    @Override

    public int getShortestPathDuration(N location1, N location2) {

       

        int intLoc1, intLoc2;


        // first, check parameters

        if ((location1 != null) && (location2 != null)) {


            // get indices of both locations.

            intLoc1 = getLocationIndex(location1);

            intLoc2 = getLocationIndex(location2);

            if ((intLoc1 != -1) && (intLoc2 != -1))

                return intMatrixD[intLoc1][intLoc2];

        }


        // something was wrong

        return -1;

   

   


       

    }

   

    private int getLocationIndex(N location) {

       

        ILocation[] locs = new ILocation[((Vector<N>) location).size()];

        ((Vector<N>) location).toArray(locs);

       

        int LocAnz = nLocations.length;

       

       

        if (location != null) {

            int i = LocAnz;

            while (i > 0) {

                return i;

            }

        }


       return -1;

    }   


   

private void initMatrices(G scenario) {

       

        int LocAnz;

        int i;

        int j;

       

        LocAnz =  nLocations.length;

       

       

       

        Vector<N> locationVector = arrayToVector(nLocations);

        intMatrixD = solutionGraph.getAdjacencyMatrix(scenario, locationVector);

        intMatrixT = solutionGraph.getAdjacencyMatrix(scenario, locationVector);   


       

        for (i=0; i<LocAnz; i++)

            for (j=0; j<LocAnz; j++){

                int value = intMatrixT[i][j];

                if ((value<1) || (value == Integer.MAX_VALUE))

                    intMatrixT[i][j]=-1;

                else

                    intMatrixT[i][j]= i;

           

        }

   

    }


public Vector<N> arrayToVector(INode[] nLocations){

    Vector<N> vecLocations = new Vector<N>();

    for(int i = 0; i < nLocations.length; i++){

        vecLocations.add((N) nLocations[i]);

    }

    return vecLocations;

}


   

   

}


[/code]



Das ist die Klasse, die die Matrix, also diese Adjacency Matrix aufbaut und die obrige Klasse auf diese hier zugreift:


[code=Java]


public class Solution08<N extends INode, E extends IEdge<N>, G extends IGraph<N,E>> extends Graph<N,G>{


   


       

   

    public int getDirectDuration(G scenario, N loc1, N loc2) {

        if (scenario != null && loc1 != null && loc2 != null){

            if (loc1.equals(loc2)){

                return 0;

            }

            else {

               

               

                Enumeration<IConnection<ILocation>> enu = ((ILocation) loc1).getConnections().elements();

                while(enu.hasMoreElements()){

                    IConnection<ILocation> element = enu.nextElement();

                    if (element.isConnectedLocation((ILocation) loc1) && element.isConnectedLocation((ILocation) loc2)){

                        return element.getWeight();

                    }

                }

                return Integer.MAX_VALUE;

            }

        }

        return -1;

    }

   

   

   


   

   

   

    public int[][] getAdjacencyMatrix(G scenario, Vector<N> locations) {

       

        ILocation locLoc1;

        ILocation locLoc2;

       

        int LocAnz;

        int i;

        int j;


      

        if ((scenario != null) && (locations != null)) {


           

            LocAnz = ((IScenario) scenario).getLocations().size();


                int [][] matrix = new int[LocAnz][LocAnz];


                ILocation[] locs = new ILocation[locations.size()];

                locations.toArray(locs);

               

               

                try {

                  

                    for (i = 0; i < LocAnz; i++) {

                        locLoc1 = ((IScenario) scenario).getLocation(locs[i].getId());

       

                        for (j = 0; j < LocAnz; j++) {

                            locLoc2 = ((IScenario) scenario).getLocation(locs[j].getId());

                           

                            matrix [i][j] = getDirectDuration((scenario), (N) locLoc1, (N) locLoc2);

                        }

                    }

               } catch (Exception e) {

                  

               }


             

               return matrix;

            }

      

        return null;

    }

   





}


[/code]


Wo kommt denn hier die NullPointerException? Und was kann ich jetzt noch tun, um diese abzufangen?

Ach mensch, kurz davor und doch noch so fern >.<



Oben