user3819936
user3819936

Reputation: 35

using switch statement to provide action to jbuttons java

I'm trying to use switch for triggering a button's statement. I've programmed frame1 to open JFrame of 2nd class. In 2nd class there are four buttons which will work on Switch statement and I'm using a variable from 1st class to work as choice for Switch, but I'm getting a null pointer exception. Why so?

Class1( BasicInfo.java)

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    name= n.getText();
    email=e.getText();
    street=s.getText();
    state=st.getText();
    city=c.getText();
    gender=(String)g.getSelectedItem();
    qualification=(String)q.getSelectedItem();
    date=Integer.parseInt((String)d.getSelectedItem());
    month=Integer.parseInt((String)m.getSelectedItem());
    year=Integer.parseInt(y.getText());
    phone_num=Integer.parseInt(nm.getText());
    //this.dispose();
                    new ViewSample().setVisible(true);
    }

Class 2(ViewSample.java)

void open()
{
    BasicInfo x= new BasicInfo();
    System.out.print(x.qualification);
    switch(x.qualification)
    {
        case "BE":
            System.out.print("a");
            break;
            case "MBA":
            System.out.print("b");
                break;
            default:
    }
}

private void s1ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void s4ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void s2ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void s3ActionPerformed(java.awt.event.ActionEvent evt) {
    open();
}

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
    this.dispose();
}

Upvotes: 0

Views: 3564

Answers (1)

nIcE cOw
nIcE cOw

Reputation: 24626

As noted in my comments, I was of the opinion of doing something like, as shown in this code example:

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

public class PassingValuesExample {

    private String qualification;
    private JTextField qualificationField;
    private JButton submitButton;

    private void displayGUI() {
        JFrame frame = new JFrame("Swing Worker Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        qualificationField = new JTextField(10);
        submitButton = new JButton("Submit");
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                submitButtonActionPerformed(ae);
            }
        });
        contentPane.add(qualificationField);
        contentPane.add(submitButton);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private void submitButtonActionPerformed(ActionEvent ae) {
        qualification = qualificationField.getText();
        new Foo(this).open();
    }

    public String getQualification() {
        return qualification;
    }

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new PassingValuesExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

class Foo {

    private PassingValuesExample pve;

    public Foo(PassingValuesExample pve) {
        this.pve = pve;
    }

    public void open() {
        String qual = pve.getQualification();
        switch(qual) {
            case "CA":
            case "MBA":
                System.out.println("Qualification: " + qual);
        }
    }
}

Upvotes: 1

Related Questions