• 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
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How to generate the Random numbers at runtime

 
Greenhorn
Posts: 18
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not been successful in running this code, it is showing NPE at the part where i add elements to a HashMap. I am also confused about how to generate the Random ID's during runtime. It is my beginner level so please feel free to sort out almost anything. Thanks.








The stack trace is:


 
Saloon Keeper
Posts: 8447
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here you create a reference to a HashMap, but so far the reference hasn't been initialized to anything.
You'll want something like

or, better still

And 'o' is not a descriptive variable name.
 
Marshal
Posts: 73745
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
flag = 1 ???
You are writing Java┬« not C. You have a boolean type which you should use rather than messing around with 0=false and non‑0=true. Also flag is a very poor name for a flag variable; nobody knows what it means.
Why is that Map static? If you don't have a good reason to make something static, then it is a serious mistake.
Where are you using random numbers? The thread asks abut random numbers but I can't see any random numbers anywhere.
 
Debdeep Ganguly
Greenhorn
Posts: 18
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Carrey Brown

Thanks for pointing me that silly one out.
I will try to be careful next time.

@ Campbell Ritchie

Thank you for highlighting my misconception. I am learning to code in Java now and this will really be in the top of list of my do's and don't's.
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would make your instance variables private unless there is some good reason not to. For example:

Likewise, you should have access modifiers on your methods. I assume that menu() should be public and all the rest should be private.

Line 73, the variable r is never used.

Line 185, it looks like you have recursion that you really don't want. I would find a way to loop back to the top of the method rather than call it again. I'm also seeing that the methods you call from menu() call menu() again at the end. I would probably put a loop in menu() and have the other methods just return. All this will keep your stack memory cleaner.
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic