Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brandon Choate
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brandon Choate
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic