Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » java.lang.NullPointerException
java.lang.NullPointerException [message #456640] Mon, 06 June 2005 13:49 Go to next message
MP is currently offline MPFriend
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 #456641 is a reply to message #456640] Mon, 06 June 2005 13:53 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

It would help if you highlighted what line threw the NPE. Without that
it is difficult to determine.

--
Thanks,
Rich Kulp
Re: java.lang.NullPointerException [message #456643 is a reply to message #456640] Mon, 06 June 2005 14:00 Go to previous messageGo to next message
MP is currently offline MPFriend
Messages: 6
Registered: July 2009
Junior Member
Sorry error message below:

java.lang.NullPointerException
selection index 0
at test.widgetSelected(test.java:96)
at
org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListe ner.java:89)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java :82)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:796)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:820)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:805)
at org.eclipse.swt.widgets.TabFolder.createItem(TabFolder.java: 240)
at org.eclipse.swt.widgets.TabItem.<init>(TabItem.java:70)
at test.createContents(test.java:72)
at test.open(test.java:49)
at test.main(test.java:41)
Re: java.lang.NullPointerException [message #456675 is a reply to message #456640] Mon, 06 June 2005 15:19 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
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 #456678 is a reply to message #456675] Mon, 06 June 2005 16:15 Go to previous messageGo to next message
MP is currently offline MPFriend
Messages: 6
Registered: July 2009
Junior Member
Thank you very much that did solve it :-)
Re: java.lang.NullPointerException [message #456684 is a reply to message #456675] Mon, 06 June 2005 21:40 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
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 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
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) {
> >> }
> >> }
> >>
> >
> >
>
>
Previous Topic:StyledText doesn't recognise SWT.FLAT
Next Topic:how to change Button foreground color?
Goto Forum:
  


Current Time: Fri Apr 26 21:48:04 GMT 2024

Powered by FUDForum. Page generated in 0.04783 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top