2017-03-03 7 views
0

私は別のプログラム用の簡単なGUIを作成しようとしています。 Eclipseとそのプラグイン 'WindowBuilder'を使ってこれは私が今までに得たものです:Java swt IllegalArgumentException:引数をnullにすることはできません。

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

一つは4つのテキストフィールド(第五、大きな1は、エラーメッセージを表示する唯一の目的のためである)に数字を書き込むためのものです。それらのすべてを満たした後、OKボタンをクリックする必要があります。それに基づいて、いくつかの計算がバックグラウンドで行われます(これが動作するまで関連性のない計算が行われます)。

ただし、代わりに次のようになります。 ボタンをクリックするとすぐに私のウィンドウが自動的に消えます。誰もが私にはないものを見ることができます

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) 

: とEclipseのコンソールには、次のエラーメッセージが表示さ?なぜこうなった?表示される変更はないはずですが、プログラムは 'window.open();'またはdisplay.readAndDispatch()何らかの理由でnullを返しますか?

私はまったく賢明な考えをしていないので、問題が正確に何かについてのヒントは高く評価されます。

答えて

1

エラーメッセージが言うようにです:Argument cannot be null

をそして、あなたがrueckmeldung.setText(null);を持っています。空のラベルにはnullの値は含まれず、空の文字列が含まれているため、代わりにrueckmeldung.setText("");を使用してください。あなたは!...getText().isEmpty()でヌルチェックを交換したい場合があります

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

この行に同じ理由

+0

うわー、私は今信じられないほど愚かだと感じます。どうもありがとうございました!あなたは、数時間の脳破壊の封鎖から私を救った。 :) – Aye