-2

I have the following codes below:

    private Integer mTarifId;

    public Integer getTarifId() {
        return mTarifId;
    }

    public void setTarifId(Integer tarifId) {
        this.mTarifId = tarifId;
    }

I have initialized a variable named 'mTarifId'. And i need to use it in the code below.

if (!Objects.equals(mTarifId, null)) {
            query = mEntityManager.createQuery("FROM TarifListeCalendrierEntity WHERE tarifListeId=:pId")
                    .setParameter("pId", mTarifId);
            List<TarifListeCalendrierEntity> tarifListCalendrierList = query.getResultList();
            if (!tarifListCalendrierList.isEmpty()) {
                TarifListeCalendrierEntity tarifListCalendrier = tarifListCalendrierList.get(0);
                request.setAttribute("tarif_list_calendrier", tarifListCalendrier);
            }
        }

But in order to do so i need to convert it to an integer in the 'doGet'

        String tarifId = request.getParameter("tarifid");

        if (!Objects.equals(tarifId, null)) {
            setTarifId(Integer.valueOf(tarifId));
        }else {
            setTarifId(null);
        }

Please help identify what i'm doing wrong.

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
Fadil
  • 57
  • 2
  • 8
  • 3
    Please describe error you are getting from the code sample. – Maciej Lach Aug 18 '15 at 09:53
  • When debugging this line of code is skipped 'if (!Objects.equals(mTarifId, null)) {' meaning the value of mTarifId is still null. – Fadil Aug 18 '15 at 09:55
  • a friend told me to use something like this to get the value in mTarifId mTarifId = request.getParameter("tarifid") But when doing so i'm getting the error incompatible types: String cannot be converted to Integer – Fadil Aug 18 '15 at 09:59
  • please focus on the main issue you are facing, is it getting null request parameter? – Amit.rk3 Aug 18 '15 at 10:07
  • 1
    possible duplicate of [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java) – bhdrkn Aug 18 '15 at 10:29

4 Answers4

0

Every time else condition is executed that why it's null.

String tarifId = request.getParameter("tarifid");

        if (tarifId!=null) 

For Typecasting String to Integer.

Integer.parseInt(String);
Man Programmer
  • 5,300
  • 2
  • 21
  • 21
0

can you maybe use try catch if there is any NumberFormatException.

Try this:

String tarifId = request.getParameter("tarifid");
Integer number;
try{
    number = Integer.valueOf(tarifId);
} catch(NumberFormatException e){
    number = 0;
}
setTarifId(number)

Also, do you need your setTarifId() expecting an Integer, or int could be fine?

Kaixin
  • 103
  • 2
  • 8
0

Your actual issue is your request parameter. It may happen that your mTarifId is not even set in request parameter named tarifid.

You have not mentioned how do you set it. So check that code if this parameter is actually set or not.

SacJn
  • 777
  • 1
  • 6
  • 16
  • you are right...this is how it should be set i think mTarifId = Integer.valueOf(request.getParameter("tarifid")); – Fadil Aug 18 '15 at 10:38
  • It would throw **NUMBER FORMAT EXCEPTION** in case request,getParameter("tarifId") returns null or blank. – SacJn Aug 18 '15 at 10:57
0

This was what i needed:

mTarifId = Integer.valueOf(request.getParameter("tarifid"));
Fadil
  • 57
  • 2
  • 8