Page 2 of 2 FirstFirst 12
Results 21 to 33 of 33
Like Tree1Likes

Thread: Boolean Method Problem

  1. #21
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    Have you followed my recommendation to set your class's totalSpaces field in the constructor? If not, you understand that totalSpaces currently == 0 right? So the for loop won't even be called.
    Regarding to my last question, I thought He already fixed constructor as You wrote:
    this.totalSpaces = totalSpaces;

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by diamonddragon View Post
    Regarding to my last question, I thought He already fixed constructor as You wrote:
    this.totalSpaces = totalSpaces;
    I assume nothing. Where does he show that he's fixed it yet?

  3. #23
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by diamonddragon View Post
    This:
    public void addHours(int addedHours)

    is better to write like this:
    public void setPaidHours(int addedHours)

    regarding convention for get and set methods.
    ...
    When asking:
    if (spaces[i] == null)

    what happens if there is no object of class Car created, but only a reference variable?
    It happens exactly what I'm saying i.e. it returns false.

    Quote Originally Posted by Fubarable View Post
    Have you followed my recommendation to set your class's totalSpaces field in the constructor? If not, you understand that totalSpaces currently == 0 right? So the for loop won't even be called.
    Doing so (this.totalSpaces = totalSpaces;) in the constructor gives me a NullPointerException when running the program.

  4. #24
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    It happens exactly what I'm saying i.e. it returns false.

    Doing so (this.totalSpaces = totalSpaces;) in the constructor gives me a NullPointerException when running the program.
    You have declared reference variable spaces of Car class object, so put Your eye on Parking object constructor, and say, what this:
    Car[] spaces = new Car[totalSpaces];

    should do exactly?

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    Doing so (this.totalSpaces = totalSpaces;) in the constructor gives me a NullPointerException when running the program.
    We need more information. Show the new code and indicate with a comment which line throws the NPE.

  6. #26
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by diamonddragon View Post
    You have declared reference variable spaces of Car class object, so put Your eye on Parking object constructor, and say, what this:
    Car[] spaces = new Car[totalSpaces];

    should do exactly?
    It should create an Array of Car objects with a number equal to the variable "totalSpaces" of objects in it. Such objects should all be initialized to null. Am I right?

    Quote Originally Posted by Fubarable View Post
    We need more information. Show the new code and indicate with a comment which line throws the NPE.
    Alright, here's all the code I have so far. It's really everything, so what you see = what I have:

    Java Code:
    public class Car
    {
        private String numberplate;
        private int paidHours;
     
        public Car(String aNumberplate, int somePaidHours)
        {
            numberplate = aNumberplate;
            paidHours = somePaidHours;
        }
     
        public String getNumberplate()
        {
            return numberplate;
        }
     
        public int getPaidHours()
        {
            return paidHours;
        }
     
        public void addHours(int addedHours)
        {
            paidHours = paidHours + addedHours;
        }
    }
    Java Code:
    import java.util.Random;
    public class Parking
    {
        private Car[] spaces;
        private int totalSpaces;
        private int freeSpaces;
     
        public Parking(int totalSpaces)
        {
            this.totalSpaces = totalSpaces;
            Car[] spaces = new Car[totalSpaces];
        }
     
        public boolean green()
        {
            for (int i=0; i<totalSpaces; i++)
            {
                if (spaces[i] == null)
                return true;
            }
            return false;
        }
    }
    Java Code:
    public class ParkingTester
    {
        public static void main(String[] args)
        {
            Parking testPark = new Parking(10);
            System.out.println(testPark.green());
        }
    }
    Exception in thread "main" java.lang.NullPointerException
    at Parking.green(Parking.java:18)
    at ParkingTester.main(ParkingTester.java:6)

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    You again need to put a comment in your code that shows us which line is line 18 of Parking. Our line numbers and yours don't match, so we need your help (which is why I requested this earlier).

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Oh, I see what you're doing, ...

    you're re-declaring the spaces variable in the constructor:

    Java Code:
        public Parking(int totalSpaces)
        {
            this.totalSpaces = totalSpaces;
            Car[] spaces = new Car[totalSpaces]; // **** here ***
        }
    This creates an array of Car called spaces inside of the constructor that is only visible inside of the constructor and no-where else. Your class field, spaces is never initialized. Solution: don't do this, don't re-declare the variable. Instead use the class field that has been declared in the class:


    Java Code:
        public Parking(int totalSpaces)
        {
            this.totalSpaces = totalSpaces;
            // Car[] spaces = new Car[totalSpaces]; // **** get rid of this ****
            spaces = new Car[totalSpaces]; // **** and instead use this ****
        }

  9. #29
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Boolean Method Problem

    Quote Originally Posted by Fubarable View Post
    Oh, I see what you're doing, ...

    you're re-declaring the spaces variable in the constructor:

    Java Code:
        public Parking(int totalSpaces)
        {
            this.totalSpaces = totalSpaces;
            Car[] spaces = new Car[totalSpaces]; // **** here ***
        }
    This creates an array of Car called spaces inside of the constructor that is only visible inside of the constructor and no-where else. Your class field, spaces is never initialized. Solution: don't do this, don't re-declare the variable. Instead use the class field that has been declared in the class:


    Java Code:
        public Parking(int totalSpaces)
        {
            this.totalSpaces = totalSpaces;
            // Car[] spaces = new Car[totalSpaces]; // **** get rid of this ****
            spaces = new Car[totalSpaces]; // **** and instead use this ****
        }
    That was the problem. Thank you really much for the help Fubarable, and all those who answered this topic.
    As a new user I got a really good impression of this forum and I'll be hanging around for a while. Nice to see such good and fast support.

  10. #30
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    It should create an Array of Car objects with a number equal to the variable "totalSpaces" of objects in it. Such objects should all be initialized to null. Am I right?
    Yes, create array of objects of type Car and assign reference to that array with variable spaces, which is already declared, since spaces is instance variable, and is assigned to each object of Parking type.
    In other word, each time object of Parking type is created, there is spaces variable, which is only reference, since no objects of Car type is been created.

  11. #31
    Bonfy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: Boolean Method Problem

    Yes you're right, I did get it only after Fubarable hand-holded me explaining what to do step-by-step tho. Feel really like a newbie :P

  12. #32
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    Yes you're right, I did get it only after Fubarable hand-holded me explaining what to do step-by-step tho. Feel really like a newbie :P
    You feel like we all did when we started Java, like I feel right now wading into Python...

  13. #33
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: Boolean Method Problem

    Quote Originally Posted by Bonfy View Post
    Yes you're right, I did get it only after Fubarable hand-holded me explaining what to do step-by-step tho. Feel really like a newbie :P
    Just one remark: Car[] spaces = new Car[totalSpaces]; actually do not create array of objects of type Car, but array of reference variables.
    In other word, it actually create reference to array of references.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. problem on expressing a boolean method
    By b.m in forum New To Java
    Replies: 9
    Last Post: 12-12-2010, 07:17 PM
  2. Boolean and Method help on Homework
    By gto400no1 in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:12 AM
  3. Boolean method help
    By syferite in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 02:32 PM
  4. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 03:36 AM
  5. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 03:01 AM

Posting Permissions

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