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!

Recommended Answers

All 8 Replies

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.

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

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

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.

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.

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.

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.