user1786288
user1786288

Reputation: 21

Java Null pointer exception in constructor call

I apologize if this has been answered in some way somewhere else, but I'm not well enough versed in java to identify a similar problem from some other situation.

It might be tough to get all the information here, so I'll try to get the essentials in.

I'm writing a Calendar program, and when I call the constructor in the demo class:

        System.out.println("Enter month (1 - 12)");
    month = console.nextInt();

    System.out.println("Enter year (> 1500)");
    year = console.nextInt();

    Calendar myCalendar = new Calendar(month, year);

I get a null pointer exception at the line where I call the constructor. I also get one at the first function call in the constructor itself:

    public Calendar(int m, int y)
{
    firstDate.setDate(m, 1, y);
    firstDayOfMonth();
}

The second number in the setDate method call is 1 because I'm using a date class I already wrote, and for the calendar, only the month and year are required, so I set the day to be 1.

After that, my error messages become less intelligible to me, as follows:

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

I am using DrJava to do this, if that makes any difference. If anyone can help me understand what's going on so I can work on fixing the issue, that would be great. I know that there may not be enough information here to provide the actual solution, but if I understood what was going on, I might be able to figure it out with a little bit of extra research. I understand that a null pointer exception occurs when a function is called with a parameter that hasn't been initialized, but backtracking through my code, following the method calls, I couldn't find anything that hadn't been defined anywhere. I think that those other error messages would help me know where the null pointer is coming from.

Thanks in advance, and apologies if I've broken some rule here.

Edit1: I am initializing firstDate as follows: `private ExtDate firstDate;' I'll try those Print statements, maybe that will help.

Edit2: All right, I think I've got it. Thanks for the help. Learning process and all that.

Upvotes: 1

Views: 14873

Answers (1)

Quirin
Quirin

Reputation: 738

NullPointerException occures, when you use null somewhere, where you should use an object. (For more details see the reference).

In nutshell: somewhere in your code you have a null reference instead of an object. You might didn't initialize it before, and that's why you got that exception.

Regarding to your error statement, that object is probably firstDate. You can check it with a System.out.println(firstDate); call, before the firstDate.setDate(m, 1, y); call. Probably it will print out null.

Upvotes: 4

Related Questions