aspose file tools*
The moose likes Beginning Java and the fly likes Need to make a lock. Having trouble getting it to open. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need to make a lock. Having trouble getting it to open. " Watch "Need to make a lock. Having trouble getting it to open. " New topic
Author

Need to make a lock. Having trouble getting it to open.

Brandon Choate
Greenhorn

Joined: Mar 06, 2009
Posts: 14
Hello, a little new here.

Ok, so I'm trying to make a combination lock using Java. The combo has to be a three letter combination.

First for the main bit.




Now for the tester.



The problem is that it keeps saying that "The Lock Is Not Opened" no matter what I do. What am I doing wrong?

Thanks in advance.


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

In the constructor, you set the combination to the empty string. You never set anything anywhere else. Your open tests checks if the combination is equal to "BOB", which it isn't because you set it to "" and never change it. So yeah, you will always get false.

You have to:
1) In the lock's constructor set the combination to the string passed into the constructor (so the String you asked for means something).
2) When you ask for a position, you call setPosition, but that method doesn't set any positions. So you will have to modify it so it does.
3) When you make the comparison you are hard-coding the test to see if the combination is "BOB". You should change that comparison to test if the sum of the guessed positions equals the combination.


Steve
Brandon Choate
Greenhorn

Joined: Mar 06, 2009
Posts: 14
That seemed to help quite a bit, thanks.

... however, now it just says this after I run the program and input everything:

----jGRASP exec: java CombinationLockTest

Exception in thread "main" java.lang.NullPointerException
at CombinationLock.unlock(CombinationLock.java:36)
at CombinationLockTest.main(CombinationLockTest.java:26)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.


Something is wrong with "public boolean unlock()," but I can't quite place it.....

After this, I think I'll be done!

for the record, unlock is now:


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The error will be on this line:
"aPosition.equals(combination)"

You de-reference (call a method on) aPosition, but you haven't assigned a value to the variable named aPosition, so it is null. As a result you get a NullPointerEcception. Resolve it by assigning the appropriate value to aPosition.
Brandon Choate
Greenhorn

Joined: Mar 06, 2009
Posts: 14
I had been setting aPosition to "position1 + position2+ position3"


Then I tried "BOB" but I keep getting the same result.


EDIT: Oh, now I have the exact opposite problem. Now it says that everything opens the lock. Well, suppose I'm making a little bit of progress.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to make a lock. Having trouble getting it to open.