This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Bumper car program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Bumper car program" Watch "Bumper car program" New topic
Author

Bumper car program

Jeremy Anderson
Greenhorn

Joined: Nov 02, 2004
Posts: 10
I'm sorry in advance if this is a stupid question, but I cannot seem to get this program of mine to work. The goal of the program is to get acquainted with creating our own classes, and I can't seem to get some of it to work. I'm not sure if I should post all the code here- it would probably take up too much room, so I will add it and externally link.

Here are the main classes of my program:

Classes here

I will have a "Game driver" class with a main method to run the playGame method in the game class to make it run. The assignment is to have two bumper cars on a grid from -10 10 on both the x and y axis- we are supposed to generate a random length and direction for movement and make the cars "bouce" off the wall if they are to go outside the boundary.

I am getting an error on the Grid class at:
System.out.print("What integer would you like to. . . : ");
car1ID = scan.nextInt();

saying it "cannot find symbol" with the car1ID highlighted.

Other than that, I can't find anything wrong. Only problem is, it would successfully compile before I made a few changes to the Game class- I was getting the wonderful nullPointerException error and tried to fix it- and I ended up making it worse.

The program is pretty much done and seems like it should work (in theory) but I can't even get to the point where I can test it. If you guys have any ideas as to what is wrong, I would really love to hear them. I'm completely lost right now. Thanks in advance guys!
[ November 02, 2004: Message edited by: Mr Anderson ]
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,

You variable 'scan' appears to be local to your constructor. When you try to use the reference variable to access your nextInt() it is a problem.

Try to declare your scan outside your constructor, and then initialize it inside your constructor.


Jimmy Die
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,

Also I noticed, in Grid class that carID1 is not declaired, but you assign a value to it in your method displayGrid().

Also in both classes you declaired your variables car1 and car2 both inside your constructors and outside your constructors. If this was not intended then you could rewrite it like
public class Grid
{
private Car car1;
private Car car2;
int miles;
private int car2ID;
private int car1XCoord;
private int car1YCoord;
private int car2XCoord;
private int car2YCoord;

public Grid()
{
Scanner scan = new Scanner(System.in);
car1XCoord = 0;
car1YCoord = 0;
car2XCoord = 0;
car2YCoord = 0;
// Car car1 = new Car(); commented out...
// Car car2 = new Car(); commented out...
car1 = new Car();
car2 = new Car();

}
Jeremy Anderson
Greenhorn

Joined: Nov 02, 2004
Posts: 10
I appreciate the quick help, It helped me a great deal. The only problem I am having is that I still get the nullpointerexception error when I try to run my game driver class- (basically main method with

inside to begin the game.

I have no idea where this is coming from, can't figure out how things aren't working. I get the error in the game class, at the point where it says



If you happen to catch anything else, I would appreciate it. May just have to sit my teacher down and say that I am completely lost and that I REALLY NEED HELP! She has a policy to let students learn on their own, but I am just too stuck on this one.
[ November 02, 2004: Message edited by: Mr Anderson ]
Jimmy Die
Ranch Hand

Joined: Nov 20, 2003
Posts: 97
Hi,


Remember that the null pointer exception is thrown when you attempt to use a reference variable which has 'null' assigned to it.

We then have to ask ourselves what scenarios would our reference variables have 'null' assigned to them. One scenario would be if we simply assigned null to it like this,

car1 = null;

Another scenario is if we declair Car car1; but do not initialize it anywhere like in the constructor. Perhaps it would look like this car1 = new Car();

If we look at the Game class you will notice that there is no initialization of the car1 and car2. The compiler says 'ok I'll just put these reference variables to null. If the people unhooked from the matrix want to use them then they'll initialize them somewhere'. So he lets you continue without compilor error. (Reference variables are initialized ok in Grid class, of course a reference variable in one class that has the same name as one in another class are two completely different variables and must be initialized in both classes).

Because the code is attempting to utilize the non-static method car1.randomLength() and the others, there is problem because car1 was never initialized in the Game class.

Have another go at it...
Jeremy Anderson
Greenhorn

Joined: Nov 02, 2004
Posts: 10
OK, got it to play and have it running to the point of where I can pick the integers to play. I must have a logic error in the displayGrid method, it's only showing up one line at a time. I'm running through it right now to find the error, having no luck. Thanks for the help so far, I really appreciate it!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
May just have to sit my teacher down and say that I am completely lost and that I REALLY NEED HELP! She has a policy to let students learn on their own.


Don't get depressed. Show your teacher that you have tried. Be specific about what things you don't understand and ask for help. That's what teachers are for (and are paid for). Discuss with your fellow students, if most are confused then maybe it's the teacher that needs the help


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jeremy Anderson
Greenhorn

Joined: Nov 02, 2004
Posts: 10
It's probably a little bit of both of us that needs help. I understand where she is coming from, but I'm too stuck to get out of this one. I almost have it, and I appreciate all your help.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Mr Anderson",

We're a friendly bunch and pretty light in rules here at the JavaRanch, but one that the take quite seriously is the display name rule. You can check it out here.

Basically we require your display name to be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and update your display name, since accounts with invalid display names get deleted, often without warning.

thanks,
Dave.
Jeremy Anderson
Greenhorn

Joined: Nov 02, 2004
Posts: 10
Sure thing, didn't see that rule. Hope all is well now!
Jeremy Anderson
Greenhorn

Joined: Nov 02, 2004
Posts: 10
I got another question, updated the classes on my storage space and would like to see if anyone can help me with this.

My problem is that I cannot get the grid to update the cars on the grid based on their coordinates. To me, it seems that everything is in order and it should be updated, but something must be wrong I'm not seeing.

If you guys wouldn't mind taking another look, I would really appreciate it. I'm going to talk to my teacher anyway no matter what tomorrow, but this may help me eliminate one problem and maybe give me a chance to ask him about another that comes up (and with this project, they will.) Thanks again guys!
 
Consider Paul's rocket mass heater.
 
subject: Bumper car program