public void actionPerformed(ActionEvent e) {
try {
int f=2;
String sql="select uname,pwd from customer";
PreparedStatement ps=connection.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next())
{
if(ut.getText().equals(rs.getString("uname")))
{
if(pt.getText().equals(rs.getString("pwd")))
{
// JFrame newf=new JFrame();
//newf.setLayout(null);
//newf.setVisible(true);
//f=0;
JOptionPane.showMessageDialog(null,"You have successfully logged in!");
sf second=new sf();
second.setVisible(true);
// second.actionPerformed(ActionEvent arg0);
}
else
JOptionPane.showMessageDialog(null,"Incorrect Password","Incorrect Password",JOptionPane.ERROR_MESSAGE);
f=1;
}
}
if(f==2)
JOptionPane.showMessageDialog(null,"Create new account","Incorrect Username",JOptionPane.ERROR_MESSAGE);
} catch (SQLException e1) {
e1.printStackTrace();
}
finally
{
}
}
JButton btnBuyNow = new JButton("Add to cart"); (INSIDE CONSTRUCTOR OF A JFRAME CLASS)
btnBuyNow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try
{
String sqln="Insert into cart values ('CSK Jersey','1','2000')";
PreparedStatement pstmst=connection.prepareStatement(sqln);
ResultSet rcsk=pstmst.executeQuery(sqln);
JOptionPane.showMessageDialog(null,"Product has been added to the cart successfully!");
} catch(Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
When I run TestDB, using the first actionPerformed as specified here, one JFrame is opened, the user enters the required details. If successful, another JFrame opens, this one belongs to the new class created.
Now this has a bunch of buttons but when I press any button, I'm getting an exception.
If I run this new class independently though, all the buttons function properly and the required queries are executed.
EDIT- Yeah, I'm getting a NullPointerException. Any idea as to how I can fix this?
This is what I got
You made it, take control your database now!
java.lang.NullPointerException
at sf$2.actionPerformed(sf.java:106)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)