Null pointer exceptions occur when you try to call a method on an object that is null. Try to determine why that is happening in your code.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Majestics
Practically a Master Poster
621 posts since Jul 2007
Reputation Points: 199
Solved Threads: 49
i'd suggest making the method getMonth() static... also why are you calling getMonth() using the class name if its in the same class?
int month = scan.nextInt();
System.out.println(getMonth(month));
the above code will be just fine if the getMonth() method is static.
however if you do not want to make it static for what ever reason call the class itself and dont assign it:
String month1 = new Main().getMonth(month);
System.out.println(month1);
another way would be to do this:
Main input=new Main();
as you can see in the above the main differnce is i didnt declare it null
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
That completely depends on whether the OP intends to write the program in an object-oriented manner or a procedural manner.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
That completely depends on whether the OP intends to write the program in an object-oriented manner or a procedural manner.
Yes i do understand Ezzaral, thats why i showed the three possibilities to call the class without getting an error or null pointer.
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
Yes i do understand Ezzaral, thats why i showed the three possibilities to call the class without getting an error or null pointer.
But you added those in an edit after I posted ;)
I didn't say your original post was wrong. He can certain make it work by changing the method to static. I just pointed out that it may not apply if he was trying to use objects and methods.
You did however give him three solutions to the program without having to think about correcting anything at all himself.
@boris: Glad you resolved your errors.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
But you added those in an edit after I posted ;)
I didn't say your original post was wrong. He can certain make it work by changing the method to static. I just pointed out that it may not apply if he was trying to use objects and methods.
You did however give him three solutions to the program without having to think about correcting anything at all himself.
sorry ezzaral i didnt know you posted is before i edited it....
and although i gave him the soultion without thinking i didnt make it obvious in my defense :)
@OP: thats great you got it working by yourself, but you just added more uneccessary code when what you had worked great and was the shortest way(which in programming quickest is best-in my opinion)
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169