We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,571 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with Java (int to month)

I need a user to input int and get the name of the month in return. I have this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se211.dz14;

import java.text.DateFormatSymbols;
import java.util.Scanner;

/**
 *
 * @author Boris
 */
public class Main {

public String getMonth(int month) {
    return new DateFormatSymbols().getMonths()[month-1];
}

    /**
     * @param args the command line arguments
     */
    


       public static void main(String[] args) {
        // TODO code application logic here
           System.out.println("Please enter month's number:\n");
           Scanner scan = new Scanner(System.in);
           int month = scan.nextInt();
           
           Main input = null;
        String month1 = input.getMonth(month);
        System.out.println(month1);
    }
}

I'm a beginner, and I'm getting a null pointer exception error. How do I solve this?

Many thanks!

4
Contributors
8
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
9
Views
boris90
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

Agreed with Ezzaral, Check Line 32, you are using a variable without even intializing it. Check this page out http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=2.

Majestics
Practically a Master Poster
696 posts since Jul 2007
Reputation Points: 209
Solved Threads: 66
Skill Endorsements: 5

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
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

That completely depends on whether the OP intends to write the program in an object-oriented manner or a procedural manner.

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

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
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

Thanks all for your help, I already managed to solve the problem the other way, using switch. This is what I have now:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se211.dz14;

import java.util.Scanner;


/**
 *
 * @author Boris
 */
public class Month {
    private static int month;


    /**
     * @param args the command line arguments
     */

                Scanner scan = new Scanner(System.in);
    
public String calculateMonth(int month)
{
            System.out.println("Please enter a month number:");
            month = scan.nextInt();

        switch (month) {
            case 1:  System.out.println("January"); break;
            case 2:  System.out.println("February"); break;
            case 3:  System.out.println("March"); break;
            case 4:  System.out.println("April"); break;
            case 5:  System.out.println("May"); break;
            case 6:  System.out.println("June"); break;
            case 7:  System.out.println("July"); break;
            case 8:  System.out.println("August"); break;
            case 9:  System.out.println("September"); break;
            case 10: System.out.println("October"); break;
            case 11: System.out.println("November"); break;
            case 12: System.out.println("December"); break;
            default: System.out.println("Not a month!"); break;
        }
        
        return "Calculation(s) over!";
        
}
    
       public static void main(String[] args) {

           Scanner scan = new Scanner(System.in);          
           
           System.out.println("Please enter a number of times you wish to see the number's month:");

int times = scan.nextInt();

for (int i = 0; i < times; i++){
           
            Month obj = new Month();
            obj.calculateMonth(month);
}
    }
}

Works fine! Thanks anyway, problem solved.

boris90
Newbie Poster
23 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

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
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0796 seconds using 2.72MB