Java SWT-asyncExec将输出定向到列表小部件
我尝试使用Display.asyncExec()方法在生成SWT列表小部件时将状态更新打印到该小部件,而不是在程序完成时将更新转储到列表中。我一直得到一个java.lang.NullPointerException。我不知道我做错了什么。在我尝试使用单独的线程和Display.asyncExec方法之前,该程序运行良好。如有任何建议,我们将不胜感激 错误:“异常:java.lang.NullPointerException从线程“thread-0”中的UncaughtExceptionHandler引发” 我的CustomOutputStream类:Java SWT-asyncExec将输出定向到列表小部件,java,list,swt,java-threads,Java,List,Swt,Java Threads,我尝试使用Display.asyncExec()方法在生成SWT列表小部件时将状态更新打印到该小部件,而不是在程序完成时将更新转储到列表中。我一直得到一个java.lang.NullPointerException。我不知道我做错了什么。在我尝试使用单独的线程和Display.asyncExec方法之前,该程序运行良好。如有任何建议,我们将不胜感激 错误:“异常:java.lang.NullPointerException从线程“thread-0”中的UncaughtExceptionHandl
import java.io.IOException;
import java.io.OutputStream;
import org.eclipse.swt.widgets.List;
/**
This class extends from OutputStream to redirect output to a SWT List widget
**/
public class CustomOutputStream extends OutputStream {
private List list;
private StringBuilder sb;
private Runnable runnable;
public CustomOutputStream(List list) {
this.list = list;
sb = new StringBuilder();
}
@Override
public void write(int b) {
// redirects data to the text area upon newline character
if(String.valueOf((char)b).equals("\n") ){
list.getDisplay().asyncExec(new Runnable() {
public void run() {
if(!list.isDisposed() ) {
// redirects data to the text area
list.add(sb.toString());
// scrolls the text area to the end of data
list.select(list.getItemCount() - 1);
list.showSelection();
list.deselectAll();
}
}
});
runnable.run();
sb.setLength(0);
}
else{
sb.append(String.valueOf((char)b));
}
}
}
在我的DVS课程中:
public class DVS implements Runnable{
private PrintStream log;
// constructor
public DVS( CustomOutputStream LOG) {
log = new PrintStream(LOG);
run();
}
public void run() {
System.setOut(log);
System.setErr(log);
/**
code that invokes System.out.println() ....
**/
}
}
在我的主课上:
Public List list = new List(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Public CustomOutputStream log = new CustomOutputStream(list);
DVS algo = new DVS(log);
Thread thread = new Thread(algo);
thread.start();
我看不到任何初始化
runnable
的地方,因此当您尝试调用runnable.run()
时,它将是null
我看不到任何初始化
runnable
的地方,因此当您尝试调用runnable.run()
时,它将是null
发布NullPointerException的堆栈跟踪可能会有帮助。它是:Exception:java.lang.NullPointerException从线程“thread-0”@JoeO'Neill中的UncaughtExceptionHandler抛出的。完整的堆栈跟踪将有帮助。请将其添加到您的问题中,而不是评论。顺便说一句,这是完整的堆栈跟踪。没有给出其他错误信息。我已将其添加到问题中。发布NullPointerException的堆栈跟踪可能会有所帮助。它是:Exception:java.lang.NullPointerException从线程“thread-0”@JoeO'Neill中的UncaughtExceptionHandler抛出。完整的堆栈跟踪将很有帮助。请将其添加到您的问题中,而不是评论。顺便说一句,这是完整的堆栈跟踪。没有给出其他错误信息。我已将其添加到问题中。
public class CustomOutputStream extends OutputStream {
// ...
private Runnable runnable;
public CustomOutputStream(List list) {
this.list = list;
sb = new StringBuilder();
}
@Override
public void write(int b) {
if(String.valueOf((char)b).equals("\n") ){
// ...
runnable.run();
// ...
}
// ...
}
}