1

I'm trying to DELETE some data from SQL when a button is clicked. However, I keep getting the error java.lang.NumberFormatException: null when the button is clicked. This confusses me because I can display the ID in my jsp.file, so i know the value is selected and displayed.

Here is my jsp file:

if (request.getParameter("delete") != null){
 long betID = Long.parseLong(request.getParameter("id"));
 System.out.print(betID);
}

<form action="newBet.jsp" method="get">
    <fieldset>
        <input class="btn btn-default" type="submit" name="w" value="Vundet"/> 
        <input class="btn btn-default" type="submit" name="L" value="Tabt" /> 
        <input type="submit" name="id" value="<%=bet.getId()%>"/>
    </fieldset>
</form>

This successfully shows the value bet.getId() in my jsp file. I will ofc change type to "hidden" when I'm done. The problem is when I click the button "delete" it does not ready my value.

Here is my Java:

public static void removeBet(long betID) throws SQLException {

 Connection connection = null;   

    try { 

        DB_Connection connect = new DB_Connection();
        connection=connect.get_Connection();

        String sql = "DELETE FROM bets WHERE id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setLong(1, betID);
        ps.executeUpdate();

      }catch (SQLException e) {
        e.printStackTrace();
    } finally {
        connection.close();
    }

The value of my betID is set to long everywhere in my code. I hope some of you might be able to help me. I've checked similar questions, but unfortunatly I've not been able to solve the issues. Thank you.

4
  • Any chance you have another submit with name = delete? (Seems from the code you wrote). HTML will only send value of submit you click. Others will be null. (In case would be better to store the id in hidden or not writable text input) Commented Jan 21, 2020 at 11:18
  • Yes I do. I forgot to post that in the code aswell. My other submit with name = delete is: <form action="newBet.jsp" method="get"> <td><input class="btn btn-default" type="submit" name="delete" value="X"/></td> </form> Commented Jan 21, 2020 at 11:20
  • 1
    HTML will only send value of submit you click. Others will be null. (In case would be better to store the id in hidden or not writable text input) Commented Jan 21, 2020 at 11:21
  • Thank you so much. I'm still very new, but this actually solved my problem. Thank you! Commented Jan 21, 2020 at 11:26

1 Answer 1

1

I add the answer just to put in line all the comments.

Only the value of the submit actually clicked will be passed as request param. This is why when you click on the name="delete" submit, you see the value on the html page built from jsp but not in the request.

I would even advice you taglib (es. JSTL) insted of writing java code in JSP.

For instance, imagining you have a request or page param named "bet" cotaining the bet bean,

<form action="newBet.jsp" method="get">
    <fieldset>
        <input class="btn btn-default" type="submit" name="w" value="Vundet"/> 
        <input class="btn btn-default" type="submit" name="L" value="Tabt" /> 
        <input type="submit" name="id" value="${bet.id}"/>
    </fieldset>
</form>

Get a log to JSTL too : <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
https://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/

Regards

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

Comments

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.