【问题标题】:java.lang.reflect.InvocationTargetException errorjava.lang.reflect.InvocationTargetException 错误
【发布时间】:2014-12-05 03:00:23
【问题描述】:

我正在研究一个机器人迷宫,机器人可以在其中找到目标而不会撞到墙壁。作为一种“回溯”方法,我需要机器人像第一次遇到路口时那样朝相反的方向前进。这是我的代码:

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;

    }

}

每当它回溯并到达它已经访问过的路口时,它就会冻结并出现。

`java.lang.reflect.InvocationTargetException
    at sun.reflect.GeneratedMethodAccessor41.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at uk.ac.warwick.dcs.maze.controllers.PolledControllerWrapper.start(PolledControllerWrapper.java:70)
    at uk.ac.warwick.dcs.maze.logic.ControllerThread.run(ControllerThread.java:46)
Caused by: java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at RobotData.searchJunction(Explorer.java:242)
    at RobotData.reverseHeading(Explorer.java:255)
    at Explorer.backtrack(Explorer.java:74)
    at Explorer.junction(Explorer.java:122)
    at Explorer.controlRobot(Explorer.java:56)
    ... 5 more`

【问题讨论】:

  • 请不要通过删除您的代码、错误消息或其他相关数据来破坏您的问题,因为这会使问题对未来的访问者完全毫无价值。我已将您的问题回滚到以前的状态,如果您将来试图破坏它,我会再次这样做。

标签: java


【解决方案1】:

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

你可以试试:

  1. private Iterator&lt;Junction&gt; junctionIterator = junctionList.iterator(); 没有多大意义,因为在初始化 RobotData 对象时列表为空。尝试将其移入 searchJunction()
  2. 先检查hasNext(),然后调用next()

    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;
    }
    

【讨论】:

  • 非常感谢!你真是个天才!我还有一个问题。是否有可能让机器人跑一次迷宫,记住正确的路线,然后在第二次运行时走正确的路线?如果是这样,我该怎么做?
  • 如何使用 LinkedList 类型的实例变量来保存正确的路径:IRobot.AHEAD/IRobot.LEFT/.....?而如果迷宫可能发生变化,则需要设计一些映射机制,在返回保存的解时评估迷宫的身份。
  • 嗯,我从未听说过 LinkedList。介意更详细一点吗?
【解决方案2】:

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

`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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2021-08-21
    • 2021-04-30
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多