Java swt IllegalArgumentException : Argument cannot be null

标签 java eclipse user-interface swt windowbuilder

我正在尝试为另一个程序编写一个简单的 GUI。为此使用 Eclipse 和插件“WindowBuilder”。这是我到目前为止得到的:

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;

public class LightGUI {

  protected Shell shell;
  private Text lichterEingabe;
  private Text befehleEingabe;
  private Text ipEingabe;
  private Text portEingabe;

 /**
  * Launch the application.
  * @param args
  */        
  
public static void main(String[] args) {
    try {
        LightGUI window = new LightGUI();
//PROBLEM APPARENTLY HERE: 
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
//...OR HERE:
        if (!display.readAndDispatch()) {
                display.sleep();
        }   
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setBackground(SWTResourceManager.getColor(230, 230, 250));
    shell.setSize(701, 513);
    shell.setText("SWT Application");
    
    Label lichter = new Label(shell, SWT.NONE);
    lichter.setBackground(SWTResourceManager.getColor(230, 230, 250));
    lichter.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    lichter.setBounds(70, 71, 76, 21);
    lichter.setText("Lichter:");
    
    Label befehle = new Label(shell, SWT.NONE);
    befehle.setBackground(SWTResourceManager.getColor(230, 230, 250));
    befehle.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    befehle.setBounds(70, 130, 76, 22);
    befehle.setText("Befehle:");
    
    Label ip = new Label(shell, SWT.NONE);
    ip.setBackground(SWTResourceManager.getColor(230, 230, 250));
    ip.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    ip.setBounds(350, 71, 76, 21);
    ip.setText("IP:");
    
    Label port = new Label(shell, SWT.NONE);
    port.setBackground(SWTResourceManager.getColor(230, 230, 250));
    port.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    port.setBounds(350, 130, 76, 22);
    port.setText("Port:");
    
    lichterEingabe = new Text(shell, SWT.BORDER);
    lichterEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    lichterEingabe.setBounds(177, 71, 88, 28);
    
    befehleEingabe = new Text(shell, SWT.BORDER);
    befehleEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    befehleEingabe.setBounds(177, 124, 88, 28);
    
    ipEingabe = new Text(shell, SWT.BORDER);
    ipEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    ipEingabe.setBounds(456, 71, 88, 28);
    
    portEingabe = new Text(shell, SWT.BORDER);
    portEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    portEingabe.setBounds(455, 124, 89, 28);
    
    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.setForeground(SWTResourceManager.getColor(138, 43, 226));
    btnNewButton.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.BOLD));
    btnNewButton.setBounds(417, 239, 120, 49);
    btnNewButton.setText("OK!");
    
    Label rueckmeldung = new Label(shell, SWT.NONE);
    rueckmeldung.setBackground(SWTResourceManager.getColor(230, 230, 250));
    rueckmeldung.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
    rueckmeldung.setBounds(70, 319, 134, 28);
    rueckmeldung.setText("Rueckmeldung:");
    
    StyledText styledText = new StyledText(shell, SWT.BORDER);
    styledText.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
    styledText.setEditable(false);
    styledText.setBounds(70, 353, 340, 96);
    
    
    btnNewButton.addListener(SWT.Selection, new Listener() {
          public void handleEvent(Event e) {
              switch (e.type) {
              case SWT.Selection:
                rueckmeldung.setText(null);
                if(lichterEingabe.getText()!=null && befehleEingabe.getText()!=null && ipEingabe.getText()!=null && portEingabe.getText()!=null){
                    new Steuerung(Integer.parseInt(lichterEingabe.getText()), Integer.parseInt(befehleEingabe.getText()));
                }
                break;
              }
            }
          });


    }
}

该窗口在启动后可见。这是我的代码的结果:

Small window

第一个用于将数字写入四个文本字段(第五个较大的文本字段仅用于显示错误消息)。填写完所有内容后,必须单击“确定”按钮。基于此,一些计算将在后台进行(在该计算起作用之前,这些计算将不相关)。

但是,会发生以下情况: 单击该按钮后,我的窗口会自动消失。 Eclipse 控制台显示以下错误消息:

java.lang.IllegalArgumentException: Argument cannot be null
at org.eclipse.swt.SWT.error(SWT.java:4514)
at org.eclipse.swt.SWT.error(SWT.java:4448)
at org.eclipse.swt.SWT.error(SWT.java:4419)
at org.eclipse.swt.widgets.Widget.error(Widget.java:482)
at org.eclipse.swt.widgets.Label.setText(Label.java:403)
at LightGUI$1.handleEvent(LightGUI.java:126)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4418)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4236)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3824)
at LightGUI.open(LightGUI.java:49)
at LightGUI.main(LightGUI.java:34)

任何人都可以看到我看不到的东西吗?为什么会发生这种情况?不应该有任何可见的变化,但程序似乎认为“window.open();”或者 display.readAndDispatch() 由于某种原因返回 null?

关于问题到底是什么的提示将不胜感激,因为我什至没有最模糊的想法。

最佳答案

正如错误消息所示:参数不能为空

你有rueckmeldung.setText(null);。请改用 rueckmeldung.setText("");,因为空标签不包含 null 值,而是包含空字符串。

出于同样的原因,这一行

if(lichterEingabe.getText()!=null && befehleEingabe.getText()!=null && ipEingabe.getText()!=null && portEingabe.getText()!=null)

您可能想用 !...getText().isEmpty() 替换空检查

关于Java swt IllegalArgumentException : Argument cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42580145/

相关文章:

java - 为什么后增量适用于包装类

java - 带有 @Scheduled 注释的方法在 Spring Boot 应用程序中不起作用

java - 如何在 Android 中使用 Smack 4.1?

java.sql.SQLSyntaxErrorException : ORA-01747

python - 在 Kivy 中使用 RecycleView 的自定义小部件的对齐问题

Java,火力地堡 : How to search values of an unknown parent

Java HttpServlet请求 : Wait with repsonse

java - 春分应用程序 : log4j broken eclipse console output

java - Jython GUI 的问题

iphone - 将主题应用到 iPhone 应用程序的最佳方式