Java一个线程中传递数组时抛出空指针异常,疑似数组没传进来?求助,谢谢!

stshow 2013-01-01 06:07:00
在写一个排序算法演示程序

先在主窗体中的Listener中调用engine:
private class StartListener implements ActionListener{
public void actionPerformed(ActionEvent e){


int[] unsort=UnsortGenerator(); //函数返回给unsort一个数组


engine.setArray(unsort);
engine.setAlgorithm(getAlgorithm());
engine.setSleepTime(jslSpeed.getValue()*50);
engine.setHistogram(histogram); //Histogram是用来画柱状图的
engine.run();
}
}


然后在Engine中:
private SelectionSort selectionSort=new SelectionSort();
...
if (thread != null && thread.getState() != Thread.State.TERMINATED)
return;

thread=new Thread(selectionSort);
selectionSort.setArray(unsort);
thread.start();

在SelectionSort中:
public class SelectionSort implements Runnable{
private int[] unsort;
private Histogram histogram;
private int sleepTime;

public void run(){
selectionSort();
}

public void setArray(int[] unsort){
this.unsort=unsort;
}

private void selectionSort(){

int key=0;
int count=0;
while(count<unsort.length-1){
for(int i=key+1;i<unsort.length;i++){
if(unsort[key]>unsort[i])
key=i;
}
int temp=unsort[count];
unsort[count]=unsort[key];
unsort[key]=temp;

count++;
key=count;

for(int i=0;i<unsort.length;i++)
System.out.printf("%d ",unsort[i]);
System.out.print('\n');//这里可以输出正常数组

histogram.showHistogram(unsort);//这里会抛出异常

try {
Thread.sleep(sleepTime);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}




请问这段代码哪里有问题?如何修改?
提前感谢!
...全文
306 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
stshow 2013-01-01
  • 打赏
  • 举报
回复
引用 3 楼 xodbc 的回复:
引用 2 楼 stshow 的回复:引用 1 楼 xodbc 的回复:异常中第36行在你贴的代码中是第多少行? 最后一段代码第34行标住的地方 sorry没注意看注释,按异常来看应该是histogram这个为null了,与数组无关,如果是数组,那异常应该在showHistogram这个方法中。
啊,的确是的!太感谢您了,我都对着这代码看了一天了,终于搞好了~
为啥呢 2013-01-01
  • 打赏
  • 举报
回复
引用 2 楼 stshow 的回复:
引用 1 楼 xodbc 的回复:异常中第36行在你贴的代码中是第多少行? 最后一段代码第34行标住的地方
sorry没注意看注释,按异常来看应该是histogram这个为null了,与数组无关,如果是数组,那异常应该在showHistogram这个方法中。
stshow 2013-01-01
  • 打赏
  • 举报
回复
引用 1 楼 xodbc 的回复:
异常中第36行在你贴的代码中是第多少行?
最后一段代码第34行标住的地方
为啥呢 2013-01-01
  • 打赏
  • 举报
回复
异常中第36行在你贴的代码中是第多少行?

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧