java - SWT - asyncExec 将输出定向到列表小部件

标签 java list swt java-threads

我正在尝试使用 Display.asyncExec() 方法在生成 SWT 列表小部件时将状态更新打印到它们,而不是在程序完成时将更新转储到列表中。我不断收到 java.lang.NullPointerException。我不明白我做错了什么。该程序工作正常,直到我尝试使用单独的线程并使用 Display.asyncExec 方法。任何建议表示赞赏。

错误:“异常:从线程“Thread-0”中的 UncaughtExceptionHandler 抛出 java.lang.NullPointerException”

我的 CustomOutputStream 类:

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 类(class)中:

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

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();
            // ...
        }
        // ...
    }

}

关于java - SWT - asyncExec 将输出定向到列表小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875717/

相关文章:

java - 互补的泛型类型

Javascript 列表和数组索引

java - 如何在 Eclipse RCP 应用程序中使用用户输入?

java - 如何在Java中使用boolean contains(Charsequence s)方法?

java - 将用户输入更改为小写

java - 2腿(客户端凭据)oauth2服务器的 Spring 安全上下文设置

python:制作列表列表时,我必须将它们包含在方括号中吗?

python - 如何将列表转换为字典?

java - 如何以编程方式禁用覆盖滚动条 (Ubuntu Java)

java - 滚动 Pane 到图