链表中元素的频率

我有一个类型点的链接列表我想计算特定点的频率

LinkedList refernce = new LinkedList(); Point neworigin = new Point(); public void distancecalculator(char [][]problem ,LinkedList refernce) { //Somewhere in my code for(int i = 0; i < 4; i++) { int a = reference.x + x[i]; // x={ 0 , 0 ,1 , -1} int b = reference.y + y[i]; // y ={ 1, -1 , 0 ,0} neworigin.x = a; neworigin.y = b; reference.add(neworigin) if(Collections.frequency(refernce, neworigin) < 6) { //End the that thread } else { solver s = new solver(newproblerm , refernce ); som = new Thread(s); som.start(); } 

}}

错误:

  at java.lang.Thread.run(Unknown Source) Exception in thread "Thread-742" java.lang.NullPointerException at java.util.LinkedList$ListItr.next(Unknown Source) at java.util.Collections.frequency(Unknown Source) 

提出来了。请帮帮我。

您正在使用来自多个线程的LinkedListLinkedList的javadoc明确指出(以粗体显示):

请注意,此实现不同步。 如果多个线程同时访问链表,并且至少有一个线程在结构上修改了列表,则必须在外部进行同步。

你的post名称“吓到”我:

线程“Thread- 742 ”中的exceptionjava.lang.NullPointerException

看起来你有很多 (数百?)个线程。 您同时访问LinkedList并且其内部状态被破坏的可能性很高。

然后Collections.frequency()尝试使用其迭代器遍历列表,该迭代器在其实现中命中null值可能是由于LinkedList被破坏。

如果没有正确的同步,请不要使用多个线程中的LinkedList