0

I'm quite stumped on my situation here. I'm really not sure why I'm getting this null pointer exception.

//here is a method from one of my classes

public class UI {

    Registry reg;

    public UI (Registry reg) {
        this.reg = reg;
        menu();
    }

    private void addAppts(Scanner sc) {
        System.out.println("Enter the month for appointment in mm/dd/yyyy format:");
        sc.nextLine();
        int month = sc.nextInt();
        System.out.println("Enter the day for appointment mm/dd/yyyy format:");
        sc.nextLine();
        int day = sc.nextInt();
        System.out.println("Enter the year for appointment mm/dd/yyyy format:");
        sc.nextLine();
        int year = sc.nextInt();
        System.out.println("Enter the hour for appointment:");
        sc.nextLine();
        int hour = sc.nextInt();
        System.out.println("Enter the minute for appointment:");
        sc.nextLine();
        int minute = sc.nextInt();
        System.out.println("Enter the owner name for appointment:");
        sc.nextLine();
        String ownerName = sc.nextLine();
        reg.addAppts(month, day, year, hour, minute, ownerName);
    }
}

//and here is another method from my other class

public class Registry {

    private Appointments[] appts;

    public void addAppts(int month, int day, int year, int hour, int minute, String ownerName) {
        if (next < appts.length) {
            Appointments e = new Appointments(month, day, year, hour, minute, ownerName);
            appts[next++] = e;
        }
    }
}

public class Appointments {

    private int month, day, year, hour, minute;
    private String ownerName;

    public Appointments(int month, int day, int year, int hour, int minute, String ownerName) {
        this.month = month;
        this.day = day;
        this.year = year;
        this.hour = hour;
        this.minute = minute;
        this.ownerName = ownerName;
    }
}

I'm getting the nullpointer exception error on the last line of the first method. Any suggestions?

4
  • 5
    where is reg defined? is it initialized? Commented Mar 14, 2014 at 9:23
  • how does UI get instansiated? Commented Mar 14, 2014 at 9:28
  • show us stack trace, and where you initiate appts, i think that might be an issue if this is complete code Commented Mar 14, 2014 at 9:31
  • Are you talking about the Exception in thread? My apologies, I'm still new to some programming words. Commented Mar 14, 2014 at 9:34

2 Answers 2

2

reg is more than likely null.

If nullness is conceptually permissible then consider using if (reg != null){ around the method. If it should not be null by the time this method executes, then ensure it's initialised when the object is constructed.

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

2 Comments

well in this specific case that line would prevent the error but break the program, it should be initialized by the time it gets used
That, of course, is down to the programmer: perhaps it can be null. It might be some listener which need not be attached. We simply don't know. But saying that, you're probably right ;-)
0

my guess will be (assuming this is completed code), you getting NPE when you are trying add new entry to appts array inside your Registry class.

in snippet which you provided, you never initialize your array.

1 Comment

Oh shoot! ._. I meant to originally create an arraylist not an array so i can work with a dynamic size instead of a set one. Shoot back to the drawing boards

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.