Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Array of SWT Buttons(How do I use an array of SWT Buttons)
Array of SWT Buttons [message #909245] Thu, 06 September 2012 15:59 Go to next message
Mike Thomson is currently offline Mike Thomson
Messages: 1
Registered: September 2012
Junior Member
In the code below I try to create an array of Buttons but I get a NullPointerException error when I try to do anything with them. Can anyone help?

package buttonArray;

import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;

public class ButtonArray {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Try Button Array");

// creates the gridlayout
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 1;
shell.setLayout(gridLayout);

// creates the buttons
Button[] button = new Button[3];
// line below causes :
// Exception in thread "main" java.lang.NullPointerException
// at buttonArray.ButtonArray.main(ButtonArray.java:28)
button[0].setParent(shell);

shell.pack();
shell.open();
shell.setSize(700, 700);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Re: Array of SWT Buttons [message #909252 is a reply to message #909245] Thu, 06 September 2012 16:15 Go to previous messageGo to next message
David Wegener is currently offline David Wegener
Messages: 1299
Registered: July 2009
Senior Member
The line Button[] button = new Button[3]; creates an array that will hold 3 buttons, but it doesn't create any buttons. You have to create the buttons. button[0] = new Button(); etc. I think you might be able to use array notation Button[] button = {new Button(), new Button(), new Button()};
Re: Array of SWT Buttons [message #910763 is a reply to message #909252] Mon, 10 September 2012 07:08 Go to previous message
Hamed Mohammadi is currently offline Hamed Mohammadi
Messages: 37
Registered: May 2010
Location: Shiraz - Iran
Member

Instead of line 28 add this:

for( int i = 0; i < button.length; i++ ) {
   button[i] = new Button( shell, SWT.NONE);
   button[i].setText("Button" + i );
}
Previous Topic:Does Eclipse Juno introduced any new Debugging Feature for Java
Next Topic:How to get class properties, methods, and events
Goto Forum:
  


Current Time: Sat Apr 19 18:39:44 EDT 2014

Powered by FUDForum. Page generated in 0.05112 seconds