java.lang.reflect.InvocationTargetException错误

java.lang.reflect.InvocationTargetException错误,java,Java,我正在制作一个机器人迷宫,在那里机器人可以在不撞墙的情况下找到目标。作为一种“回溯”方法,我需要机器人朝相反的方向移动,就像它第一次遇到交叉路口时那样。这是我的代码: import uk.ac.warwick.dcs.maze.logic.IRobot; import java.util.ArrayList; import java.util.*; import java.util.Iterator; public class Explorer { private int pollR

我正在制作一个机器人迷宫,在那里机器人可以在不撞墙的情况下找到目标。作为一种“回溯”方法,我需要机器人朝相反的方向移动,就像它第一次遇到交叉路口时那样。这是我的代码:

import uk.ac.warwick.dcs.maze.logic.IRobot;
import java.util.ArrayList;
import java.util.*;
import java.util.Iterator;

public class Explorer {

    private int pollRun = 0; // Incremented after each pass.
    private RobotData robotData; // Data store for junctions.
    private ArrayList<Integer> nonWallDirections;   
    private ArrayList<Integer> passageDirections; 
    private ArrayList<Integer> beenbeforeDirections;
    private Random random = new Random();
    int [] directions = {IRobot.AHEAD, IRobot.LEFT, IRobot.RIGHT, IRobot.BEHIND};

    public void controlRobot (IRobot robot) {

        // On the first move of the first run of a new maze.
        if ((robot.getRuns() == 0) && (pollRun ==0))
            robotData = new RobotData();
        pollRun++; /* Increment poll run so that the data is not reset 
                        each time the robot moves. */

        int exits = nonwallExits(robot);
        int direction;

        nonWallDirections = new ArrayList<Integer>();
        passageDirections = new ArrayList<Integer>();
        beenbeforeDirections = new ArrayList<Integer>();

            // Adding each direction to the appropriate state ArrayList.
            for(int item : directions) {
                if(robot.look(item) != IRobot.WALL) {
                    nonWallDirections.add(item);
                }
            }

            for(int item : directions) {
                if(robot.look(item) == IRobot.PASSAGE) {
                    passageDirections.add(item);
                }
            }

            for(int item : directions) {
                if(robot.look(item) == IRobot.BEENBEFORE) {
                    beenbeforeDirections.add(item);
                }
            }

        // Calling the appropriate method depending on the number of exits.
        if (exits < 2) {
            direction = deadEnd(robot);
        } else if (exits == 2) {
            direction = corridor(robot);
        } else {
            direction = junction(robot);
            robotData.addJunction(robot);
            robotData.printJunction(robot);
        } 

        robot.face(direction);
    }


    /* The specification advised to have to seperate controls: Explorer and Backtrack
        and a variable explorerMode to switch between them.
        Instead, whenever needed I shall call this backtrack method.
        If at a junction, the robot will head back the junction as to when it first approached it.
        When at a deadend or corridor, it will follow the beenbefore squares until it
        reaches an unexplored path. */
    public int backtrack (IRobot robot) {

        if (nonwallExits(robot) > 2) {
            return robotData.reverseHeading(robot);
        } else {
                do {
                    return nonWallDirections.get(0);
                } while (nonwallExits(robot) == 1);
        }

    }


    //  Deadend method makes the robot follow the only nonwall exit.
    public int deadEnd (IRobot robot) {

        return backtrack(robot);

    }


    /* Corridor method will make the robot follow the one and only passage. 
        The exception is at the start. Sometimes, the robot will start with 
        two passages available to it in which case it will choose one randomly.
        If there is no passage, it will follow the beenbefore squares
        until it reaches an unexplored path.*/
    public int corridor (IRobot robot) {

        if (passageExits(robot) == 1) {
            return passageDirections.get(0);
        } else if (passageExits(robot) == 2) {
            int randomPassage = random.nextInt(passageDirections.size());
            return passageDirections.get(randomPassage);
        } else {
                return backtrack(robot);
        }
    }


    /* Junction method states if there is more than one passage, it will randomly select one.
        This applies to crossroads as well as essentially they are the same.
        If there is no passage, it will follow the beenbefore squares until it reaches an unexplored
        path. */
    public int junction(IRobot robot) {

        if (passageExits(robot) == 1) {
            return passageDirections.get(0);
        } else if (passageExits(robot) > 1) {
            int randomPassage = random.nextInt(passageDirections.size());
            return passageDirections.get(randomPassage);
        } else {
            return backtrack(robot);
        }

    }


    // Calculates number of exits.
    private int nonwallExits (IRobot robot) {

        int nonwallExits = 0;

        for(int item : directions) {
            if(robot.look(item) != IRobot.WALL) {
               nonwallExits++;
            }
        }

        return nonwallExits;
    }


    // Calculates number of passages.
    private int passageExits (IRobot robot) {

        int passageExits = 0;

        for(int item : directions) {
            if(robot.look(item) == IRobot.PASSAGE) {
                passageExits++;
            }
        }

        return passageExits;
    }


    // Calculates number of beenbefores.
    private int beenbeforeExits (IRobot robot) {

        int beenbeforeExits = 0;

        for(int item : directions) {
            if(robot.look(item) == IRobot.PASSAGE) {
                beenbeforeExits++;
            }
        }

        return beenbeforeExits;
    }

    // Resets Junction Counter in RobotData class.
    public int reset() {

        return robotData.resetJunctionCounter();

    }
}

class RobotData { 

    /* It was advised in the specification to include the variable:
        private static int maxJunctions = 10000;
        However, as I am not using arrays, but ArrayLists, I do not 
        need this. */
    private static int junctionCounter = 0;
    private ArrayList<Junction> junctionList = new ArrayList<Junction>();
    private Iterator<Junction> junctionIterator = junctionList.iterator();

    // Resets the Junction counter.
    public int resetJunctionCounter() {

        return junctionCounter = 0;

    }


    // Adds the current junction to the list of arrays.
    public void addJunction(IRobot robot) {

        Junction newJunction = new Junction(robot.getLocation().x, robot.getLocation().y, robot.getHeading());
        junctionList.add(newJunction);
        junctionCounter++;

    }


    // Gets the junction counter for Junction info method in Junction class.
    public int getJunctionCounter (IRobot robot) {

        return junctionCounter;
    }


    // Prints Junction info.
    public void printJunction(IRobot robot) {

        String course = "";
        switch (robot.getHeading()) {
            case IRobot.NORTH:
                course = "NORTH";
                break;
            case IRobot.EAST:
                course = "EAST";
                break;
            case IRobot.SOUTH:
                course = "SOUTH";
                break;
            case IRobot.WEST:
                course = "WEST";
                break;
        }

        System.out.println("Junction " + junctionCounter + " (x=" + robot.getLocation().x + ", y=" + robot.getLocation().y +") heading " + course);

    }

    /* Iterates through the junction arrayList to find the 
        heading of the robot when it first approached the junction. */
    public int searchJunction(IRobot robot) {

        Junction currentJunction = junctionIterator.next(); 
        while (junctionIterator.hasNext()) {
            if ((((currentJunction.x)==(robot.getLocation().x))) && ((currentJunction.y)==(robot.getLocation().y))) 
                break;
        }

        return currentJunction.arrived;
    }


    // Returns the reverse of the heading the robot had when first approaching the junction.
    public int reverseHeading(IRobot robot) {

        int firstHeading = searchJunction(robot);
        int reverseHeading = 1; // Random integer to Iniitalise variable.

        switch (firstHeading) {
                    case IRobot.NORTH:
                        if (robot.getHeading() == IRobot.NORTH)
                            reverseHeading = IRobot.BEHIND;
                        else if (robot.getHeading() == IRobot.EAST)
                            reverseHeading = IRobot.RIGHT;
                        else if (robot.getHeading() == IRobot.SOUTH)
                            reverseHeading = IRobot.AHEAD;
                        else 
                            reverseHeading = IRobot.LEFT;
                    break;

                    case IRobot.EAST:
                        if (robot.getHeading() == IRobot.NORTH)
                            reverseHeading = IRobot.LEFT;
                        else if (robot.getHeading() == IRobot.EAST)
                            reverseHeading = IRobot.BEHIND;
                        else if (robot.getHeading() == IRobot.SOUTH)
                            reverseHeading = IRobot.RIGHT;
                        else 
                            reverseHeading = IRobot.AHEAD;
                    break;

                    case IRobot.SOUTH:
                        if (robot.getHeading() == IRobot.NORTH)
                            reverseHeading = IRobot.AHEAD;
                        else if (robot.getHeading() == IRobot.EAST)
                            reverseHeading = IRobot.LEFT;
                        else if (robot.getHeading() == IRobot.SOUTH)
                            reverseHeading = IRobot.BEHIND;
                        else 
                            reverseHeading = IRobot.RIGHT;
                    break;

                    case IRobot.WEST:
                        if (robot.getHeading() == IRobot.NORTH)
                            reverseHeading = IRobot.RIGHT;
                        else if (robot.getHeading() == IRobot.EAST)
                            reverseHeading = IRobot.AHEAD;
                        else if (robot.getHeading() == IRobot.SOUTH)
                            reverseHeading = IRobot.LEFT;
                        else 
                            reverseHeading = IRobot.BEHIND;
                    break;
                }

        return reverseHeading;

    }

}


class Junction {

    int x;
    int y;
    int arrived;

    public Junction(int xcoord, int ycoord, int course) {

        x = xcoord;
        y = ycoord;
        arrived = course;

    }

}

看起来问题出现在以下代码中-

`public int searchJunction(IRobot robot) {

    Junction currentJunction = junctionIterator.next(); 
    while (junctionIterator.hasNext()) {
        if ((((currentJunction.x)==(robot.getLocation().x))) && ((currentJunction.y)==(robot.getLocation().y))) 
            break;
    }

    return currentJunction.arrived;
}

在调用junctionIterator.hasNext()之前,先调用junctionIterator.next()。迭代器规范规定,只有在调用hasNext()之后才应该调用next()

问题似乎出现在以下代码中-

`public int searchJunction(IRobot robot) {

    Junction currentJunction = junctionIterator.next(); 
    while (junctionIterator.hasNext()) {
        if ((((currentJunction.x)==(robot.getLocation().x))) && ((currentJunction.y)==(robot.getLocation().y))) 
            break;
    }

    return currentJunction.arrived;
}

在调用junctionIterator.hasNext()之前,先调用junctionIterator.next()。迭代器规范规定,只有在调用hasNext()之后才能调用next()

我认为您的
searchJunction()
不正确或不安全,ConcurrentModificationException可能是由于通过
junctionList
的迭代器不正确而引发的。问题应该更多地涉及迭代器,而不是反射

您可以尝试:

  • private迭代器junctionIterator=junctionList.Iterator()没有多大意义,因为初始化RobotData对象时列表为空。尝试将其移动到searchJunction()中
  • 首先选中hasNext(),然后调用
    next()

    public int-searchJunction(IRobot机器人){
    迭代器junctionIterator=junctionList.Iterator();
    while(junctionIterator.hasNext()){
    Junction currentJunction=junctionInitiator.next();
    如果(((currentJunction.x)=(robot.getLocation().x))&((currentJunction.y)=(robot.getLocation().y)))
    打破
    }
    返回当前连接。已到达;
    }
    

  • 我认为您的
    searchJunction()
    不正确或不安全,ConcurrentModificationException可能是由于通过
    junctionList
    的迭代器不正确而引发的。问题应该更多地涉及迭代器,而不是反射

    您可以尝试:

  • private迭代器junctionIterator=junctionList.Iterator()没有多大意义,因为初始化RobotData对象时列表为空。尝试将其移动到searchJunction()中
  • 首先选中hasNext(),然后调用
    next()

    public int-searchJunction(IRobot机器人){
    迭代器junctionIterator=junctionList.Iterator();
    while(junctionIterator.hasNext()){
    Junction currentJunction=junctionInitiator.next();
    如果(((currentJunction.x)=(robot.getLocation().x))&((currentJunction.y)=(robot.getLocation().y)))
    打破
    }
    返回当前连接。已到达;
    }
    

  • 非常感谢你!你真是个天才!我还有一个问题。是否有可能让机器人运行一次迷宫,记住正确的路线,然后在第二次运行时选择正确的路线?如果是的话,我该怎么做?使用LinkedList类型实例变量来保存正确的路径:IRobot.AHEAD/IRobot.LEFT/?如果迷宫可能会改变,那么当返回保存的解决方案时,您需要设计一些映射机制来评估迷宫的身份。嗯,我从来没有听说过LinkedList。介意再详细一点吗?非常感谢!你真是个天才!我还有一个问题。是否有可能让机器人运行一次迷宫,记住正确的路线,然后在第二次运行时选择正确的路线?如果是的话,我该怎么做?使用LinkedList类型实例变量来保存正确的路径:IRobot.AHEAD/IRobot.LEFT/?如果迷宫可能会改变,那么当返回保存的解决方案时,您需要设计一些映射机制来评估迷宫的身份。嗯,我从来没有听说过LinkedList。介意更详细一点吗?请不要删除代码、错误消息或其他相关数据,因为这会使问题对未来的访问者毫无价值。我已将您的问题回滚到以前的状态,如果您以后试图破坏它,我将再次这样做。请不要通过删除您的代码、错误消息或其他相关数据来破坏您的问题,因为这会使该问题对未来的访问者毫无价值。我已将您的问题退回到以前的状态,如果您以后试图诽谤它,我将再次这样做。
    public int searchJunction(IRobot robot) {
        Iterator<Junction> junctionIterator = junctionList.iterator();
        while (junctionIterator.hasNext()) {
            Junction currentJunction = junctionIterator.next(); 
            if ((((currentJunction.x)==(robot.getLocation().x))) && ((currentJunction.y)==(robot.getLocation().y))) 
                break;
        }
    
        return currentJunction.arrived;
    }