Reputation: 3
I have a method in which i am trying to save the values from text boxes into databases but sometime it give me the error
Parameter index out of range (5>number of parameter, which is 1)
and some time java.lang.nullpointerexception
I don't understand what to do, please help.
Following is the code of the method in which i try to do insert.
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
try{
if (y1.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel code","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (y2.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel location","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (y3.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel name","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (y4.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter no of rooms","Error", JOptionPane.ERROR_MESSAGE);
return;}
else if (img.getText().equals("")) {
JOptionPane.showMessageDialog( this, "Please enter hostel image","Error", JOptionPane.ERROR_MESSAGE);
return;}
String room =gender.getSelectedItem().toString();
pst.setString(5, room);
Statement stmt;
stmt= conn.createStatement();
String sql1="Select HostelName from hostels where HostelName= '" + y3.getText() + "'";
rs=stmt.executeQuery(sql1);
if(rs.next()){
JOptionPane.showMessageDialog( this, "hostel name already exists","Error", JOptionPane.ERROR_MESSAGE);
y3.setText("");
y3.requestDefaultFocus();
return;
}
String sql = "INSERT INTO hostels (`id`, `hloc`, `HostelName`, `RoomNo`, `Capacity`,`image`) VALUES (?,?,?,?,?,?)";
pst=conn.prepareStatement(sql);
//for setting values corresponding ?
pst.setInt(1,Integer.parseInt(y1.getText()));
pst.setString(2,y2.getText());
pst.setString(3,y3.getText());
pst.setInt(4,Integer.parseInt(y4.getText()));
pst.setString(6,y5.getText());
pst.setString(7,y6.getText());
pst.executeUpdate();
pst.execute();
refresh();//Update jTable After adding a new record
JOptionPane.showMessageDialog(null, "Record Successfully Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Can anyone help me to get rid of this error? following is the stack trace any help now?
java.lang.NullPointerException
at Hostels.saveActionPerformed(Hostels.java:472)
at Hostels.access$700(Hostels.java:28)
at Hostels$8.actionPerformed(Hostels.java:228)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Upvotes: 0
Views: 1232
Reputation: 41188
You never actually set any values into your prepared statement. Between pst=conn.prepareStatement(sql);
and pst.execute();
you need to do pst.setXXX
to set up all the values corresponding to the ?
in your question.
Keep in mind that the ?
are indexed starting from 1, not from 0.
Upvotes: 1