java.lang.NullPointerException [message #456640] |
Mon, 06 June 2005 13:49 |
MP Messages: 6 Registered: July 2009 |
Junior Member |
|
|
I'm afraid a real noob question. Why do I get a null pointer exception as
soon as I try to run this code snippet.
public class test implements SelectionListener {
private TabItem tabItem_4;
private TabItem tabItem_3;
private TabFolder tabFolder_2;
private TabItem tabItem_1;
private TabItem tabItem;
private TabFolder tabFolder;
private Composite rightcomposite;
private Composite leftcomposite;
private SashForm sashForm;
protected Shell shell;
public static void main(String[] args) {
try {
test window = new test();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
protected void createContents() {
shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(500, 375);
shell.setText("SWT Application");
sashForm = new SashForm(shell, SWT.NONE);
leftcomposite = new Composite(sashForm, SWT.NONE);
leftcomposite.setLayout(new FillLayout());
tabFolder = new TabFolder(leftcomposite, SWT.NONE);
tabFolder.addSelectionListener(this);
tabItem_1 = new TabItem(tabFolder, SWT.NONE);
tabItem_1.setText("Item1");
tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("Item2");
rightcomposite = new Composite(sashForm, SWT.NONE);
rightcomposite.setLayout(new FillLayout());
tabFolder_2 = new TabFolder(rightcomposite, SWT.NONE);
tabItem_3 = new TabItem(tabFolder_2, SWT.NONE);
tabItem_3.setText("ItemA");
tabItem_4 = new TabItem(tabFolder_2, SWT.NONE);
tabItem_4.setText("ItemB");
sashForm.setWeights(new int[] { 1, 1 });
tabFolder.setSelection(0);
}
public void widgetSelected(SelectionEvent e) {
if (this.tabFolder.getSelectionIndex() == 0) {
System.out.println("selection index 0");
tabFolder_2.setVisible(true);
} else {
tabFolder_2.setVisible(false);
System.out.println("selection index 1");
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
}
|
|
|
|
|
Re: java.lang.NullPointerException [message #456675 is a reply to message #456640] |
Mon, 06 June 2005 15:19 |
Steve Northover Messages: 1636 Registered: July 2009 |
Senior Member |
|
|
You are getting selection notification when the first item is added because
it is seleted. You can avoid this by adding the selection listener after
all of the items have been created. Hope this helps.
"MP" <pijnmar@home.nl> wrote in message
news:32a7b28de53e741fd45aca8ce6469441$1@www.eclipse.org...
> I'm afraid a real noob question. Why do I get a null pointer exception as
> soon as I try to run this code snippet.
>
> public class test implements SelectionListener {
>
> private TabItem tabItem_4;
> private TabItem tabItem_3;
> private TabFolder tabFolder_2;
> private TabItem tabItem_1;
> private TabItem tabItem;
> private TabFolder tabFolder;
> private Composite rightcomposite;
> private Composite leftcomposite;
> private SashForm sashForm;
> protected Shell shell;
>
> public static void main(String[] args) {
> try {
> test window = new test();
> window.open();
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> public void open() {
> final Display display = Display.getDefault();
> createContents();
> shell.open();
> shell.layout();
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> }
>
> protected void createContents() {
> shell = new Shell();
> shell.setLayout(new FillLayout());
> shell.setSize(500, 375);
> shell.setText("SWT Application");
>
> sashForm = new SashForm(shell, SWT.NONE);
>
> leftcomposite = new Composite(sashForm, SWT.NONE);
> leftcomposite.setLayout(new FillLayout());
>
> tabFolder = new TabFolder(leftcomposite, SWT.NONE);
> tabFolder.addSelectionListener(this);
>
> tabItem_1 = new TabItem(tabFolder, SWT.NONE);
> tabItem_1.setText("Item1");
>
> tabItem = new TabItem(tabFolder, SWT.NONE);
> tabItem.setText("Item2");
>
> rightcomposite = new Composite(sashForm, SWT.NONE);
> rightcomposite.setLayout(new FillLayout());
>
> tabFolder_2 = new TabFolder(rightcomposite, SWT.NONE);
>
> tabItem_3 = new TabItem(tabFolder_2, SWT.NONE);
> tabItem_3.setText("ItemA");
>
> tabItem_4 = new TabItem(tabFolder_2, SWT.NONE);
> tabItem_4.setText("ItemB");
> sashForm.setWeights(new int[] { 1, 1 });
>
> tabFolder.setSelection(0);
> }
> public void widgetSelected(SelectionEvent e) {
>
> if (this.tabFolder.getSelectionIndex() == 0) {
> System.out.println("selection index 0");
> tabFolder_2.setVisible(true);
> } else {
> tabFolder_2.setVisible(false);
> System.out.println("selection index 1");
> }
>
> }
> public void widgetDefaultSelected(SelectionEvent e) {
> }
> }
>
|
|
|
|
Re: java.lang.NullPointerException [message #456684 is a reply to message #456675] |
Mon, 06 June 2005 21:40 |
No real name Messages: 97 Registered: July 2009 |
Member |
|
|
"Steve Northover" <steve_northover@ca.ibm.com> wrote in message
news:d81pid$u5s$1@news.eclipse.org...
> You are getting selection notification when the first item is added
> because
> it is seleted. You can avoid this by adding the selection listener after
> all of the items have been created. Hope this helps.
Is this rather unusual behavior? This type of behavior makes things
difficult to handle. As in AWT, all programatic selections should not
call listeners.
Regards.
>
> "MP" <pijnmar@home.nl> wrote in message
> news:32a7b28de53e741fd45aca8ce6469441$1@www.eclipse.org...
>> I'm afraid a real noob question. Why do I get a null pointer exception as
>> soon as I try to run this code snippet.
>>
>> public class test implements SelectionListener {
>>
>> private TabItem tabItem_4;
>> private TabItem tabItem_3;
>> private TabFolder tabFolder_2;
>> private TabItem tabItem_1;
>> private TabItem tabItem;
>> private TabFolder tabFolder;
>> private Composite rightcomposite;
>> private Composite leftcomposite;
>> private SashForm sashForm;
>> protected Shell shell;
>>
>> public static void main(String[] args) {
>> try {
>> test window = new test();
>> window.open();
>> } catch (Exception e) {
>> e.printStackTrace();
>> }
>> }
>>
>> public void open() {
>> final Display display = Display.getDefault();
>> createContents();
>> shell.open();
>> shell.layout();
>> while (!shell.isDisposed()) {
>> if (!display.readAndDispatch())
>> display.sleep();
>> }
>> }
>>
>> protected void createContents() {
>> shell = new Shell();
>> shell.setLayout(new FillLayout());
>> shell.setSize(500, 375);
>> shell.setText("SWT Application");
>>
>> sashForm = new SashForm(shell, SWT.NONE);
>>
>> leftcomposite = new Composite(sashForm, SWT.NONE);
>> leftcomposite.setLayout(new FillLayout());
>>
>> tabFolder = new TabFolder(leftcomposite, SWT.NONE);
>> tabFolder.addSelectionListener(this);
>>
>> tabItem_1 = new TabItem(tabFolder, SWT.NONE);
>> tabItem_1.setText("Item1");
>>
>> tabItem = new TabItem(tabFolder, SWT.NONE);
>> tabItem.setText("Item2");
>>
>> rightcomposite = new Composite(sashForm, SWT.NONE);
>> rightcomposite.setLayout(new FillLayout());
>>
>> tabFolder_2 = new TabFolder(rightcomposite, SWT.NONE);
>>
>> tabItem_3 = new TabItem(tabFolder_2, SWT.NONE);
>> tabItem_3.setText("ItemA");
>>
>> tabItem_4 = new TabItem(tabFolder_2, SWT.NONE);
>> tabItem_4.setText("ItemB");
>> sashForm.setWeights(new int[] { 1, 1 });
>>
>> tabFolder.setSelection(0);
>> }
>> public void widgetSelected(SelectionEvent e) {
>>
>> if (this.tabFolder.getSelectionIndex() == 0) {
>> System.out.println("selection index 0");
>> tabFolder_2.setVisible(true);
>> } else {
>> tabFolder_2.setVisible(false);
>> System.out.println("selection index 1");
>> }
>>
>> }
>> public void widgetDefaultSelected(SelectionEvent e) {
>> }
>> }
>>
>
>
|
|
|
Re: java.lang.NullPointerException [message #456982 is a reply to message #456684] |
Mon, 13 June 2005 20:19 |
Steve Northover Messages: 1636 Registered: July 2009 |
Senior Member |
|
|
It wasn't a programatic selection. When the first item is added to a tab
folder, Windows makes that tab be selected. The application did not call
setSelection().
"Joe Smith" <ohcho@tpg.com.au> wrote in message
news:d82fr2$s2i$1@news.eclipse.org...
>
> "Steve Northover" <steve_northover@ca.ibm.com> wrote in message
> news:d81pid$u5s$1@news.eclipse.org...
> > You are getting selection notification when the first item is added
> > because
> > it is seleted. You can avoid this by adding the selection listener
after
> > all of the items have been created. Hope this helps.
>
> Is this rather unusual behavior? This type of behavior makes things
> difficult to handle. As in AWT, all programatic selections should not
> call listeners.
>
> Regards.
>
> >
> > "MP" <pijnmar@home.nl> wrote in message
> > news:32a7b28de53e741fd45aca8ce6469441$1@www.eclipse.org...
> >> I'm afraid a real noob question. Why do I get a null pointer exception
as
> >> soon as I try to run this code snippet.
> >>
> >> public class test implements SelectionListener {
> >>
> >> private TabItem tabItem_4;
> >> private TabItem tabItem_3;
> >> private TabFolder tabFolder_2;
> >> private TabItem tabItem_1;
> >> private TabItem tabItem;
> >> private TabFolder tabFolder;
> >> private Composite rightcomposite;
> >> private Composite leftcomposite;
> >> private SashForm sashForm;
> >> protected Shell shell;
> >>
> >> public static void main(String[] args) {
> >> try {
> >> test window = new test();
> >> window.open();
> >> } catch (Exception e) {
> >> e.printStackTrace();
> >> }
> >> }
> >>
> >> public void open() {
> >> final Display display = Display.getDefault();
> >> createContents();
> >> shell.open();
> >> shell.layout();
> >> while (!shell.isDisposed()) {
> >> if (!display.readAndDispatch())
> >> display.sleep();
> >> }
> >> }
> >>
> >> protected void createContents() {
> >> shell = new Shell();
> >> shell.setLayout(new FillLayout());
> >> shell.setSize(500, 375);
> >> shell.setText("SWT Application");
> >>
> >> sashForm = new SashForm(shell, SWT.NONE);
> >>
> >> leftcomposite = new Composite(sashForm, SWT.NONE);
> >> leftcomposite.setLayout(new FillLayout());
> >>
> >> tabFolder = new TabFolder(leftcomposite, SWT.NONE);
> >> tabFolder.addSelectionListener(this);
> >>
> >> tabItem_1 = new TabItem(tabFolder, SWT.NONE);
> >> tabItem_1.setText("Item1");
> >>
> >> tabItem = new TabItem(tabFolder, SWT.NONE);
> >> tabItem.setText("Item2");
> >>
> >> rightcomposite = new Composite(sashForm, SWT.NONE);
> >> rightcomposite.setLayout(new FillLayout());
> >>
> >> tabFolder_2 = new TabFolder(rightcomposite, SWT.NONE);
> >>
> >> tabItem_3 = new TabItem(tabFolder_2, SWT.NONE);
> >> tabItem_3.setText("ItemA");
> >>
> >> tabItem_4 = new TabItem(tabFolder_2, SWT.NONE);
> >> tabItem_4.setText("ItemB");
> >> sashForm.setWeights(new int[] { 1, 1 });
> >>
> >> tabFolder.setSelection(0);
> >> }
> >> public void widgetSelected(SelectionEvent e) {
> >>
> >> if (this.tabFolder.getSelectionIndex() == 0) {
> >> System.out.println("selection index 0");
> >> tabFolder_2.setVisible(true);
> >> } else {
> >> tabFolder_2.setVisible(false);
> >> System.out.println("selection index 1");
> >> }
> >>
> >> }
> >> public void widgetDefaultSelected(SelectionEvent e) {
> >> }
> >> }
> >>
> >
> >
>
>
|
|
|
Powered by
FUDForum. Page generated in 0.04783 seconds