-5

I am my making a project Bank management System. The code here is of the 3rd page of my project where user have to choose the account type he wants. If he is not selecting any account type then a message dialog will pop up which shows account type is required. But I am not getting message dialog box. here is the piece of code where I think the problem isn bt I am unable to resolve it.

'''JAVA

public void actionPerformed(ActionEvent ae)
        {
            String accountType=null;
            if(ae.getSource()==submit)
                {
                    
                    if(r1.isSelected()) {
                        accountType="Saving Account";
                    } else if(r2.isSelected())
                    {
                        accountType="Fixed Deposit Account";
                    }
                    
                    else if(r3.isSelected())
                    {
                        accountType="Current Account";
                    }
                    
                    else if(r4.isSelected())
                    {
                        accountType="Recuuring Deposit Account";
                    }       
                    Random random=new Random();
                    String cardnumber=" "+Math.abs((random.nextLong()%90000000L+1609972900000000L));
                    String pinnumber=" "+Math.abs((random.nextLong()%9000L+1000L));
                    
                    String facility="";
                    
                    if(c1.isSelected())
                    {
                        facility=facility+" ATM Card";
                    }
                    
                    else if(c2.isSelected())
                    {
                        facility=facility+" Internet Banking";
                    }
                    
                    else if(c3.isSelected())
                    {
                        facility=facility+" Mobile Banking";
                    }
                    
                    else if(c4.isSelected())
                    {
                        facility=facility+" Email & SMS Alerts";
                    }
                    
                    else if(c5.isSelected())
                    {
                        facility=facility+" Cheque Book";
                    }
                    
                    else if(c6.isSelected())
                    {
                        facility=facility+" E-Statement";
                    }
                    
                    try {
                        if(accountType.equals("")){
                            Conn conn=new Conn();
                            String query1="insert into Signupthree values('"+formno+"','"+accountType+"','"+cardnumber+"','"+pinnumber+"','"+facility+"')";
                            String query2="insert into login values('"+formno+"','"+cardnumber+"','"+pinnumber+"')";
                            conn.s.executeUpdate(query1);
                            conn.s.executeUpdate(query2);
                            
                            JOptionPane.showMessageDialog(null, "Card Number: "+cardnumber+"\n Pin: "+pinnumber);
                        }
                        else{
                            JOptionPane.showMessageDialog(null, "AccountType is Required");
                        }
                    } catch (Exception e) {
                        System.out.println(e);
                                            '''

I am getting this error '''java java.lang.NullPointerException: Cannot invoke "String.equals(Object)" because "accountType" is null`

Nikhil
  • 1
  • 1
  • Why not simply check for null first? `if (foo == null) { ... .don't try to use foo here ...} else { .... OK to use foo here ....}` – Hovercraft Full Of Eels May 19 '22 at 22:57
  • You initialized `accountType` to `null` ... but you are then calling `accountType.equals("")` to test it. That fails. Compare it against `null`; e.g. `accountType == null` – Stephen C May 19 '22 at 22:57
  • If your customer is allowed to select none of the r1/r2/r3/r4, you should get null accountType there. Your code should consider it first. If your customer is NOT allowed to unselect all, but should choose one, that condition should be forced by your GUI layer. – MNEMO May 20 '22 at 00:16

0 Answers0