Results 21 to 33 of 33
Thread: Boolean Method Problem
- 01-22-2012, 09:25 PM #21
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
-
Re: Boolean Method Problem
- 01-22-2012, 09:37 PM #23
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
- 01-22-2012, 09:44 PM #24
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
-
Re: Boolean Method Problem
- 01-22-2012, 10:04 PM #26
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Boolean Method Problem
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?
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)
-
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).
-
Re: Boolean Method Problem
Oh, I see what you're doing, ...
you're re-declaring the spaces variable in the constructor:
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]; // **** here *** }
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 **** }
- 01-22-2012, 10:24 PM #29
Member
- Join Date
- Jan 2012
- Posts
- 9
- Rep Power
- 0
Re: Boolean Method Problem
- 01-22-2012, 10:57 PM #30
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: Boolean Method Problem
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.
- 01-22-2012, 11:12 PM #31
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
-
- 01-23-2012, 01:21 AM #33
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Similar Threads
-
problem on expressing a boolean method
By b.m in forum New To JavaReplies: 9Last Post: 12-12-2010, 06:17 PM -
Boolean and Method help on Homework
By gto400no1 in forum New To JavaReplies: 3Last Post: 02-22-2010, 12:12 AM -
Boolean method help
By syferite in forum New To JavaReplies: 6Last Post: 10-28-2009, 01:32 PM -
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02-22-2009, 02:36 AM -
[SOLVED] boolean method problem
By shadowblade19 in forum New To JavaReplies: 6Last Post: 11-30-2008, 02:01 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks