Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 26th, 2005, 9:40 PM   #1
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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?
elford is offline   Reply With Quote
Old Dec 26th, 2005, 11:48 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 904
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Dec 27th, 2005, 12:27 AM   #3
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
Quote:
Originally Posted by titaniumdecoy
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.
That could be, as I'm trying to set the date before opening the window on the screen, so it is possible that the date variable isn't initialized.

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;
	}
}
The application shows the date and time in a window, prompting the user for more info about an event that occured at the given date and time (the data comes from a card reader, hence the constant use of the term "swipe"). I'm assuming I would get the same error on the time (labeled as firstSwipe in the code), so I included that as well.

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.
elford is offline   Reply With Quote
Old Dec 27th, 2005, 12:33 AM   #4
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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.
elford is offline   Reply With Quote
Old Dec 27th, 2005, 1:38 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 904
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I suggest calling open() in the constructor before calling super():
    public InsertEvent(Shell parent, int style) {
        open();
        super(parent, style);
    }
You should also remove the unnecessary curly brackets in open().

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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC