Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception with JRadioButton

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I'm receiving this error at runtime, after entering a value, selecting the 'Binary' checkbox and clicking go. The value prints out to the console OK, but there seems to be a problem with calling the .isSelected() method on the radio button.

Any help is appreciated :).
Code:
package convertor;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Main {
    private JFrame mainwindow;
    private Container contentpane;
    private Container convtype;
    
    private JButton gobutton;
    private JTextField input;
    private JTextField binaryout;
    private JTextField decimalout;
    private JRadioButton binaryconv;
    
    public Main() {
    }
    
    public static String bin2dec(String input) {
        int len = input.length();
        int power = len-1;
        int total = 0;
        for(int i=0;i<len;i++) {
            int binval = Character.getNumericValue(input.charAt(i));
            total += Math.pow(2,power)*binval;
            power--;
        }
        return ""+total;
    }
    
    public void bin2all(String inputval) {
        decimalout.setText("1"); // bin2dec(inputval));
    }
    
    public void calculate() {
        String inputval = input.getText();
// System.out.println(inputval);
// System.out.println("S"+binaryconv.isSelected());
        if(binaryconv.isSelected()) {
            bin2all(inputval);
        }
    }
    
    public void drawgui() {
        mainwindow = new JFrame();
        mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainwindow.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.ipady = 1;
        c.insets = new Insets(1,1,1,1);
        
        JLabel inputlabel = new JLabel("Input:");
        input = new JTextField("");
        c.gridx = 0;
        c.gridy = 0;
        mainwindow.add(inputlabel,c);
        c.gridx = 1;
        mainwindow.add(input,c);
        
        contentpane = new JPanel(new GridLayout(1,4));
        c.gridx = 0;
        c.gridwidth = 2;
        c.gridy = 1;
        JRadioButton binaryconv = new JRadioButton("Binary");
        ButtonGroup ctype = new ButtonGroup();
        ctype.add(binaryconv);
        contentpane.add(binaryconv);
        gobutton = new JButton("Go");
        contentpane.add(gobutton);
        gobutton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) { calculate(); }
        });
        mainwindow.add(contentpane,c);
        
        JLabel binarylabel = new JLabel("Binary:");
        binaryout = new JTextField();
        c.gridx = 0;
        c.gridy = 2;
        mainwindow.add(binarylabel,c);
        c.gridx = 1;
        mainwindow.add(binaryout,c);
        
        mainwindow.pack();
        mainwindow.setVisible(true);
    }
    
    public static void main(String[] args) {
        Main main = new Main();
        main.drawgui();
    }
    
}
Give the error:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at convertor.Main.calculate(Main.java:42)
        at convertor.Main$1.actionPerformed(Main.java:76)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
 
You declared binaryconv twice. Scoping problem

Try changing

Code:
JRadioButton binaryconv = new JRadioButton("Binary");

to
Code:
binaryconv = new JRadioButton("Binary");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top