0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

i use following for setting values corresponding to the ? by doing this pst=conn.prepareStatement(sql); 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(); but now it give me error java.lang.nullpointerexception
What line in the code gives the null pointer? Is there any chance that one of the y.getText() calls is returning null?
where is the line y.getText() and i set a messagebox to show me error so it didn't give me line number just show the error
Log the error as well as displaying the message box and you will see the full stack trace. That's very important when trying to track down errors, especially NullPointerException which is one of the most basic and easy to find Exceptions in Java. Print the full stack trace and/or step through the code using a Debugger and you should be able to find it very easily. By y.getText() I mean any of the y1, y2, y3, etc.
sorry but i didn't understand how to log the error give any suggestion how to do it and also no idea of debugging in netbeans just know how to start the debugger but never understand how it works
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.