-1

I want that, if I click the buttons btnA1 and btnA2 the values like that (value A1 from btnA1 & value A2 from btnA2) are added in an arraylist . And after that when I click the Confirm button the values of thearraylist are will insert into mysql database.

MY code is given below:

public class bookseat extends JFrame {

private JPanel contentPane;
private JToggleButton btnA2;
private JToggleButton btnA1;
private JButton btnConfirm;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                bookseat frame = new bookseat();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public bookseat() {
    initialize();
}

ArrayList<String> arr = new ArrayList<String>();
int i;
{
    for (i = 0; i < 200; i++) {
        if (btnA1.getAction() != null) {
            if (btnA1.isSelected()) {
                arr.add("A1");
            }

        } else if (btnA2.getAction() != null) {
            {
                if (btnA2.isSelected()) {
                    arr.add("A2");
                }
            }
        }
    }
}

Connection con;
PreparedStatement pst;
ResultSet rs;
public void connect() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/airline","root","");
        
    }
    catch(ClassNotFoundException ex) {
        
    }
    catch(SQLException ex) {
        
    }
}

private void initialize() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    btnA1 = new JToggleButton("A1");
    btnA1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(btnA1.isSelected()) {
                String st = btnA1.getText();
                arr.add(st);
            }
        }
    });
    btnA1.setBounds(10, 48, 67, 55);
    contentPane.add(btnA1);
    
    btnA2 = new JToggleButton("A2");
    btnA2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(btnA2.isSelected()) {
                String st = btnA2.getText();
                arr.add(st);
            }
        }
    });
    btnA2.setBounds(87, 48, 67, 55);
    contentPane.add(btnA2);
    
    btnConfirm = new JButton("Confirm");
    btnConfirm.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                pst = con.prepareStatement("insert into ticket_booking(Seats)values(?)");

                for(int i=0; i < arr.size();i++){
                    pst.setString(1, arr.get(i).toString());
                }
                
                pst.executeUpdate();
            }
            catch(SQLException e1) {
                
            }
        }
    });
    btnConfirm.setBounds(35, 173, 89, 23);
    contentPane.add(btnConfirm);
}
}

But my code looks wrong and give me null pointer.

java.lang.NullPointerException
    at com.alpha.bookflight.bookseat.<init>(bookseat.java:183)
    at com.alpha.bookflight.bookseat$1.run(bookseat.java:158)

java:183 line number is if (btnA1.getAction() != null)

MK.Whridoy
  • 68
  • 1
  • 8
  • Generally the error (Exception) displayed will provide the code line number causing the issue. Which code line is it? If the ArrayList is declared as a class member then what the heck is that code below it? – DevilsHnd - 退職した Jun 27 '21 at 19:32
  • mainly in my code it shows line no 182 which is `if(btnA1.getAction() != null) ` – MK.Whridoy Jun 27 '21 at 19:34
  • Has `btnA1` been initialized at this point? It may be declared...but has it been initialized? – DevilsHnd - 退職した Jun 27 '21 at 19:37
  • btnA1 is declared privately/globaly – MK.Whridoy Jun 27 '21 at 19:38
  • 1
    Would need a [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) to really see what is happening here. – DevilsHnd - 退職した Jun 27 '21 at 19:41
  • I Add a picture in my question. Because my codes are too long to give here. so i only posted the problem sectoin – MK.Whridoy Jun 27 '21 at 19:45
  • No, please don't add pictures of code or your error. All code and error text needs to be posted as code-formatted text, preferably code we can compile and run. Please read the [mre] link and try to create and post one. – Hovercraft Full Of Eels Jun 27 '21 at 19:46
  • 1
    btnA1 is null where your exception is being thrown, and likely you are *shadowing* the variable -- redeclaring it and initializing it within a limited scope, while leaving the instance field null. Where or how you're doing this, we can't say. – Hovercraft Full Of Eels Jun 27 '21 at 19:49
  • sorry can you give me the example please? I am really confused :( – MK.Whridoy Jun 27 '21 at 19:51
  • Please show adequate code and we can give an example *with your code*. Again, a [mre] would be nice. Again, please read the link for the details. – Hovercraft Full Of Eels Jun 27 '21 at 19:52
  • My codes are too long to post here so I have posted here only which parts are given error. :( – MK.Whridoy Jun 27 '21 at 19:55
  • No one is asking for the entire code. Again, please read the [mre] link that will explain what we ***do*** want. And no, you're not posting enough that can tell us where the error has come from that I can see. Yes, you're posting the line that throws it, but we can't see how/where you initialize the key variables. ..... – Hovercraft Full Of Eels Jun 27 '21 at 20:33
  • Finally I can make a solution. I declare the public `ArrayList arr = new ArrayList();` globally and remove the loop and inside the code of the buttons I write this `if(btnA1.isSelected()) { arr.add(btnA1.getText()); }` and inside the `Confirm` button i use a loop then it worked perfectly. – MK.Whridoy Jun 27 '21 at 22:33
  • Glad that you have a solution, but ask yourself -- how could any of us have provided this as a solution based on the limited information and code posted? That is why I requested (and *still* request) a valid [mre]. There are few things more frustrating than questions that are impossible to answer – Hovercraft Full Of Eels Jun 28 '21 at 02:16
  • I understand. But actually, I have posted the only problem parts. But I think my Question pattern isn't perfect. But , it's a matter of joy that I finally find the solution. :) – MK.Whridoy Jun 28 '21 at 06:08

1 Answers1

0
  1. Use a GridLayout to ensure that you have an equal-width and height grid to display the seats.
  2. Do not directly influence component positions by using setBounds().
  3. Extend a JToggleButton to a class e.g. "SeatNumberButton" that bears the row and seat number.
  4. Consider the case that seats are already occupied, or do not exist in the rectangle layout suggested by the GridLayout. (Add an empty JPanel for "no seat" and a disabled JToggleButton for "occupied", maybe change background color)
  5. Create a SeatNumberButton with the seat number and add it both to the Layout and your ArrayList.
  6. On confirmation, read out the arraylist, get the seat number from your SeatNumberButton and record this in the database.
  7. For the next customer, read all occupied seats from the database and handle point 4 with this information.
The Calif
  • 11
  • 3