Results 1 to 20 of 20
Thread: Don't know why I have an error?
- 07-08-2011, 06:49 AM #1
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Don't know why I have an error?
I am getting this error...
FOR THIS PROGRAM....Java Code:F:\Lab 08\Employee.java:19: unreported exception MissingNameException; must be caught or declared to be thrown setFirstName( first ); ^ F:\Lab 08\Employee.java:20: unreported exception MissingNameException; must be caught or declared to be thrown setLastName( last ); ^ F:\Lab 08\Employee.java:21: unreported exception MissingSSNException; must be caught or declared to be thrown setSocialSecurityNumber( ssn ); ^ 3 errors
Java Code:public abstract class Employee { /* * You will need to add a throws clause to the class header. * List each of the exceptions that may be thrown by the constructor */ private String firstName; private String lastName; private String socialSecurityNumber; // three-argument constructor public Employee( String first, String last, String ssn ) { /* * Before executing each "set" method, test the argument. * Example: if first is a null string, throw the exception indicatign a missing name, * else, invoke setFirstName method */ setFirstName( first ); setLastName( last ); setSocialSecurityNumber( ssn ); } // end three-argument Employee constructor // set first name public void setFirstName( String first ) throws MissingNameException { if (first == null) { try { throw new MissingNameException(); } catch(MissingNameException missingNameException) { System.out.println(missingNameException); } } else { firstName = first; // should validate } // end method setFirstName } // return first name public String getFirstName() { return firstName; } // end method getFirstName // set last name public void setLastName( String last ) throws MissingNameException { if (last == null) { try { throw new MissingNameException(); } catch(MissingNameException missingNameException) { System.out.println(missingNameException); } } else { lastName = last; // should validate } // end method setLastName } // return last name public String getLastName() { return lastName; } // end method getLastName // set social security number public void setSocialSecurityNumber( String ssn ) throws MissingSSNException { if (ssn == null) { try { throw new MissingSSNException(); } catch (MissingSSNException missingSSNException) { System.out.println(missingSSNException); } } else { socialSecurityNumber = ssn; // should validate } // end method setSocialSecurityNumber } // return social security number public String getSocialSecurityNumber() { return socialSecurityNumber; } // end method getSocialSecurityNumber // return String representation of Employee object @Override public String toString() { return String.format( "%s %s\nsocial security number: %s", getFirstName(), getLastName(), getSocialSecurityNumber() ); } // end method toString }// end abstract class Employee
- 07-08-2011, 06:57 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You are trying to use methods that throw exceptions in your constructor. There are a few things I will suggest, which I suggested previously. First, you should not be throwing and catching the exception in the same method, there is no point, you may as well just save the time and print an error message. You throw exceptions if the code CAN throw an exception and let the client code handle the exception correctly. SetName should look like this
Then you would wrap calls to this method in a try catch clause.Java Code:public void setFirstName(String first){ if(first == null) throw new MissingNameException(); } else firstName = name; }
Next, you should not ever use method that can be overridden in a constructor. Instead of using setters in the constructor, access the variables directly. It's safe to do this. If you use a so called "Alien" method(one which can be overridden), you can get unexpected results or broken code from creating a subclass with overridden methods.
- 07-08-2011, 06:58 AM #3
The three methods you are trying to call are declared to throw an exception. This means you have to handle them just as the error message says. Have you covered exception handling (eg try/catch statements) yet?
- 07-08-2011, 07:08 AM #4
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
I am still getting the same error message with the changes you suggested, is this correct?
Java Code:public abstract class Employee { /* * You will need to add a throws clause to the class header. * List each of the exceptions that may be thrown by the constructor */ private String firstName; private String lastName; private String socialSecurityNumber; // three-argument constructor public Employee( String first, String last, String ssn ) { /* * Before executing each "set" method, test the argument. * Example: if first is a null string, throw the exception indicatign a missing name, * else, invoke setFirstName method */ setFirstName( first ); setLastName( last ); setSocialSecurityNumber( ssn ); } // end three-argument Employee constructor // set first name public void setFirstName( String first ) throws MissingNameException { if (first == null) { throw new MissingNameException(); } else { firstName = first; // should validate } // end method setFirstName } // return first name public String getFirstName() { return firstName; } // end method getFirstName // set last name public void setLastName( String last ) throws MissingNameException { if (last == null) { throw new MissingNameException(); } else { lastName = last; // should validate } // end method setLastName } // return last name public String getLastName() { return lastName; } // end method getLastName // set social security number public void setSocialSecurityNumber( String ssn ) throws MissingSSNException { if (ssn == null) { throw new MissingSSNException(); } else { socialSecurityNumber = ssn; // should validate } // end method setSocialSecurityNumber } // return social security number public String getSocialSecurityNumber() { return socialSecurityNumber; } // end method getSocialSecurityNumber // return String representation of Employee object @Override public String toString() { return String.format( "%s %s\nsocial security number: %s", getFirstName(), getLastName(), getSocialSecurityNumber() ); } // end method toString }// end abstract class Employee
- 07-08-2011, 07:15 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The problem now is that you are calling unsafe methods from the constructor. These methods can throw an exception so whenever you use them you want to try calling them and handle any exceptions that can occur.
You can easily fix the constructor to manipulate the instance variables directly
Instead of calling methods, this is safer because it won't be (possibly) broken by inheritance. This is actually important enough to be part of a item in the book "Effective Java", the reason is this:Java Code:public Employee(String firstName, ...){ this.firstName = firstName; ... }
If the subclass overrides the method it can cause problems because the subclass first calls the superclass constructor. If you override the method in a subclass to depend on something in the subclass to be instantiated you can cause an error when constructing the super class. Long story short, either make the methods final so they cannot be overridden, or manipulate the variables directly as shown above.
It's safe to access the variables inside the class without breaking encapsulation because you can test for and ensure that invariants are kept, and it's safe/easier to do this.
- 07-08-2011, 07:21 AM #6
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
I made the methods final but still the same error message...
Java Code:--------------------Configuration: <Default>-------------------- F:\Lab 08\Employee.java:19: unreported exception MissingNameException; must be caught or declared to be thrown setFirstName( first ); ^ F:\Lab 08\Employee.java:20: unreported exception MissingNameException; must be caught or declared to be thrown setLastName( last ); ^ F:\Lab 08\Employee.java:21: unreported exception MissingSSNException; must be caught or declared to be thrown setSocialSecurityNumber( ssn ); ^ 3 errors
- 07-08-2011, 07:26 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Like I said, you don't actually need the methods to be used in the constructor(and shouldn't), if you really are insistent on it, they have to be final and you still must declare that either 1.) the method/constructor can throw an exception, or 2.) handle any thrown exceptions that may occur.
- 07-08-2011, 07:35 AM #8
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Oh ok, so I place the try... catch clause in the constructor rather than the methods?
- 07-08-2011, 07:37 AM #9
The line that calls a method that throws an exception needs to be inside a try statement.
Java Code:try { methodThatThrowsException(); } catch(Exception e) { // handle exception here }
- 07-08-2011, 07:49 AM #10
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
I just debugged it, all I did was add a throws clause to the constructor and the program compiled.
- 07-08-2011, 07:53 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yes, but errors could still occur if you are not weary. Another reason to directly test invariants and set variables. In your employee class, the invariant is that nothing should be null, so you can check for this, and then set the variables.
Try causing an exception to be thrown immediately and see what happens to the constructed object(anything weird about it?)
Here is a snippet to demonstrate
This demonstrates what can happen and even more warns against methods in constructors.Java Code:public class X{ private int x; private int y; private int z; public X(int x, int y, int z){ try{ setX(x); setY(y); setZ(z); } catch(Exception e){ e.printStackTrace(); } } public void setX(int x) throws Exception{ throw new Exception(); } public void setY(int y){} public void setZ(int z){} public String toString(){ return x + " " + y + " " + z; } public static void main(String[] args){ X x = new X(1, 2, 3); System.out.println(x); } }
Finally, when you do call a dangerous method(one which can throw an exception, you want to try to call the method and be ready to(catch) handle the exception.
I had a lengthier post, however; my browser ate them :(
- 07-08-2011, 08:07 AM #12
I just read the comment in your constructor. It would seem you are not following the instructions./*
* Before executing each "set" method, test the argument.
* Example: if first is a null string, throw the exception indicatign a missing name,
* else, invoke setFirstName method
*/
- 07-08-2011, 08:07 AM #13
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Now I have problems in my main program, here is the program...
HERE IS THE PROBLEMS...Java Code:// test harness // test harness /* * This test harness will attemp to instantiate 7 objects belonging to sub-classes of * Employee. The first two methods instantiate complete and proper objects - one each of * HourlyEmployee and SalariedEmployee. The following methods will attempt to instantiate * mal-formed objects. Each attempt to instantiate a mal-formed object will result in a * thrown exception and an associated exception message(s). */ public class PostTest { public static void main( String[] args ) { instantiateHourly(); //instantiate a 'good' HourleEmployee instantiateSalaried(); //instantiate a 'good' SalarieEmployee missingFirstName(); //attempt to instantiate an employee with no first name missingLastName(); //attempt to instantiate an employee with no last name missingSSN(); //attampt to instantiate an employee missing SSN zeroHours(); //attempt to instantiate HourlyEmployee with 0 hours worked overHours(); //attempt to instantiate HourlyEmployee with >84.0 hours worked } // end main public static void instantiateHourly() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating a proper Hourly Employee" ); try { HourlyEmployee Lisa = new HourlyEmployee( "Lisa", "Wilson", "444-44-4444", 40.00, 2.50 ); } // catch blocks for each of the exceptions that might occur when instantiate an HourlyEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } catch( InvalidHoursWorkedException badHrs ) { System.out.println( badHrs.getMessage() ); } } public static void instantiateSalaried() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating a proper Salaried Employee" ); try { SalariedEmployee John = new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 ); } // catch blocks for each of the exceptions that might occur when instantiate a SalariedEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } } public static void missingFirstName() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating an Employee with no first name" ); try { SalariedEmployee noFirst = new SalariedEmployee( "", "Barnes", "888-88-8888", 1200.00 );; } // catch blocks for each of the exceptions that might occur when instantiate a SalariedEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } } public static void missingLastName() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating an Employee with no last name" ); try { HourlyEmployee noLast = new HourlyEmployee( "Jim", "", "222-22-2222", 40.00, 12.50 ); } // catch blocks for each of the exceptions that might occur when instantiate an HourlyEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } catch( InvalidHoursWorkedException badHrs ) { System.out.println( badHrs.getMessage() ); } } public static void missingSSN() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating an Employee with no SSN" ); try { SalariedEmployee noSSN = new SalariedEmployee( "Ron", "Donald", "", 1200.00 ); } // catch blocks for each of the exceptions that might occur when instantiate a SalariedEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } } public static void zeroHours() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating an Hourly Employee with zero hours" ); try { HourlyEmployee zeroHours = new HourlyEmployee( "Max", "Headroom", "321-88-4563", 0.00, 22.50 ); } // catch blocks for each of the exceptions that might occur when instantiate an HourlyEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } catch( InvalidHoursWorkedException badHrs ) { System.out.println( badHrs.getMessage() ); } } public static void overHours() { // Output message indicating which instantiation is beind attempted System.out.println( "\nInstantiating an Hourly Employee with > 84 hours" ); try { HourlyEmployee overHours = new HourlyEmployee( "Millie", "Smith", "666-66-6666", 100.00, 52.50 ); } // catch blocks for each of the exceptions that might occur when instantiate an HourlyEmployee catch( MissingNameException badName) { System.out.println( badName.getMessage() ); } catch( MissingSSNException badSSN ) { System.out.println( badSSN.getMessage() ); } catch( InvalidHoursWorkedException badHrs ) { System.out.println( badHrs.getMessage() ); } } } // end class PostTest
Is it because I named the objects differently in my subclass? just wondering....Java Code:F:\Lab 08\PostTest.java:59: exception MissingNameException is never thrown in body of corresponding try statement catch( MissingNameException badName) ^ F:\Lab 08\PostTest.java:63: exception MissingSSNException is never thrown in body of corresponding try statement catch( MissingSSNException badSSN ) ^ F:\Lab 08\PostTest.java:79: exception MissingNameException is never thrown in body of corresponding try statement catch( MissingNameException badName) ^ F:\Lab 08\PostTest.java:83: exception MissingSSNException is never thrown in body of corresponding try statement catch( MissingSSNException badSSN ) ^ F:\Lab 08\PostTest.java:123: exception MissingNameException is never thrown in body of corresponding try statement catch( MissingNameException badName) ^ F:\Lab 08\PostTest.java:127: exception MissingSSNException is never thrown in body of corresponding try statement catch( MissingSSNException badSSN ) ^ 6 errors
- 07-08-2011, 08:12 AM #14
Now you have the opposite. You have included a try/catch statement for an Exception that will never be thrown.
- 07-08-2011, 08:14 AM #15
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
Where at? My main program or the subclass? Would it help if I posted my other subclasses?
- 07-08-2011, 08:20 AM #16
- 07-08-2011, 08:20 AM #17
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
It depends what your subclasses look like, the sub classes constructor will call the super class constructor, so your subclass constructor becomes an exception throwing constrctor(guessing, haven't seen the subclasses).
Last edited by sunde887; 07-08-2011 at 08:22 AM.
- 07-08-2011, 10:05 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 07-11-2011, 07:51 PM #19
Member
- Join Date
- May 2011
- Posts
- 39
- Rep Power
- 0
So should I put the try...catch clause in the constructor?
- 07-12-2011, 04:55 AM #20
Similar Threads
-
java out of memory error-heap space error
By elsanthosh in forum NetBeansReplies: 4Last Post: 06-15-2010, 09:31 AM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks