• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Program Coding Issue  RSS feed

 
Robert Partridge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello,

Please help me review the following Java code because I get the following error: Exception in thread "main" java.lang.NullPointerException
at SolarSystem.getSolarSystemMoonCount(SolarSystem.java:36)
at Driver.main(Driver.java:39)

I can't see to figure out why the variable has a value of null..



I need output to be similar to this:

You Have 9 Planets In Your REAL Solar System and 178 Moons
The Planet Mercury Has 0 Moon(s)
The Planet Venus Has 0 Moon(s)
The Planet Earth Has 1 Moon(s)
The Planet Mars Has 2 Moon(s)
The Planet Jupiter Has 67 Moon(s)
The Planet Saturn Has 62 Moon(s)
The Planet Uranus Has 27 Moon(s)
The Planet Neptune Has 14 Moon(s)
The Planet Pluto Has 5 Moon(s)

You Have 9 Planets In Your VIRTUAL Solar System and 268171150 Moons
The Planet Walmart Has 4444311 Moon(s)
The Planet K-Mart Has 13342321 Moon(s)
The Planet Target Has 13344411 Moon(s)
The Planet Dollar General Has 134555111 Moon(s)
The Planet Family Dollar Has 6704994 Moon(s)
The Planet Dollar Tree Has 62272772 Moon(s)
The Planet Walgreens Has 27717272 Moon(s)
The Planet CVS Has 141662 Moon(s)
The Planet Costco Has 5648296 Moon(s)

Thank you so much for your help!!
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you have never initialized your array planets. Where you do this:

You have defined plants as a reference to an array of type Planet, but you do not have an array yet. You need to do this:

before you actually have an array that is referenced by the variable planets.

Robert Partridge wrote:

Hello,

Please help me review the following Java code because I get the following error: Exception in thread "main" java.lang.NullPointerException
at SolarSystem.getSolarSystemMoonCount(SolarSystem.java:36)
at Driver.main(Driver.java:39)

I can't see to figure out why the variable has a value of null..



I need output to be similar to this:

You Have 9 Planets In Your REAL Solar System and 178 Moons
The Planet Mercury Has 0 Moon(s)
The Planet Venus Has 0 Moon(s)
The Planet Earth Has 1 Moon(s)
The Planet Mars Has 2 Moon(s)
The Planet Jupiter Has 67 Moon(s)
The Planet Saturn Has 62 Moon(s)
The Planet Uranus Has 27 Moon(s)
The Planet Neptune Has 14 Moon(s)
The Planet Pluto Has 5 Moon(s)

You Have 9 Planets In Your VIRTUAL Solar System and 268171150 Moons
The Planet Walmart Has 4444311 Moon(s)
The Planet K-Mart Has 13342321 Moon(s)
The Planet Target Has 13344411 Moon(s)
The Planet Dollar General Has 134555111 Moon(s)
The Planet Family Dollar Has 6704994 Moon(s)
The Planet Dollar Tree Has 62272772 Moon(s)
The Planet Walgreens Has 27717272 Moon(s)
The Planet CVS Has 141662 Moon(s)
The Planet Costco Has 5648296 Moon(s)

Thank you so much for your help!!
 
Robert Partridge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the reply. I changed that line, but I'm still getting the same error. It's odd because the program would always run if I put the real solar system in comments and uncommented the real solar system section that has the moons as single digit numbers. It doesn't like the moons with higher numbers.
 
Robert Partridge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I got it! Finally! I had duplicate array slot fillers in the driver. Thank you so much for your help!!
 
Robert Partridge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to print the output without the brackets and commas when using util.Arrays?
 
Anil KumarVerma
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,

You need to use a For or While loop.
 
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!