Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By Norm

Thread: new to java - NEED HELP!

  1. #1
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default new to java - NEED HELP!

    Hello i am new to java and in my java class we just started using classes. I am getting an error in one of my programs saying:
    "java.lang.NullPointerException
    at LogicOfProgramming.Assignment4.MonthlyCalendar.cal endar(MonthlyCalendar.java:30)
    at LogicOfProgramming.Assignment4.MonthlyCalendarAppl ication.main(MonthlyCalendarApplication.java:20)"

    Here are my classes:
    Attached Files Attached Files

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: new to java - NEED HELP!

    Did you google the cause of that Exception?

    If you want help, you'll have to provide an SSCCE that demonstrates the problem.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    I have googled the problem but I couldn't find exactly what was causeing it... something about me goofing up on a class.... also there are only 3 class files.. i believe thats pretty short?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: new to java - NEED HELP!

    Quote Originally Posted by Sandro View Post
    I have googled the problem but I couldn't find exactly what was causeing it... something about me goofing up on a class.... also there are only 3 class files.. i believe thats pretty short?
    I'd be pretty surprised if you couldn't figure out what causes an NPE by a google search, or a search of these forums. And three classes is actually quite huge for this type of problem- you have a line number, right? So you already know the exact line causing the problem. Isolate that line in a standalone program, and copy and paste it here. Don't forget the code tags.

    Chances are, in the course of creating your standalone program, you'll figure the problem out on your own. But if not, then many people hesitate to click on .zip attachments, and we don't have time to wade through too much code. Make it easier for people to help you, and you'll get help faster.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    NPE? I dont know what that is.
    I don't know which parts of the code i need and which parts I wouldn't even with the lines.

    I haven't done much codeing in general and i hardly know how to fix bugs. You need to remember ive been doing classes in general about 2days now and my teacher didn't even trully explain it.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: new to java - NEED HELP!

    java.lang.NullPointerException
    at LogicOfProgramming.Assignment4.MonthlyCalendar.cal endar(MonthlyCalendar.java:30)
    Look at line 30 in the MonthlyCalendar class. There is a variable on that line that has a null value.
    Determine what variable is null and then backtrack in your code and find out why the variable does not have a valid value.
    If you can not determine which variable is null, add a println just before line 30 that prints the values of all the variables used on line 30
    KevinWorkman likes this.

  7. #7
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: new to java - NEED HELP!

    Date was never initialized in MonthlyCalendar

  8. #8
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    I thought i just had to put "Date theDate;" in my variable declaration and then i could call on it? Am i wrong?

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: new to java - NEED HELP!

    If an object is not initialized it will be assigned to null.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: new to java - NEED HELP!

    There are two steps:
    define the variable and
    give it a value.

  11. #11
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    im sorry guys... but i still have no clue what i did wrong T_T im trying to understand what you guys are telling me but sadly half of it is going right over my head... (partially because i don't know the terminology of most of what im actually doing.)

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: new to java - NEED HELP!

    You are passing an initialized Date object to MonthlyCalendars constructor. You have an uninitialized Date object inside MonthlyCalander. Perhaps some kind of assignment can be done inside the the constructor?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default Re: new to java - NEED HELP!

    Do you know what an assignment statment is? It is used to give a value to a variable:
    x = 123; // assign the value 123 to the variable x

    Do you know how to define a variable:
    int x; // define an int variable named x

    These can be done in one statement:
    int x = 123; // define a variable and give it a value.

  14. #14
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    i don't even call of the MonthlyCalendars constructor though.

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: new to java - NEED HELP!

    hmmmm Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Java Code:
    MonthlyCalendar newMonthlyCalendar = new MonthlyCalendar (newDate);

  16. #16
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    Ohhh your right (feels stupid). So what would i put into this constructor? The Date class?

  17. #17
    Sandro is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: new to java - NEED HELP!

    Did this:
    public MonthlyCalendar (Date myDate)
    {
    theDate = myDate;
    }

    Worked. Love you al_Marshy_1981 :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •