• Post Reply Bookmark Topic Watch Topic
  • New Topic

SWT: app close when trying to change text.setText()  RSS feed

 
Joe Jiggs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, im new to Java and have run into a jam.

Im using Eclipse to develop my application.
When ever i try and change the text of a text_box my program crashs with the follow error message:
Exception in thread "main" java.lang.NullPointerException
at MainWin$5.widgetSelected(MainWin.java:301)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:90)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:843)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3125)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2758)
at MainWin.main(MainWin.java:103)


Heres some snippets of my current code:
The article_text string is being populated with text, ive printed it out to a message box and also printed it to the consel. When trying to add/change it ot the textbox i get the error.

Ive tried:
Article_txt.setText(article_text); or Article_txt.setText(article_text.toString());
Article_txt.update();
and
Article_txt.append("test");
Article_txt.update();
and just
Article_txt.append("test"); or Article_txt.setText("test");

The program crashs no matter what.

Small snip, only shows the creating of the texbox and the menu item which the kicks the error when trying ot change the text.






Bigger chunk of code with the core functions, but not the whole class...


Thanks for any insight.

_
[ May 01, 2006: Message edited by: Joe ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Classic error that everybody makes once, and once you've made it, you won't make it again:

You've got two "Article_txt" variables (terrible name, by the way; don't use underscores in Java names, and use camel case for variable names: "articleTxt". Use upper case only for class names!) One Article_txt is the member variable, while the other is a local defined in createContents():

Text Article_txt = new Text (shell, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.WRAP);

You initialized the local, but the member is always null. In the event handler, it's the member you're referring to, and so any attempt to use it gives a NullPointerException.
 
Joe Jiggs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responce, now i just have to do alittle reading on what you said

With this...
(terrible name, by the way; don't use underscores in Java names, and use camel case for variable names: "articleTxt". Use upper case only for class names!)

Why ? is there a "true" problem with that underscore and case or is it just a "standard" which most people just adopt to ?
Just wondering.

Thanks again.
[ May 02, 2006: Message edited by: Joe ]
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Joe-

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe:

Why ? is there a "true" problem with that underscore and case or is it just a "standard" which most people just adopt to ?
Just wondering.


Do please take care of your display name, as Ulf has requested. Did you have a different display name before?

The problem is just that by not following conventions, your code becomes harder for people to read. Remember that the audience for your code is other people, and you want to make it as easy on them as possible.

In Java, the convention is for class names to start with an upper case letter, and variable names with lower case. Therefore, if I see

FooBar.someThing();

I can assume that FooBar is a class, and someThing() is a static method, and always be right, as long as the standard conventions are in use. This affects my expectations of what the code is doing and how it will behave. It takes extra time for me to figure out that FooBar is a variable, and that's wasted time that I could have spent doing something else.
 
Joe Jiggs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you have a different display name before?

No... when i joined i used n/a as my last name and then deleted it before i started posting.

_
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!