Results 1 to 7 of 7
  1. #1
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default JButtons and If statements

    Hi - I'm back. No surprise there. I am trying to write a program that calculates the number of gallons * the selected FuelTypeRadioButton. I have everything in place - but the calculation is not recognizing the "IF SELECTED" statements. I've highlighted where I think I've gone wrong. :confused: RED

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;
    import java.util.Date;
    
    public class FuelService extends JApplet implements ActionListener
    {
    	// declarations
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	Color light_gray = new Color(240, 240, 240);
    
    	DecimalFormat decimalFormat;
     
    	//format objects
    	DateFormat formatter;
    
    	// components
    	JLabel numberofGallonsJLabel;
    	JTextField numberofGallonsJTextField;
    
    	JLabel AmountJLabel;
    	JTextField AmountJTextField;
    
    	JRadioButton FuelTypeGJRadioButton;
    	JRadioButton FuelTypeDJRadioButton;
    	JRadioButton FuelTypeHJRadioButton;
    	ButtonGroup displayButtonGroup;
    
    	JButton enterJButton;
    	JButton clearJButton;
    
    	JTextArea selectfuelTypeJTextArea;
    
    	JTextField FuelTypeJTextField;
    	
    	JTextField selectedJTextField;
    
    	JTextField citeJTextField;
    
    	String citeText = "n";
    	
    	// variables
    	int numberofGallons;
    	int Amount;
    	int selected;
    	int FuelTypeButton;
    
    	public void init()
    	{
    		setLayout(null);
    		setSize(400, 400);
    		
    		// initialize components
    
    		selectfuelTypeJTextArea = new JTextArea();
    		selectfuelTypeJTextArea.setText("Select Fuel Type");
    		selectfuelTypeJTextArea.setBounds(90, 50, 100, 20);
    		selectfuelTypeJTextArea.setFont(new Font("Default", Font.PLAIN, 12));
    		selectfuelTypeJTextArea.setForeground(black);
    		selectfuelTypeJTextArea.setBackground(light_gray);
    		selectfuelTypeJTextArea.setEditable(false);
    		add(selectfuelTypeJTextArea);
    
    		numberofGallonsJLabel = new JLabel();
    		numberofGallonsJLabel.setBounds(90, 200, 120, 20);
    		numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		numberofGallonsJLabel.setText("Number of Gallons");
    		numberofGallonsJLabel.setForeground(black);
    		numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
    		add(numberofGallonsJLabel);
    		
    		numberofGallonsJTextField = new JTextField();
    		numberofGallonsJTextField.setBounds(200, 200, 60, 20);
    		numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
    		numberofGallonsJTextField.setForeground(black);
    		numberofGallonsJTextField.setBackground(white);
    		numberofGallonsJTextField.setEditable(true);
    		add(numberofGallonsJTextField);
    
    		AmountJLabel = new JLabel();
    		AmountJLabel.setBounds(90, 230, 90, 20);
    		AmountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		AmountJLabel.setText("Amount");
    		AmountJLabel.setForeground(black);
    		AmountJLabel.setBackground(white);
    		add(AmountJLabel);					
    
    		AmountJTextField = new JTextField();
    		AmountJTextField.setBounds(200, 230, 60, 20);
    		AmountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		AmountJTextField.setHorizontalAlignment(JTextField.CENTER);
    		AmountJTextField.setForeground(black);
    		AmountJTextField.setBackground(white);
    		AmountJTextField.setEditable(true);
    		add(AmountJTextField);
    
    		displayButtonGroup = new ButtonGroup();
    		
    		FuelTypeGJRadioButton = new JRadioButton();
    		FuelTypeGJRadioButton.setBounds(90, 85, 140, 20);
    		FuelTypeGJRadioButton.setFont(new Font("Default", Font.BOLD, 12));
    		FuelTypeGJRadioButton.setText("Gasoline $1.90");
    		FuelTypeGJRadioButton.setSelected(true);
    		FuelTypeGJRadioButton.setForeground(black);
    		FuelTypeGJRadioButton.setBackground(white);
    		displayButtonGroup.add(FuelTypeGJRadioButton);
    		add(FuelTypeGJRadioButton);
    
    		FuelTypeDJRadioButton = new JRadioButton();
    		FuelTypeDJRadioButton.setBounds(90, 120, 140, 20);
    		FuelTypeDJRadioButton.setFont(new Font("Default", Font.BOLD, 12));
    		FuelTypeDJRadioButton.setText("Diesel $2.00");
    		FuelTypeDJRadioButton.setSelected(true);
    		FuelTypeDJRadioButton.setForeground(black);
    		FuelTypeDJRadioButton.setBackground(white);
    		displayButtonGroup.add(FuelTypeDJRadioButton);
    		add(FuelTypeDJRadioButton);
    
    		FuelTypeHJRadioButton = new JRadioButton();
    		FuelTypeHJRadioButton.setBounds(90, 155, 140, 20);
    		FuelTypeHJRadioButton.setFont(new Font("Default", Font.BOLD, 12));
    		FuelTypeHJRadioButton.setText("Hydrogen $2.25");
    		FuelTypeHJRadioButton.setSelected(false);
    		FuelTypeHJRadioButton.setForeground(black);
    		FuelTypeHJRadioButton.setBackground(white);
    		displayButtonGroup.add(FuelTypeHJRadioButton);
    		add(FuelTypeHJRadioButton);
    
    		enterJButton = new JButton();
    		enterJButton.setBounds(80, 300, 100, 20);
    		enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		enterJButton.setText("Enter");
    		enterJButton.setForeground(black);
    		enterJButton.setBackground(white);
    		add(enterJButton);
    		enterJButton.addActionListener(this);
    
    		clearJButton = new JButton();
    		clearJButton.setBounds(200, 300, 100, 20);
    		clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		clearJButton.setText("Clear");
    		clearJButton.setForeground(black);
    		clearJButton.setBackground(white);
    		add(clearJButton);
    		clearJButton.addActionListener(this);
    
    		
    		citeJTextField = new JTextField();
    		citeJTextField.setBounds(40, 350, 300, 20);
    		citeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		citeJTextField.setHorizontalAlignment(JTextField.CENTER);
    		citeJTextField.setForeground(black);
    		citeJTextField.setBackground(light_gray);
    		citeJTextField.setEditable(false);
    		add(citeJTextField);
    
    		formatter = DateFormat.getDateTimeInstance();
    		citeJTextField.setText("" + citeText + " " + formatter.format(new Date()));
    
    
    	}
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Object obj = event.getSource();
    		if(obj == enterJButton)
    		{
    			getFuelTypeButton();
    		}
    
    		if(obj == clearJButton)
    		{
    			clearAll();
    		}
    	}
    
    [COLOR="Red"]	public void getFuelTypeButton()
    	{
    		if(FuelTypeGJRadioButton.isSelected())
    		{
    			selectedJTextField.setText("1.50");
    		}
    
    		if(FuelTypeDJRadioButton.isSelected())
    		{
    			selectedJTextField.setText("2.00");
    		}
    
    		if(FuelTypeHJRadioButton.isSelected())
    		{
    			selectedJTextField.setText("2.25");
    		}
    		getnumberofGallons();
    	}[/COLOR]
    	public void getnumberofGallons()
    	{
    		try
    		{
    		numberofGallons = Integer.parseInt(numberofGallonsJTextField.getText());
    		getAmount();
    		}
    		catch(NumberFormatException exception)
    		{
    			JOptionPane.showMessageDialog(this,
    			"Please enter the number of gallons!",
    			"Number Format Error", JOptionPane.ERROR_MESSAGE);
    			numberofGallonsJTextField.setText("");
    			numberofGallonsJTextField.requestFocusInWindow(); 
    		}
    
    	}
    
    [COLOR="red"]	public void getAmount()
    
    	{
    		Amount = FuelTypeButton * numberofGallons;
    		displayAmount();
    	}[/COLOR]
    	public void displayAmount()
    	{
    		decimalFormat = new DecimalFormat("$0.00");
    		AmountJTextField.setText("" + decimalFormat.format(Amount));
    	}
    
    	public void clearAll()
    	{
    		numberofGallonsJTextField.setText("");
    		numberofGallonsJTextField.requestFocusInWindow();
    		AmountJTextField.setText("");
    	}
    
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    Well what happens when you add some System.out.println(...) statements to your code?

    Or what happens when you use a debugger to step through the code.

    Does the ActionEvent get executed?

    Also, learn proper Java naming conventions which can be found in any text book or tutorial. Your code is hard to read because you should NOT be capitalizing the first character of your variable names.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're getting a NullPointerException which means you're trying to use on object that has not been initialized. Look at the line that is throwing the NPE, and an object on that line (there's only one) is null. Then look at your code and you'll likely be able to figure out why. For instance, check out that object ... is it ever initialized? is it ever displayed? (no and no).

  4. #4
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default

    sorry about the caps. i keep renaming my variables. i changed those...

  5. #5
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default

    a few notes:
    i really am NEW at this. really. so please excuse my ignorance when i talk about coding - i'm trying..........
    i use text pad - so my program compiles correctly - i don't work with a debugger...unless you're talking about that lovely black screen that comes up behind my display. i did take a look at the screen and found that the fueltype is where the exception is occurring. i'm working on that now but i think what i'm struggling with has more to do with logic flow. i'll post my updates...and i do really appreciate the pointers. your comments are certainly more helpful than the book i have.

  6. #6
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default

    here are my updates. i can only get the radio button info to print to the black screen and not factor in my amount equation. argh. highlighted in red.
    if i add a
    displayfuelType();
    it goes back to not registering anything in the amount section.

    Java Code:
    /*	document segment
    	filename:	FuelService
    	author:		N
    	date:		October.2009
    */
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.text.*;
    import java.util.Date;
    
    public class FuelService extends JApplet implements ActionListener
    {
    	// declarations
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	Color light_gray = new Color(240, 240, 240);
    
    	DecimalFormat decimalFormat;
     
    	//format objects
    	DateFormat formatter;
    
    	// components
    	JLabel numberofGallonsJLabel;
    	JTextField numberofGallonsJTextField;
    
    	JLabel amountJLabel;
    	JTextField amountJTextField;
    
    	JRadioButton fueltypeGJRadioButton;
    	JRadioButton fueltypeDJRadioButton;
    	JRadioButton fueltypeHJRadioButton;
    	ButtonGroup displayButtonGroup;
    
    	JButton enterJButton;
    	JButton clearJButton;
    
    	JTextArea selectfueltypeJTextArea;
    
    	JTextField fuelTypeJTextField;
    	
    	JTextField citeJTextField;
    
    	String citeText = "n";
    	
    	// variables
    	double fuelType;
    	double numberofGallons;
    	double amount;
    	int fueltypeG;
    	int fueltypeH;
    	int fueltypeD;
    
    	
    	public void init()
    	{
    		setLayout(null);
    		setSize(400, 400);
    		
    		// initialize components
    
    		selectfueltypeJTextArea = new JTextArea();
    		selectfueltypeJTextArea.setText("Select Fuel Type");
    		selectfueltypeJTextArea.setBounds(90, 50, 100, 20);
    		selectfueltypeJTextArea.setFont(new Font("Default", Font.PLAIN, 12));
    		selectfueltypeJTextArea.setForeground(black);
    		selectfueltypeJTextArea.setBackground(light_gray);
    		selectfueltypeJTextArea.setEditable(false);
    		add(selectfueltypeJTextArea);
    
    		numberofGallonsJLabel = new JLabel();
    		numberofGallonsJLabel.setBounds(90, 200, 120, 20);
    		numberofGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		numberofGallonsJLabel.setText("Number of Gallons");
    		numberofGallonsJLabel.setForeground(black);
    		numberofGallonsJLabel.setHorizontalAlignment(JLabel.LEFT);
    		add(numberofGallonsJLabel);
    		
    		numberofGallonsJTextField = new JTextField();
    		numberofGallonsJTextField.setBounds(200, 200, 60, 20);
    		numberofGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		numberofGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
    		numberofGallonsJTextField.setForeground(black);
    		numberofGallonsJTextField.setBackground(white);
    		numberofGallonsJTextField.setEditable(true);
    		add(numberofGallonsJTextField);
    
    		amountJLabel = new JLabel();
    		amountJLabel.setBounds(90, 230, 90, 20);
    		amountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		amountJLabel.setText("Amount");
    		amountJLabel.setForeground(black);
    		amountJLabel.setBackground(white);
    		add(amountJLabel);					
    
    		amountJTextField = new JTextField();
    		amountJTextField.setBounds(200, 230, 60, 20);
    		amountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		amountJTextField.setHorizontalAlignment(JTextField.CENTER);
    		amountJTextField.setForeground(black);
    		amountJTextField.setBackground(white);
    		amountJTextField.setEditable(true);
    		add(amountJTextField);
    
    		displayButtonGroup = new ButtonGroup();
    		
    		fueltypeGJRadioButton = new JRadioButton();
    		fueltypeGJRadioButton.setBounds(90, 85, 140, 20);
    		fueltypeGJRadioButton.setFont(new Font("Default", Font.BOLD, 12));
    		fueltypeGJRadioButton.setText("Gasoline $1.90");
    		fueltypeGJRadioButton.setSelected(true);
    		fueltypeGJRadioButton.setForeground(black);
    		fueltypeGJRadioButton.setBackground(white);
    		displayButtonGroup.add(fueltypeGJRadioButton);
    		add(fueltypeGJRadioButton);
    
    		fueltypeDJRadioButton = new JRadioButton();
    		fueltypeDJRadioButton.setBounds(90, 120, 140, 20);
    		fueltypeDJRadioButton.setFont(new Font("Default", Font.BOLD, 12));
    		fueltypeDJRadioButton.setText("Diesel $2.00");
    		fueltypeDJRadioButton.setSelected(true);
    		fueltypeDJRadioButton.setForeground(black);
    		fueltypeDJRadioButton.setBackground(white);
    		displayButtonGroup.add(fueltypeDJRadioButton);
    		add(fueltypeDJRadioButton);
    
    		fueltypeHJRadioButton = new JRadioButton();
    		fueltypeHJRadioButton.setBounds(90, 155, 140, 20);
    		fueltypeHJRadioButton.setFont(new Font("Default", Font.BOLD, 12));
    		fueltypeHJRadioButton.setText("Hydrogen $2.25");
    		fueltypeHJRadioButton.setSelected(false);
    		fueltypeHJRadioButton.setForeground(black);
    		fueltypeHJRadioButton.setBackground(white);
    		displayButtonGroup.add(fueltypeHJRadioButton);
    		add(fueltypeHJRadioButton);
    
    		enterJButton = new JButton();
    		enterJButton.setBounds(80, 300, 100, 20);
    		enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		enterJButton.setText("Enter");
    		enterJButton.setForeground(black);
    		enterJButton.setBackground(white);
    		add(enterJButton);
    		enterJButton.addActionListener(this);
    
    		clearJButton = new JButton();
    		clearJButton.setBounds(200, 300, 100, 20);
    		clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		clearJButton.setText("Clear");
    		clearJButton.setForeground(black);
    		clearJButton.setBackground(white);
    		add(clearJButton);
    		clearJButton.addActionListener(this);
    
    		
    		citeJTextField = new JTextField();
    		citeJTextField.setBounds(40, 350, 300, 20);
    		citeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		citeJTextField.setHorizontalAlignment(JTextField.CENTER);
    		citeJTextField.setForeground(black);
    		citeJTextField.setBackground(light_gray);
    		citeJTextField.setEditable(false);
    		add(citeJTextField);
    
    		formatter = DateFormat.getDateTimeInstance();
    		citeJTextField.setText("" + citeText + " " + formatter.format(new Date()));
    
    
    	}
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Object obj = event.getSource();
    		if(obj == enterJButton)
    		{
    			getfuelType();
    		}
    
    		if(obj == clearJButton)
    		{
    			clearAll();
    		}
    	}
    
    [COLOR="Red"]	public void getfuelType()
    	{
    		if(fueltypeGJRadioButton.isSelected())
    		{
    			System.out.print("$1.90");
    		}
    
    		if(fueltypeDJRadioButton.isSelected())
    		{
    			System.out.print("$2.00");
    		}
    
    		if(fueltypeHJRadioButton.isSelected())
    		{
    			System.out.print("$2.25");
    		}
    		getnumberofGallons();
    	}[/COLOR]
    	public void getnumberofGallons()
    	{
    		try
    		{
    		numberofGallons = Integer.parseInt(numberofGallonsJTextField.getText());
    		getamount();
    		}
    		catch(NumberFormatException exception)
    		{
    			JOptionPane.showMessageDialog(this,
    			"Please enter the number of gallons!",
    			"Number Format Error", JOptionPane.ERROR_MESSAGE);
    			numberofGallonsJTextField.setText("");
    			numberofGallonsJTextField.requestFocusInWindow(); 
    		}
    
    	}
    
    	public void getamount()
    
    	{
    		amount = fuelType * numberofGallons;
    		displayamount();
    	}
    
    
    	public void displayamount()
    	{
    		decimalFormat = new DecimalFormat("$0.00");
    		amountJTextField.setText("" + decimalFormat.format(amount));
    	}
    
    
    	public void clearAll()
    	{
    		numberofGallonsJTextField.setText("");
    		numberofGallonsJTextField.requestFocusInWindow();
    		amountJTextField.setText("");
    	}
    
    }

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    A NullPointException tells you a variable is null. The error messages tells you the line of code causing the problem. So you need to find the variable and make sure it has been "created", not just "declared".

Similar Threads

  1. placing JButtons with x and y coords (possible?)
    By v1nsai in forum New To Java
    Replies: 4
    Last Post: 08-18-2009, 06:21 AM
  2. Help with JButtons...
    By ashton in forum New To Java
    Replies: 8
    Last Post: 01-26-2009, 09:38 AM
  3. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  4. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 11:42 PM
  5. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •