Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Working with Interfaces

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Working with Interfaces

    Greetings everyone. Sorry if this is a bit long, but I'm at my wits end. The program I'm working on is suppose to take the user input for grades (Name and grades, ie A, B, C, all uppercase), store it, and output the number of Graduates, the Current Gradute's GPA, and the Average of all the Graduates, up to 10. When prompted, the program should show each of the graduate's name's and GPA's, in descending order.

    Right now, whenever I try to add a graduate, I get the following error. Am I not calling getGraduateGPA correctly?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at myGraduates.getGraduateGPA(myGraduates.java:43)
    at GraduatePanel$addGradButton.actionPerformed(Gradua tePanel.java:285)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



    Here are the interfaces.

    import java.util.*;
    /**
     * Data Manager for a Collection of data elements (student)
     * Uses an array to hold the collection
     * @author 
     *
     */
    public interface Graduates {
     
    	/**
    	 * Add a student data element to the collection
    	 * @param newStudent - the student data element to be added to the collection
    	 */
        public void addGraduate(Graduate newStudent);
     
        /**
         * Create a student data element from the data and add to the collection
         * @param first first name of the Student
         * @param last last name of the Student
         * @param g1 grade for CS110
         * @param g2 grade for CS140
         * @param g3 grade for CS226
         * @param g4 grade for CS249
         * @param g5 grade for CS213
         * @param g6 grade for CS214
         */
        public void addGraduate(String first, String last, String g1, String g2, String g3, String g4, String g5, String g6);
     
        /**
         * Use the Arrays.sort to sort the array of student data elements by GPA
         * If your array is not full, use the sort method that uses a fromIndex and toIndex
         */
        public void sortGraduates();
     
        /**
         * returns the GPA for the student at this index of the array
         * @return the GPA for the student at this index of the array
         */
        public double getGraduateGPA(int index);
     
        /**
         * Calculate the average GPA of all student data elements in the collection
         * @return average GPA of all students in collection
         */
        public double calculateAverageofGraduates();
     
        /**
         * Returns the number of graduates in the collection
         * @return the number of graduates in the collection
         */
        public int getNumberOfGraduates();
        /**
         * Creates a string representation of the collection of student data elements
         * @return a string which represents the collection of student data elements
         */
        public String toString();
     
       /**
        * Clears the array, average, and number of students.  Sets the semester name
        * @param newSemsterName name of new semester
        */
        public void newSemster(String newSemsterName);
    }

    /**
     * Class for the Student Data Elements
     * @author 
     *
     */
    public interface Graduate extends Comparable{
     
    	/**
    	 * Calculates the GPA for a Student
    	 * @return the GPA for a Student
    	 */
      public double calculateGPA();
     
      /**
       * Convert a Grade of A,B,C,D or F into a numeric value
       * @param grade A, B, C, D or F
       * @return the numeric value of the Grade of A,B,C,D or F
       */
      public double convertGradeToNumeric(String grade);
     
      /**
       * compare the GPA's for two student
       * @param otherElement
       * @return >0 if this > otherElement, <0 if this < otherElement, 0 if this = otherElement
       */
      public int compareTo(Object otherElement);
     
      /**
       * String representation of the Student Data Element
       * @return String representation of the Student Data Element
       */
      public String toString();
     
    }

    Here are the other classes:
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    /**
     * Panel for the Program
     * @author 
     *
     */
     
    public class GraduatePanel extends JPanel {
     
    	private JLabel userTitle, progTitle, firstName, lastName, CS110, CS140, CS226, CS249, CS213, CS214, numGrads, gradGPA, depAvg;
    	private JTextField firstField, lastField, field110, field140, field226, field249, field213, field214, fieldNumGrads, fieldGradGPA, fieldDepAvg;
    	private JButton addGrad, clear, listGPA, newSemester, Exit;
     
    	String title = JOptionPane.showInputDialog("What is the semester and year? ex; Spring 2012"); //Prompts user for semesterand year.
     
    	myGraduates entry = new myGraduates();
     
    	public GraduatePanel() {
     
    		//Creates JLabels
    		userTitle = new JLabel(title);
    		progTitle = new JLabel("Programming Certificate");
    		firstName = new JLabel("First Name:");
    		lastName = new JLabel("Last Name:");
    		CS110 = new JLabel("CS110");
    		CS140 = new JLabel("CS140");
    		CS226 = new JLabel("CS226");
    		CS249 = new JLabel("CS249");
    		CS213 = new JLabel("CS213");
    		CS214 = new JLabel("CS214");
    		numGrads = new JLabel("Number of Graduates:");
    		gradGPA = new JLabel("Graduate GPA:");
    		depAvg = new JLabel("Department Average:");
     
    		//Creates JTextFields.
    		firstField = new JTextField(7);
    		lastField = new JTextField(10);
    		field110 = new JTextField(7); 
    		field140 = new JTextField(7);
    		field226 = new JTextField(7);
    		field249 = new JTextField(7);
    		field213 = new JTextField(7);
    		field214 = new JTextField(7);
    		fieldNumGrads = new JTextField(7);
    		fieldGradGPA = new JTextField(7);
    		fieldDepAvg = new JTextField(7);
     
    		fieldNumGrads.setEditable(false);
    		fieldGradGPA.setEditable(false);
    		fieldDepAvg.setEditable(false);
     
    		//Creates JButtons and Action Listeners.
    		addGrad = new JButton("Add Graduate");
    		addGrad.addActionListener(new addGradButton());
    		clear = new JButton("Clear");
    		clear.addActionListener(new ClearButton());
    		listGPA = new JButton("List GPA");
    		listGPA.addActionListener(new GPAButton());
    		newSemester = new JButton("New Semester");
    		newSemester.addActionListener(new newSemesterButton());
    		Exit = new JButton("Exit");
    		Exit.addActionListener(new ExitButton());
     
    		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
     
    		c.gridx = 3;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(userTitle, c);
     
    		c.gridx = 3;
    		c.gridy = 2;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(progTitle, c);
     
    		c.gridx = 2;
    		c.gridy = 3;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(firstName, c);
     
    		c.gridx = 3;
    		c.gridy = 3;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(firstField, c);
     
    		c.gridx = 2;
    		c.gridy = 4;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(lastName, c);
     
    		c.gridx = 3;
    		c.gridy = 4;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(lastField, c);
     
    		c.gridx = 1;
    		c.gridy = 5;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(CS110, c);
     
    		c.gridx = 1;
    		c.gridy = 6;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(CS226, c);
     
    		c.gridx = 1;
    		c.gridy = 7;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(CS213, c);
     
    		c.gridx = 2;
    		c.gridy = 5;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field110, c);
     
    		c.gridx = 2;
    		c.gridy = 6;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field226, c);
     
    		c.gridx = 2;
    		c.gridy = 7;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field213, c);
     
    		c.gridx = 3;
    		c.gridy = 5;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(CS140, c);
     
    		c.gridx = 3;
    		c.gridy = 6;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(CS249, c);
     
    		c.gridx = 3;
    		c.gridy = 7;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(CS214, c);
     
    		c.gridx = 4;
    		c.gridy = 5;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field140, c);
     
    		c.gridx = 4;
    		c.gridy = 6;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field249, c);
     
    		c.gridx = 4;
    		c.gridy = 7;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field214, c);
     
    		c.gridx = 4;
    		c.gridy = 7;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(field214, c);
     
    		c.gridx = 2;
    		c.gridy = 8;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(numGrads, c);
     
    		c.gridx = 2;
    		c.gridy = 9;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(gradGPA, c);
     
    		c.gridx = 2;
    		c.gridy = 10;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(depAvg, c);		
     
    		c.gridx = 3;
    		c.gridy = 8;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(fieldNumGrads, c);
     
    		c.gridx = 3;
    		c.gridy = 9;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(fieldGradGPA, c);
     
    		c.gridx = 3;
    		c.gridy = 10;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(fieldDepAvg, c);
     
     
     
     
     
     
     
     
     
     
    		c.gridx = 2;
    		c.gridy = 12;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(addGrad, c);
     
    		c.gridx = 3;
    		c.gridy = 12;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(clear, c);
     
    		c.gridx = 4;
    		c.gridy = 12;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(listGPA, c);
     
    		c.gridx = 2;
    		c.gridy = 13;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(newSemester, c);
     
    		c.gridx = 3;
    		c.gridy = 13;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		add(Exit, c);
     
     
     
    	}
     
    	public class addGradButton implements ActionListener
    	   {
    		   public void actionPerformed (ActionEvent event) {
     
    			   double average;
    			   int numGrads;			   
     
     
     
    			   entry.addGraduate(firstField.getText(), lastField.getText(), field110.getText(), 
    					   field140.getText(), field226.getText(), field249.getText(), 
    					   field213.getText(), field214.getText());			   
     
    			   numGrads = entry.getNumberOfGraduates();
     
    			   average = entry.getGraduateGPA(numGrads);
     
    			   fieldNumGrads.setText(Double.toString(numGrads));			   
     
    			   fieldGradGPA.setText(Double.toString(average));
     
     
     
     
    		    }
    	   } 
     
    	public class ClearButton implements ActionListener
    	   {
    			//Clears all fields except Number of Grads and Department Average
    		   public void actionPerformed (ActionEvent event)
    		   {
    			   firstField.setText("");
    			   lastField.setText("");
    			   field110.setText("");
    			   field140.setText("");
    			   field226.setText("");
    			   field249.setText("");
    			   field213.setText("");
    			   field214.setText("");
    			   fieldGradGPA.setText("");			   
    		   }
    	   } 
     
    	public class GPAButton implements ActionListener
    	   {
    		   public void actionPerformed (ActionEvent event)
    		   {
    			   JOptionPane.showMessageDialog(null, entry.toString());
    		   }
    	   } 
     
    	public class newSemesterButton implements ActionListener
    	   {
    		   public void actionPerformed (ActionEvent event)
    		   {
    			   entry.newSemster(title);
    			   firstField.setText("");
    			   lastField.setText("");
    			   field110.setText("");
    			   field140.setText("");
    			   field226.setText("");
    			   field249.setText("");
    			   field213.setText("");
    			   field214.setText("");
    			   fieldGradGPA.setText("");
    			   fieldNumGrads.setText("");
    			   fieldDepAvg.setText("");
    		   }
    	   } 
     
     
    	public class ExitButton implements ActionListener
    	   {
    			//Action listener for exit button.
    		   public void actionPerformed (ActionEvent event)
    		   {
    			   System.exit(0); //Exits the program.
    		   }
    	   } 
    }

    import java.util.Arrays;
     
     
    public class myGraduates implements Graduates {
     
    	private Graduate[] x;
    	private int count;
     
    	public myGraduates() {
     
    		x = new Graduate[10];
    		count = 0;
     
    	}
     
     
    	@Override
    	public void addGraduate(Graduate newStudent) {
    		// TODO Auto-generated method stub
    		 x[count] = newStudent;
    		 count++;                                                       
    	}
     
    	@Override
    	public void addGraduate(String first, String last, String g1, String g2,
    			String g3, String g4, String g5, String g6) {		
     
    		x[count] = new myGraduate(first, last, g1, g2, g3, g4, g5, g6);
    		count++;
    	}
     
    	@Override
    	public void sortGraduates() {
     
    		Arrays.sort(x, 0, count);
    	}
     
    	@Override
    	public double getGraduateGPA(int index) {
     
    		double gpa;
     
    		gpa = x[index].calculateGPA();
     
    		return gpa;
    	}
     
    	@Override
    	public double calculateAverageofGraduates() {
     
    		double depAvg = 0;
    		int i;
     
    		for (i = 0; i < count; i++) {			
    			depAvg+= x[i].calculateGPA();			
    		}
     
    		return depAvg/count;
    	}
     
    	@Override
    	public int getNumberOfGraduates() {
    		return count;
    	}
     
        public String toString() {
     
        	String everything = "";
        	int i;
     
        	for (i = 0; i < count; i++) {
     
        		everything+= x[count].toString();
     
        	}
     
     
     
        	return everything;
        }
     
     
    	@Override
    	public void newSemster(String newSemsterName) {
     
    			x = null;
    	}
     
    }

    public class myGraduate implements Graduate {
     
    	private String first, last, g1, g2, g3, g4, g5, g6;
    	private double average;
     
    	public myGraduate() {
     
    		first = "";
    		last = "";
    		g1 = "";
    		g2 = "";
    		g3 = "";
    		g4 = "";
    		g5 = "";
    		g6 = "";		
    		average = 0;
    	}
     
    	public myGraduate(String first, String last, String g1, String g2,
    			String g3, String g4, String g5, String g6) {
     
    		this.first = first;
    		this.last = last;
    		this.g1 = g1;
    		this.g2 = g2;
    		this.g3 = g3;
    		this.g4 = g4;
    		this.g5 = g5;
    		this.g6 = g6;
    	}
     
    	@Override
    	public double calculateGPA() {
     
    		average = (convertGradeToNumeric(g1) +
    		convertGradeToNumeric(g2) +
    		convertGradeToNumeric(g3) +
    		convertGradeToNumeric(g4) +
    		convertGradeToNumeric(g5) +
    		convertGradeToNumeric(g6))/6;
     
    		return average;
    	}
     
    	@Override
    	public double convertGradeToNumeric(String grade) {
     
    		double numeric = 0;
     
    		if (grade == "A")
    			numeric = 4.0;
    		else if (grade == "B")
    			numeric = 3.0;
    		else if (grade == "C")
    			numeric = 2.0;
    		else if (grade == "D")
    			numeric = 1.0;
    		else
    			numeric = 0.0;
     
     
    		return numeric;
    	}
     
    	@Override
    	public int compareTo(Object otherElement) {
     
    		myGraduate other = (myGraduate)otherElement;
     
    		if (other.calculateGPA() == this.average)
    			return 0;
    		else if (other.calculateGPA() < this.average)
    			return -1;
    		else
    			return 1;
     
    	}
     
    	public String toString() {
     
    		String out = "";
     
    		out = "\n" + average + " " + last + ", " + first;
     
    		return out;
    	}
     
     
    }
    Last edited by imamess; April 12th, 2012 at 12:48 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Working with Interfaces

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at myGraduates.getGraduateGPA(myGraduates.java:43)
    At line 43 in myGraduates there is a null variable. Look at the code at that line, find the variable that has a null value and then backtrack in the code to find out why that variable does not have a valid value.
    If you can tell what variable is null, add a println just before line 43 to print out the values of all the variables used on line 43.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with Interfaces

    Quote Originally Posted by Norm View Post
    At line 43 in myGraduates there is a null variable. Look at the code at that line, find the variable that has a null value and then backtrack in the code to find out why that variable does not have a valid value.
    If you can tell what variable is null, add a println just before line 43 to print out the values of all the variables used on line 43.
    Thank you. I've seem to have gotten past the error by changing the paramaters for getGraduateGPA, but now I seem to be having trouble getting the Graduate GPA. It always comes out 0. I'm trying to call getGraduateGPA by using the count, which I get from getNumberOfGraduates, minus 1.

    	public class addGradButton implements ActionListener
    	   {
    		   public void actionPerformed (ActionEvent event) {
     
    			   double average;
    			   int numGrads;			      
     
    			   entry.addGraduate(firstField.getText(), lastField.getText(), field110.getText(), 
    					   field140.getText(), field226.getText(), field249.getText(), 
    					   field213.getText(), field214.getText());			   
     
     
    			   average = entry.getGraduateGPA(entry.getNumberOfGraduates()-1);
     
    			   numGrads = entry.getNumberOfGraduates();
     
    			   fieldNumGrads.setText(Double.toString(numGrads));			   
     
    			   fieldGradGPA.setText(Double.toString(average));
     
     
     
     
     
    		    }
    	   }

    I think the problem is whats being sent using the following method from myGraduates.

    	public double getGraduateGPA(int index) {
     
    		double gpa;
     
    		gpa = x[index].calculateGPA();	
     
    		return gpa;
    	}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Working with Interfaces

    What values does the calculateGPA() method have to work with to compute the value that it returns?

    What does the println() in getGraduateGPA() method print out?

    x is not a very descriptive variable name. What does the x array have in it?

    (grade == "A")
    You should use the equals() method for comparing Strings, not the == operator
    Last edited by Norm; April 12th, 2012 at 11:45 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Working with Interfaces

    Quote Originally Posted by Norm View Post
    What values does the calculateGPA() method have to work with to compute the value that it returns?

    What does the println() in getGraduateGPA() method print out?

    x is not a very descriptive variable name. What does the x array have in it?

    (grade == "A")
    You should use the equals() method for comparing Strings, not the == operator
    That was one of the problems. I got everything working. Thank you very much.

Similar Threads

  1. Interfaces
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: July 13th, 2018, 12:57 AM
  2. Using JFrame interfaces
    By Andreasweiz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2012, 05:01 PM
  3. Interfaces
    By leyland in forum Java Theory & Questions
    Replies: 4
    Last Post: April 5th, 2011, 08:51 PM
  4. Can't understand why Interfaces are there
    By vortexnl in forum Object Oriented Programming
    Replies: 9
    Last Post: February 14th, 2011, 01:06 PM
  5. Importance of interfaces
    By jyothishey in forum Object Oriented Programming
    Replies: 9
    Last Post: March 12th, 2010, 07:11 AM