# Thread: Boolean Method Problem

1. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Boolean Method Problem

Originally Posted by Fubarable
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. ## Re: Boolean Method Problem

Originally Posted by diamonddragon
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. Member
Join Date
Jan 2012
Posts
9
Rep Power
0

## Re: Boolean Method Problem

Originally Posted by diamonddragon
This:

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

regarding convention for get and set methods.
...
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.

Originally Posted by Fubarable
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. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Boolean Method Problem

Originally Posted by Bonfy
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. ## Re: Boolean Method Problem

Originally Posted by Bonfy
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. Member
Join Date
Jan 2012
Posts
9
Rep Power
0

## Re: Boolean Method Problem

Originally Posted by diamonddragon
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?

Originally Posted by Fubarable
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;
}

{
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. ## 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. ## 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. Member
Join Date
Jan 2012
Posts
9
Rep Power
0

## Re: Boolean Method Problem

Originally Posted by Fubarable
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. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Boolean Method Problem

Originally Posted by Bonfy
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. Member
Join Date
Jan 2012
Posts
9
Rep Power
0

## 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. ## Re: Boolean Method Problem

Originally Posted by Bonfy
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. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
6

## Re: Boolean Method Problem

Originally Posted by Bonfy
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 First 12

#### Posting Permissions

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