Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Request Information

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Request Information

    Hi Mates,

    I am extremely new to java. I just started learning the basics. I am stuck with the following piece of code. Could someone please tell me where I went wrong?

    Code goes here!!!!!

    package trialdate;

    public class TrialDate {

    private int month;
    private int year;
    TrialDate hiredate;
    public TrialDate(){

    System.out.println("Here goes nothing!!!!!");

    }
    public TrialDate(int month, int year){

    hiredate.month = month;
    hiredate.year = year;
    System.out.println("The month and year is " + hiredate.month+" "+ hiredate.year);
    }
    public void myTrial(){

    hiredate = new TrialDate(12, 2008);

    }
    public static void main(String[] args) {
    TrialDate newDate = new TrialDate();
    newDate.myTrial();
    }
    }

    I just want to learn the concept pass by reference
    The out put of the program is

    Here goes nothing!!!!!
    Exception in thread "main" java.lang.NullPointerException
    at trialdate.TrialDate.<init>(TrialDate.java:19)
    at trialdate.TrialDate.myTrial(TrialDate.java:25)
    at trialdate.TrialDate.main(TrialDate.java:30)
    Java Result: 1


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Request Information

    Your error is in the constructor with two integers. In the beginning you made the variables month and year private and in your constructor you try and access them as if they were public. To fix this, either make your variables public or make two methods that return month and year so that you can access them even though they are private. The reason you got a NullPointerException is that when you call hiredate.month it is looking for a public integer named month in the TrialDate class and can't find one. I hope this was helpful.

Similar Threads

  1. request
    By aegr in forum What's Wrong With My Code?
    Replies: 38
    Last Post: March 24th, 2013, 10:07 AM
  2. Http Request
    By nillson in forum Loops & Control Statements
    Replies: 3
    Last Post: March 20th, 2013, 06:42 PM
  3. Replies: 0
    Last Post: February 1st, 2013, 12:25 PM
  4. Clueless on request.getContextPath()
    By ram.java in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 29th, 2011, 12:07 PM

Tags for this Thread