3 Replies - 552 Views - Last Post: 14 March 2009 - 05:35 PM Rate Topic: -----

#1 MeaganMML  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Using calc button in conjunction with radio buttons

Post icon  Posted 14 March 2009 - 12:17 PM

This is part of a bigger program(GUI interface) that is suppose to calculate the grades, it compiles okay but I get the error below when I hit the "calc" button...does anyone know what the heck I'm doing wrong?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class GradesTypePanel extends JPanel
{
	//components used
	
	private JRadioButton numerical;
	private JRadioButton alphabetical;
	private JButton calcButton;
	private ButtonGroup bg;
	private TestGradesPanel grades;
	private JTextField resultsField;
	private JLabel results;

	/**
		constructor
	*/
	
	public GradesTypePanel()
	{
		//create grid layout
		setLayout(new GridLayout(2,2));
		
		//create the radio buttons
		numerical = new JRadioButton("Numerical", true);
		alphabetical = new JRadioButton("Alphabetical");
		calcButton = new JButton("Calc");
		results = new JLabel("Results");
		resultsField = new JTextField(10);
		resultsField.setEditable(false);
		
		//register event listener
		calcButton.addActionListener(new calcButtonListener());
		
		//group the buttons
		bg = new ButtonGroup();
		bg.add(numerical);
		bg.add(alphabetical);
				//add border
		setBorder(BorderFactory.createTitledBorder("Grade Type"));
		
		//add the radio buttons to the panel
		add(numerical);
		add(alphabetical);
		add(calcButton);
		add(results);
		add(resultsField);


	}
	
	//private inner class for calc button
	private class calcButtonListener implements ActionListener
	{
			String letterGrade, A, B, C, D, F;
			double input;

			
		public void actionPerformed(ActionEvent e)
		{
			double input;
			double grade;
			
			
			input = grades.TestGrades();
			
			if (numerical.isSelected())
			{
				grade = input;
				resultsField.setText(Double.toString(grade));
				
			}
							

	
		
		else 
			{
				grade = input;
			
				if (input < 60)
					letterGrade =F;
				else if (input < 70)
					letterGrade = D;
				else if (input < 80)
					letterGrade = C;
				else if (input <  90)
					letterGrade = B;
				else if (input <= 100)
					letterGrade = A;
				resultsField.setText(letterGrade);
			}	
		}
	}
}



This is the error I get
			
 ----jGRASP exec: java RunGradesProgram

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at GradesTypePanel$calcButtonListener.actionPerformed(GradesTypePanel.java:69)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6134)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
	at java.awt.Component.processEvent(Component.java:5899)
	at java.awt.Container.processEvent(Container.java:2023)
	at java.awt.Component.dispatchEventImpl(Component.java:4501)
	at java.awt.Container.dispatchEventImpl(Container.java:2081)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
	at java.awt.Container.dispatchEventImpl(Container.java:2067)
	at java.awt.window.dispatchEventImpl(window.java:2458)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.E



Is This A Good Question/Topic? 0
  • +

Replies To: Using calc button in conjunction with radio buttons

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,782
  • Joined: 20-September 08

Re: Using calc button in conjunction with radio buttons

Posted 14 March 2009 - 04:10 PM

>>private TestGradesPanel grades

The above is not initialized, so you'll get an NPE
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,133
  • Joined: 06-March 08

Re: Using calc button in conjunction with radio buttons

Posted 14 March 2009 - 04:58 PM

These are not initialized

String letterGrade, A, B, C, D, F;

String A = "A", B = "B", C = "C", .....

actually for the use you do with it better just to hard code it:


letterGrade = "D";
instead of
letterGrade = D;
Was This Post Helpful? 1
  • +
  • -

#4 MeaganMML  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: Using calc button in conjunction with radio buttons

Posted 14 March 2009 - 05:35 PM

View Postg00se, on 14 Mar, 2009 - 03:10 PM, said:

>>private TestGradesPanel grades

The above is not initialized, so you'll get an NPE



If I initialize it as

private double TestGradesPanel grades;

I get
 ----jGRASP exec: javac -g C:\Java Progs\Project 2\project2 4th new\GradesTypePanel.java

GradesTypePanel.java:14: ';' expected
	private double TestGradesPanel grades;
								  ^
GradesTypePanel.java:14: <identifier> expected
	private double TestGradesPanel grades;
										 ^
2 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



If I initialize it as

double TestGradesPanel grades;

 ----jGRASP exec: javac -g C:\Java Progs\Project 2\project2 4th new\GradesTypePanel.java

GradesTypePanel.java:14: ';' expected
	double TestGradesPanel grades;
						  ^
GradesTypePanel.java:14: <identifier> expected
	double TestGradesPanel grades;
								 ^
2 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.



But I'm not surprise, since they are not proper initializations

If I put it in as
double grades;

I get
  ----jGRASP exec: javac -g C:\Java Progs\Project 2\project2 4th new\GradesTypePanel.java

GradesTypePanel.java:68: double cannot be dereferenced
			input = grades.TestGrades();
						  ^
1 error

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1