1

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.

6
  • 9
    my first guess is firstDate is null Commented Feb 24, 2013 at 22:06
  • 1
    firstDate or console is null. hard to tell from what you've posted. Commented Feb 24, 2013 at 22:06
  • How are you initializing firstDate? Commented Feb 24, 2013 at 22:07
  • I would add System.out.println(firstDate); and System.out.println(firstDayOfMonth); to the constructor before the first of the existing method calls. Commented Feb 24, 2013 at 22:08
  • 1
    You are declaring firstDate, but not initializing it, so it is null, causing the exception. Commented Feb 24, 2013 at 22:12

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

So inside the Calendar constructor, the first line should be private ExtDate firstDate = new ExtDate(); ? Or should initializing firstDate take place somewhere else besides the Calendar constructor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.