# Problem with external class

• October 19th, 2013, 09:27 PM
outime
Problem with external class
Hello guys,

I'm working on project for school, on the one the professor required us to use the class Counter as part of the exercise. The exercise itself is to implement a combination lock class that has a dial with 26 positions labeled A..Z. The dial needs to be set 3 times. If it is set to the correct combination, the lock can be opened. When the lock is closed again, the combination can be entered again.

So far, this is what I have, and the problem resides on the counter class.

CombinationLock.java

Code Java:

```/** * A class to implement a combination lock with 26 dial positions * and a three-letter combination * */ public class CombinationLock { private String S1, S2, S3; private String myPositions; // A string that represents the positions set on the dial. private Counter counter; private String Pos1; private String Pos2; private String Pos3; private boolean open; public int value;   // instance variable declarations go here   /** * Creates a lock with a given combination consisting of three upper-case characters. * @param first the first letter of the combination * @param second the second letter of the combination * @param third the third letter of the combination */ public CombinationLock(String first, String second, String third) { S1 = first; S2 = second; S3 = third; myPositions = ""; value = 1; Counter myCounter = new Counter();       // TO DO: write method body }   public String getfirst() { return S1; } public String getsecond() { return S2; } public String getthird() { return S3; }   /** * Set the dial to a position * @param aPosition a String consisting of a single uppercase letter (A..Z) */ public void setPosition(String aPosition) {   counter.click();     if (counter.getValue() == 1) { Pos1 = aPosition ;   } else if (counter.getValue() == 2) { Pos2 = aPosition ; } else if (counter.getValue() == 3) { Pos3 = aPosition; } }   /** * Try opening the lock */ public void tryToOpen() { // TO DO: write method body   if(Pos1.equals(S1) && Pos2.equals(S2) && Pos3.equals(S3)) { open = true; System.out.println("Combo correct! Lock is open."); } else { open = false; System.out.println("Wrong combo. Try again."); } }   /** * Check whether the lock is open * @return true or false indicating whether the lock is open */ public boolean isOpen() { return open; }   /** * Close the lock and print a message indicating that the lock is now closed */ public void lock() { counter.reset(); open = false; System.out.println("The MasterLock is now locked"); } }```

CombinationLockTest.java

Code Java:

```public class CombinationLockTest { public static void main (String args[]) {   CombinationLock MasterLock = new CombinationLock("J","A","P"); System.out.println("The combination to MasterLock is " + MasterLock.getfirst() + MasterLock.getsecond() + MasterLock.getthird());   String input = JOptionPane.showInputDialog("Enter first character"); MasterLock.setPosition(input);   String input2 = JOptionPane.showInputDialog("Enter second character"); MasterLock.setPosition(input2);   String input3 = JOptionPane.showInputDialog("Enter third character"); MasterLock.setPosition(input3);   System.out.println("The user entered combo is " + input + input2 + input3);   MasterLock.tryToOpen();   System.out.println(MasterLock.isOpen());   MasterLock.lock(); } }```

Counter.java

Code Java:

```/** This class models a tally counter. */ public class Counter { private int value;   /** Gets the current value of this counter. @return the current value */ public int getValue() { return value; }   /** Advances the value of this counter by 1. */ public void click() { value = value + 1; }   /** Resets the value of this counter to 0. */ public void reset() { value = 0; } }```

The error that I'm getting is

run:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at Counter.<init>(Counter.java:9)
at CombinationLock.<init>(CombinationLock.java:33)
at CombinationLockTest.main(CombinationLockTest.java: 15)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Any help on this? At this point I don't know what else to do. I cannot modify anything else, and I must use the counter.java
• October 19th, 2013, 10:44 PM
GregBrannon
Re: Problem with external class
The error you're getting is typically due to running code that contains incomplete method stubs that were generated by Netbeans or perhaps other IDEs. Be sure you're compiling and running the latest version of your code, because I don't see a reason you'd be getting that error.

However, you should be getting an NPE when you run the code you've posted, because the CombinationLock class provides for 2 Counter instances, creates one local to the constructor, myCounter, which is of no use, and then tries to use the other, counter, that has not been instantiated throughout the rest of the class. Initialize either counter or myCounter correctly in the constructor and then use that instance variable throughout the rest of the class.
• October 20th, 2013, 11:40 AM
outime
Re: Problem with external class
Quote:

Originally Posted by GregBrannon
The error you're getting is typically due to running code that contains incomplete method stubs that were generated by Netbeans or perhaps other IDEs. Be sure you're compiling and running the latest version of your code, because I don't see a reason you'd be getting that error.

However, you should be getting an NPE when you run the code you've posted, because the CombinationLock class provides for 2 Counter instances, creates one local to the constructor, myCounter, which is of no use, and then tries to use the other, counter, that has not been instantiated throughout the rest of the class. Initialize either counter or myCounter correctly in the constructor and then use that instance variable throughout the rest of the class.

I think I found the problem. At least in NetBeans. I needed to declare the object Counter on the constructor

more like this:

Any opinion why this happened?

Code java:

```public class CombinationLock { private String S1, S2, S3; private String Pos1; private String Pos2; private String Pos3; private boolean open; private Counter tally = new Counter();       // instance variable declarations go here /** * Creates a lock with a given combination consisting of three upper-case characters. * @param first the first letter of the combination * @param second the second letter of the combination * @param third the third letter of the combination */ public CombinationLock(String first, String second, String third) { S1 = first; S2 = second; S3 = third;     // TO DO: write method body }   public String getfirst() { return S1; } public String getsecond() { return S2; } public String getthird() { return S3; }   /** * Set the dial to a position * @param aPosition a String consisting of a single uppercase letter (A..Z) */ public void setPosition(String aPosition) {   tally.click();   if (tally.getValue() == 1) { Pos1 = aPosition ;   } else if (tally.getValue() == 2) { Pos2 = aPosition ; } else if (tally.getValue() == 3) { Pos3 = aPosition; } }   /** * Try opening the lock */ public void tryToOpen() { // TO DO: write method body   if(Pos1.equals(S1) && Pos2.equals(S2) && Pos3.equals(S3))   { open = true; System.out.println("Combo correct! Lock is open."); }   else { open = false; System.out.println("Wrong combo. Try again."); } }   /** * Check whether the lock is open * @return true or false indicating whether the lock is open */ public boolean isOpen() { return open; }   /** * Close the lock and print a message indicating that the lock is now closed */ public void lock() { tally.reset(); open = false; System.out.println("The MasterLock is now locked"); }     }```
• October 20th, 2013, 01:15 PM
GregBrannon
Re: Problem with external class
Because I'm a fan of actually constructing as much of the class in the constructor as possible, you could also:
Code java:

```public class CombinationLock { private String S1, S2, S3; private String Pos1; private String Pos2; private String Pos3; private boolean open;   // GB: declare it here private Counter tally;       // instance variable declarations go here /** * Creates a lock with a given combination consisting of three upper-case characters. * @param first the first letter of the combination * @param second the second letter of the combination * @param third the third letter of the combination */ public CombinationLock(String first, String second, String third) { S1 = first; S2 = second; S3 = third;   // GB: initialize it here tally = new Counter();   // TO DO: write method body } // . . . plus the rest```
I'm not sure what you mean asking for opinions as to "why it happened." You're opening yourself up for criticism, unless you think it can be blamed on Netbeans. It was a mistake. Learn from it and don't do it again.