Results 1 to 4 of 4
Thread: Super class and sub class help
- 02-18-2012, 01:05 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Super class and sub class help
Hey Java Forum,
I first apologize for this long long post, but i really need help.
I need some help with my homework coding, I'm stuck on this one part but before that here is what the problem is:
Check list:XML Code:Develop a set of classes for a college to use in various student service and personnel applications. Classes you need to design include the following: u Person—A Person contains a first name, last name, street address, zip code, and phone number. The class also includes a method that sets each data field, using a series of dialog boxes and a display method that displays all of a Person’s information on a single line at the command line on the screen. u CollegeEmployee—CollegeEmployee descends from Person. A CollegeEmployee also includes a Social Security number, an annual salary, and a department name, as well as methods that override the Person methods to accept and display all CollegeEmployee data. u Faculty—Faculty descends from CollegeEmployee. This class also includes a Boolean field that indicates whether the Faculty member is tenured, as well as methods that override the CollegeEmployee methods to accept and display this additional piece of information. u Student—Student descends from Person. In addition to the fields available in Person, a Student contains a major field of study and a grade point average as well as methods that override the Person methods to accept and display these additional facts. Write an application named CollegeList that declares an array of four “regular” CollegeEmployees, three Faculty, and seven Students. Prompt the user to specify which type of person’s data will be entered (‘C’, ‘F’, or ‘S’), or allow the user to quit (‘Q’). While the user chooses to continue (that is, does not quit), accept data entry for the appropriate type of Person. If the user attempts to enter data for more than four CollegeEmployees, three Faculty, or seven Students, display an error message. When the user quits, display a report on the screen listing each group of Persons under the appropriate heading “College Employees,” “Faculty,” or “Students.” If the user has not entered data for one or more types of Persons during a session, display an appropriate message under the appropriate heading. Save the files as Person.java, CollegeEmployee.java, Faculty.java, Student.java, and CollegeList.java.
I already done Person.java, CollegeEmployee.java, Faculty.java, Student.java.
Problem:
Stuck on CollegeList.java
Below is my code for Person.java the parent(base) class
CollegeEmployee.java a child class extended from PersonJava Code:import javax.swing.JOptionPane; public class Person { protected String firstName; protected String lastName; protected String address; protected String zipCode; protected String phoneNumber; /* public Person(String firstName, String lastName, String address, String zipCode, String phoneNumber) { this.firstName = firstName; this.lastName = lastName; this.address = address; this.zipCode = zipCode; this.phoneNumber = phoneNumber; } */ public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setAddress(String address) { this.address = address; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getAddress() { return address; } public String getZipCode() { return zipCode; } public String getPhoneNumber() { return phoneNumber; } public void display() { JOptionPane.showMessageDialog(null, firstName + lastName + " lives in " + address + ", " + zipCode + "Tel: " + phoneNumber, "Information", JOptionPane.QUESTION_MESSAGE); } }
Faculty.java a child class extended from CollegeEmployee.Java Code:import javax.swing.JOptionPane; public class CollegeEmployee extends Person { protected String ssn; protected double annSalary; protected String department; public CollegeEmployee() { super(); } public void setSSN(String ssn) { this.ssn = ssn; } public void setAnnSalary(double annSalary) { this.annSalary = annSalary; } public void setDepartment(String department) { this.department = department; } public String getSSN() { return ssn; } public double getAnnSalary() { return annSalary; } public String getDepartment() { return department; } public void display() { super.display(); JOptionPane.showMessageDialog(null, "\nSSN: " + getSSN() + "\nAnnual Salary: " + getAnnSalary() + "\nDepartment: " + getDepartment(), "Information", JOptionPane.QUESTION_MESSAGE); } }
Student.java extended from Person.javaJava Code:import javax.swing.JOptionPane; public class Faculty extends CollegeEmployee { protected boolean tenured; public Faculty() { super(); if(tenured) tenured = true; else tenured = false; } public void display() { super.display(); JOptionPane.showMessageDialog(null, "Tenured: " + tenured, "Information", JOptionPane.QUESTION_MESSAGE); } }
And here is my main class CollegeList.java, I am stuck at this one part where I am trying to store what the user enters into my arrays:Java Code:import javax.swing.JOptionPane; public class Student extends Person { protected String major; protected double gradePointAverage; public Student() { super(); } public void setMajor(String major) { this.major = major; } public void setGPA(double gradePointAverage) { this.gradePointAverage = gradePointAverage; } public String getMajor() { return major; } public double getGPA() { return gradePointAverage; } public void display() { super.display(); JOptionPane.showMessageDialog(null, "\nMajor: " + getMajor() + "\nGPA: " + getGPA(), "Information", JOptionPane.QUESTION_MESSAGE); } }
When I compile and run, it compiled right but when I enter name it gives me this error:Java Code:import java.util.Scanner; public class CollegeList { public static void main(String[] args) { CollegeEmployee[] group1 = new CollegeEmployee[4]; Faculty[] group2 = new Faculty[3]; Student[] group3 = new Student[7]; Scanner scan = new Scanner(System.in); String response; String fname; String lname; String address; String zip; String phone; String cont ="Y"; String QUIT = "Q"; System.out.println("(C)ollege employee, (F)aculty or (S)tudent, (" + QUIT + ") to quit:"); response = scan.nextLine(); switch (response) { case "C": while(cont.equals("Y")) { int i = 0; System.out.println("Enter first name: "); fname = scan.nextLine(); group1[i].setFirstName(fname); System.out.println("Enter last name: "); lname = scan.nextLine(); group1[i].setLastName(lname); System.out.println("Enter address: "); address = scan.nextLine(); group1[i].setAddress(address); System.out.println("Enter zip code: "); zip = scan.nextLine(); group1[i].setZipCode(zip); System.out.println("Enter phone number: "); phone = scan.nextLine(); group1[i].setPhoneNumber(phone); System.out.println("Enter more? (Y/N)"); cont = scan.nextLine(); i++; } } } }
Java Code:----jGRASP exec: java CollegeList (C)ollege employee, (F)aculty or (S)tudent, (Q) to quit: C Enter first name: John Exception in thread "main" java.lang.NullPointerException at CollegeList.main(CollegeList.java:33) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
- 02-18-2012, 01:36 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Super class and sub class help
I'm going to assume line 33 of CollegeList.java is "group1[i].setFirstName(fname);". (The 33 in the stacktrace refers to the line number in the source code. It's a good idea to say which line that is in your post because it won't always match up with the line numbers provided by the site's CSS.)Exception in thread "main" java.lang.NullPointerException
at CollegeList.main(CollegeList.java:33)
You get a NullPointerException when you use a variable or other expression as if it had a non null value when it is really null. Typical such usages are using the "dot" operator to dereference the variable to call a method, or access a variable and using the [] operator to access an array element. Clearly you can't call a method of something that is null, or access some position in an array which is null.
So what is null in line 33? Possible candidates (things you use "dot" or [] with) are: group1 and group1[i]. (*) You can tell which by using System.out.println() to do a little debugging. In such cases it is probably a good idea to print i as well. The more information, the better!Java Code:String[] foo; System.out.println(foo.length); // bad! foo is null System.out.println(foo[0]); // bad! foo is null foo[0] = "BAH!"; // bad! foo is null foo = new String[42]; System.out.println(foo[0].toLowerCase()); // bad! foo[0] is null foo[0] = "BAH!"; System.out.println(foo[0].toLowerCase()); // finally good: foo!=null and foo[0]!=null
Once you have determined which is null when it shouldn't be, go back through your code and find where you thought you had given it a non null value. Either you fogot to give it a value, or the code you thought gave it a value didn't and you have to figure out why.Java Code:System.out.println("About to set the first name"); System.out.println("i=" + i); System.out.println("group1=" + group1); System.out.println("group1[i]=" + group1[i]); group1[i].setFirstName(fname);
(*) Edit: It may not be obvious, but we don't care whether fname is null or not. It may be null, and that might cause a problem. But, if so, the stack trace will report the problem as occurring in the setFirstName() method. By giving us the focus of the problem the stack trace is limiting what we have to check.Last edited by pbrockway2; 02-18-2012 at 01:43 AM.
- 02-18-2012, 02:17 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Super class and sub class help
The line number matches.
So I looked into see where the null occurs, added that 4 line you wrote, compiled and run:
So the null happens when group1[i], when i print out fname it has the data "John", so it should be able to add John to group[i] but why won't it? I thought my code is correct, since in my other example I did in class also uses the same code, it hurts my head cause I get confuse...Java Code:----jGRASP exec: java CollegeList (C)ollege employee, (F)aculty or (S)tudent, (Q) to quit: C Enter first name: John About to set the first name i=0 group1=[LCollegeEmployee;@2f3cf887 group1[i]=null Exception in thread "main" java.lang.NullPointerException at CollegeList.main(CollegeList.java:37) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
- 02-18-2012, 02:29 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: Super class and sub class help
The output is telling you not so much that the null "happens", but that group[i] has the value null. And that's why you cannot call the setFirstName() method. You cannot call the method because group1[i] does not refer to anything with a method to call.So the null happens when group1[i], when i print out fname it has the data "John", so it should be able to add John to group[i] but why won't it?
You must give group1[i] a value before you can call any methods like group1[i].setFirstName().
This will involve using the "new" operator.
Similar Threads
-
Sub class and super class
By dragstang86 in forum New To JavaReplies: 5Last Post: 09-28-2011, 09:50 AM -
Gathering Data from a super class from a sub class
By kammce in forum New To JavaReplies: 2Last Post: 08-17-2011, 08:09 AM -
Referring to a super class
By blug in forum New To JavaReplies: 7Last Post: 03-20-2011, 05:06 AM -
super class reference variable accesses overriding sub class method
By subith86 in forum New To JavaReplies: 5Last Post: 01-26-2011, 06:38 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks