• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Null pointer exception in button event handler.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, First time on this forum. Wonder if anyone could help me with a null pointer exception. Thanks, Michael
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi michael, Welcome to CodeRanch First things firsts- Please UseCodeTags to post your source code- You can do it by editing your post.That way the code is more readable. Also please TellTheDetails post the stacktrace for the exception. See that you are not using any of the uninitialized references in the event handler.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The nice thing about NullPointerExceptions is that they're usually pretty straightforward to track down...if you know what line it's happening on.
 
Marshal
Posts: 79703
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:Hi michael, Welcome to CodeRanch

Agree

. . . Please UseCodeTags to post your source code- . . .

Since he's new I have added the code tags.

Also please TellTheDetails post the stacktrace for the exception. See that you are not using any of the uninitialized references in the event handler.

No, it is a much more subtle error than that
 
Campbell Ritchie
Marshal
Posts: 79703
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: . . . usually pretty straightforward to track down... . . .

Who's the incurable optimist then?

There is another error waiting for us whenever we get rid of that Exception.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No, it is a much more subtle error than that


Ooh, it is, isn't it?

Michael - you might want to check the Javadocs for the Integer class - and specifically the getInteger method.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:and specifically the getInteger method.


Incidentally - is it just me, or is that a ridiculous name for a method that does that?
 
Campbell Ritchie
Marshal
Posts: 79703
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and some of the logic. It took about 21 seconds ( ) to work out prime numbers under 150, including 143. I didn't know 143 is a prime number. I always thought it was 11 × 13.
I would expect calculating prime numbers up to 150 to take maybe 1 millisecond.
 
Campbell Ritchie
Marshal
Posts: 79703
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: . . . is that a ridiculous name for a method that does that?

Close examination of the documentation for that method does not reveal a "since" clause. That suggests this method was introduced in the very earliest stages of development of Java™. There were quite a lot of mistakes made in the earlier stages (Joshau Bloch in Effective Java quotes "public class Stack extends Vector" as another example), and the name of that method was obviously one of them. The only way to correct it at this stage would be to add a @Depracted tag to that method and "@deprecated Use Integer#getIntegerFromPropertyName instead" to its documentation comments.
 
michael wrzos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help. Should have used Int.ParseInteger. Mike
 
Campbell Ritchie
Marshal
Posts: 79703
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done finding the problem. What was happening before was
  • You were passing the text, maybe "1234"
  • The System class looks for a system property called "1234"
  • It can't find anything by that name.
  • The getInteger method returns null
  • The JVM attempts to un-box that null, but that is not possible.
  • The Exception is thrown.
  • As we said, a fascinating error.

    Have you got the finding of prime numbers worked out yet? It should take maybe 1 or 2 milliseconds to iterate through several thousand numbers with a Sieve of Eratosthenes. Something is going very wrong if it takes 21 seconds.
     
    michael wrzos
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I am still working on taking the output out of the loop. Right now it is outputing each prime several times, hence the slowdown.Thanks Mike
     
    Campbell Ritchie
    Marshal
    Posts: 79703
    381
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    find out about the sieve of Eratosthenes.
     
    I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic