• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why my code get error ?

 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, good day, i have compile my code but then it compile successfully, however, it give me the error when i run it ..i post my sample code which cause error java.lang.nullPointerException as stated in compiler



the code run after few time in for loop, and it fail to diplay the word "show me"..can anyone point out my mistake ? thank you !
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say "fails" to display "Show Me" where are you looking for the "Show me". It looks like you are generating some sort of HTML. If this is being run in an app server (and dependent upon the app server) you need to check the server's output log.
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I get some line breaks in those strings? The format of this post is hurting my head...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, one thing that's likely to be a problem is that in every branch of this if-statement, you call a method on prnary[i] and only then do you check if prnary[i] is null -- but by then, it's too late, of course, since trying to call the method will throw a NullPointerException.

It's also kind of messy to check for null at each branch -- why not check at the beginning of the loop and use a "continue" statement if prnany[i] is null? Better yet, use a Collection like a List or Set to more easily let you assemble a group of items without any nulls in the first place.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i have solved the problem ..thanks for reply and comments, the error happen as i didn't initialise the prnary[], what i did is something like this

prnary[]={"","" ..} rather than just using private String[] prnary = new String[sizeprn];
reply
    Bookmark Topic Watch Topic
  • New Topic