![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
using Text fields in SWT
In my GUI (developed using SWT), I have a Text field called date.
It appears in the window with this code date = new Text(dialogShell, SWT.NONE); date.setBounds(42, 49, 119, 14); date.setEnabled(false); When the window is started, the appropiate date is set. date.setText("1/1/2000");However, when the application is run, it chokes on the setText field, throwing a NullPointerException. I can't figure out why it's throwing that exception, even with an explicitly set date. The code to set the Text appears to follow the code in a SWT book I use for reference. Am I missing some small detail? Why the NullPointerException, and what should be done to fix it? |
|
|
|
|
|
#2 |
|
Expert Programmer
|
I believe the reason you are getting a NullPointerException (assuming the information you provided is correct) is because the variable date is null when you call the setText(String s) method.
You are probably calling date.setText(String s) from a different method than the one you set it in, in which case you need to make sure you have declared date as a class variable and already set its value before calling this method. If you still can't get it to work, posting the whole class on this forum would help. Last edited by titaniumdecoy; Dec 26th, 2005 at 11:58 PM. |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
Quote:
Let me post my code, and see if it helps. I am going to eliminate some of the other SWT widgets in the actual code here just for simplicity. package gui;
import com.cloudgarden.resource.SWTResourceManager;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
public class InsertEvent extends org.eclipse.swt.widgets.Dialog {
private Shell dialogShell;
private Label message;
private Label message2;
private static Text firstSwipe;
private static Text date;
private Label dateL;
private Label lateTimeL;
private Label exc;
private Button ignore;
private Button ok;
private Group specialGrp;
private Text latePts;
private Text ontimePoints;
private Label latePtsL;
private Label onTimePtsL;
private Group regGame;
private Label colon;
private Text min;
private Text hour;
private Combo ensembleType;
private Label ensembleTypeL;
private Combo eventType;
private Label eventTypeL;
private Label firstSwipeL;
/**
* Auto-generated main method to display this org.eclipse.swt.widgets.Dialog
* inside a new Shell.
*/
public static void main(String[] args) {
try {
Display display = Display.getDefault();
Shell shell = new Shell(display);
InsertEvent inst = new InsertEvent(shell, SWT.NULL);
inst.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public InsertEvent(Shell parent, int style) {
super(parent, style);
}
//sets the date and time before opening the window
public static void runFromGui(String d, String t){
/*
date.setText(d.substring(0, 2) + "/" + d.substring(2, 4) + "/"
+ d.substring(4));
firstSwipe.setText(t.substring(0, 2) + ":" + t.substring(2));
String a[] = new String[1];
main(a);
*/
date.setText("1/1/2000");
firstSwipe.setText("00:00");
String a[] = new String[1];
main(a);
}
public void open() {
try {
Shell parent = getParent();
dialogShell = new Shell(parent, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
{
// Register as a resource user - SWTResourceManager will
// handle the obtaining and disposing of resources
SWTResourceManager.registerResourceUser(dialogShell);
}
dialogShell.setLayout(null);
dialogShell.layout();
dialogShell.pack();
dialogShell.setSize(358, 286);
dialogShell.setText("Insert Member");
{
date = new Text(dialogShell, SWT.NONE);
date.setBounds(42, 49, 119, 14);
date.setEnabled(false);
date.setText("");
}
{
firstSwipe = new Text(dialogShell, SWT.NONE);
firstSwipe.setBounds(105, 63, 133, 14);
firstSwipe.setEnabled(false);
firstSwipe.setText("");
}
dialogShell.open();
Display display = dialogShell.getDisplay();
while (!dialogShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Text getDate() {
return date;
}
public Text getFirstSwipe() {
return firstSwipe;
}
}What happens in the grand scope of the application is that when the application detects an event that it does not have data for, it opens this window for the user to fill in the details. Since the program uses the date and time for testing if it knows the details of an event, I'm trying to bring in that date and time into this dialog box so the user knows which date/time the program wants info about. When the application sees it needs to get info about an event, it hits the line //open GUI window, set date and time InsertEvent.runFromGui(date, Integer.toString(time)); The runFromGui method (as opposed to running this app from a command prompt) takes in the date as a String of characters (i.e. 01012000), and formats it so that is readable when displayed on screen (i.e. 01/01/2000). The actual way this should be done in the final product is in the /* quote. I included the explicit setText("1/1/2000") string just to see if the String was going out of bounds, but it wasn't. |
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2005
Posts: 35
Rep Power: 0
![]() |
OK, I realize now that the date variable isn't initalized when I try to set it's value...the date variable is only init in the open() method.
date = new Text(dialogShell, SWT.NONE); I guess the real question now is, where should I init date so I can set its value before opening the window? Also, is there a better way to open the window rather than creating the dummy String array and passing that in to main()? It seemed like an awkard hack when I wrote it. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
I suggest calling open() in the constructor before calling super():
public InsertEvent(Shell parent, int style) {
open();
super(parent, style);
}String[] args is a necessary argument in your application's public static void main(String[] args) method. It is used to read arguments into your application when it is run from a terminal window. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|