我有一个使用堆栈操作的函数。本质上,它是通过 ArrayLists 搜索来查找两个城市之间的连接。
例如,如果我想检查芝加哥和杰克逊维尔之间是否有连接:
格式:城市:连接的ArrayList
芝加哥:罗利,波士顿
罗利:达拉斯、亚特兰大
亚特兰大:杰克逊维尔,坦帕
它应该返回(使用它的堆栈):芝加哥、罗利、亚特兰大、杰克逊维尔。
不过,目前它会返回:芝加哥、罗利、达拉斯,然后抛出一个 NPE。
这是代码:
stack.clear();
if(isDirectPath(originCity,destinationCity)){
stack.push(originCity);
stack.push(destinationCity);
return stack;
}
stack = new Stack<City>();
City topCity, nextCity = null;
Iterator iter;
reset(cities);//mark all unvisited
// get iterator to iterate over the arraylist attached to the origin city
try
{
//get the arraylist(as an iterator) connected to origin city , push it on the stack
List<City> list1 = (ArrayList) cities.get(originCity.getName());
originCity.markVisited();
iter = list1.iterator();
stack.push(originCity);
// store the top city on the stack in topCity
topCity = stack.peek();
// search cityList
// examines all the cities connected to topCity until destination city is found (or not!)
while(!stack.isEmpty() && !topCity.equals(destinationCity))
{
// nextcity method traveres the arraylist look for first unvisited city
if((nextCity = getNextCity(iter)) == null)// no more unvisited cities connect to topCity
{ //so if there are no more unvisited cities connected to topCity,
// pop stack and store the next city on the stack in topCity
stack.pop();
if (!stack.isEmpty())
topCity = stack.peek(); }// close if nextCity
else // there is another city connected to topCity that is unvisited
{
nextCity.markVisited(); // mark it as visited
//if the city is not already on the stack, store it on the stack and in topCity
if (!stack.contains(nextCity))
{
stack.push(nextCity);
System.out.println(nextCity);
topCity = nextCity;
}
else // the city is already on the stack, so get another topCity from the stack
{
stack.pop();
topCity = stack.peek();
}
} // close outer else
// get arraylist connected to topCity
list1 = (ArrayList) cities.get(topCity.getName());
iter = list1.iterator();
}// close while
}
catch(NullPointerException e)
{
System.out.println("city Not Found: ") ; // !!FILE_NOT_FOUND
}
return stack; //An empty stack indicates no connections
} // end isPath
似乎正在发生的事情是,在下一行......
if((nextCity = getNextCity(iter)) == null)
该函数无法检查 null 条件,而只是捕获了异常。这会导致堆栈按原样返回,而不是按照预期的方式正确执行。
我什至不确定这是否正在发生,但这是我最好的猜测。有谁知道这里有什么问题?
这是 getNextCity 函数:
public City getNextCity(Iterator iter )
{
City nextCity = null;
// determines if there are more cities to process and return null if not
// if there are more cities, gets the next city. If this city is already visited,
// returns null, otherwise returns next city
while(iter.hasNext() && (nextCity=(City)iter.next()).hasBeenVisited());
return nextCity;
}
这是错误堆栈跟踪:
java.lang.NullPointerException
at FlightMap.isPath(FlightMap.java:185)
at ControlPanel$ActionHandler.actionPerformed(ControlPanel.java:265)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
city Not Found:
[Ljava.lang.StackTraceElement;@4bc6c86
第 185 行是iter = list1.iterator();
ControlPanel 错误只是对函数的调用。