-1

I tried different things for solution but as I failed, I need to know what's wrong with my code and what it should be.

Here is my logcat error

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: freemig.freemigmessenger, PID: 31699
                                                                      java.lang.NumberFormatException: s == null
                                                                          at java.lang.Integer.parseInt(Integer.java:570)
                                                                          at java.lang.Integer.parseInt(Integer.java:643)

And it happens on a button click event which is

    private class SentMessageHolder extends RecyclerView.ViewHolder {
    TextView messageText, timeText;
    ImageButton iB;
    MsgDltAPIService msgDltAPIService;
    String rec_id;
    String content_id;
    int[] ids = new int[100];

    SentMessageHolder(final View itemView) {
        super(itemView);

        messageText = itemView.findViewById(R.id.text_message_body);
        timeText = itemView.findViewById(R.id.text_message_time);
        iB = itemView.findViewById(R.id.sentMessageTextDelete);
        msgDltAPIService = RestClient.getClient().create(MsgDltAPIService.class);

        iB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userAuthenticationKey = new UserAuthenticationKey(mContext.getApplicationContext());
                sharedPreferences =  mContext.getApplicationContext().getSharedPreferences("user authentication", MODE_PRIVATE);
                editor = sharedPreferences.edit();
                ids[0] = Integer.parseInt(content_id);
                final MsgDltRequest msgDltRequest = new MsgDltRequest(
                        ids,
                        rec_id);
                Call<MsgDltResponse> call =
                        msgDltAPIService.msgDlt(userAuthenticationKey.getUserTokenKey(),
                                msgDltRequest);
                call.enqueue(new Callback<MsgDltResponse>() {
                    @Override
                    public void onResponse(Call<MsgDltResponse> call, Response<MsgDltResponse> response) {

                        Toast.makeText(mContext.getApplicationContext(), "" + response.body().getData(),
                                Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onFailure(Call<MsgDltResponse> call, Throwable t) {
                        Toast.makeText(mContext.getApplicationContext(), "please try again",
                                Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

    void bind(Datum2 message) {
        messageText.setText(message.getMsg());

        // Format the stored timestamp into a readable String using method.
        timeText.setText(message.getCreatedAt().getFormatTime());
        content_id.valueOf(message.getContentId().toString()) ;
        if (! message.getOriginatorId().equals(userID)){
            rec_id.valueOf(message.getOriginatorId());
        }
        else {
            rec_id = null;
        }
                        }

}

It gives me error on this line

ids[0] = Integer.parseInt(content_id);

I tried several things as I said previously , but what should be my code? My full class is here. One more thing I'm working on a Adapter Class.

user7310707
  • 39
  • 1
  • 1
  • 5
  • Possible duplicate of [What is a NumberFormatException and how can I fix it?](https://stackoverflow.com/questions/39849984/what-is-a-numberformatexception-and-how-can-i-fix-it) – xenteros Aug 21 '17 at 12:51
  • You can check which value are contained by "content_id". After that you decide how you format. – Aunog Aug 20 '17 at 06:30

4 Answers4

4

Log says that it's a NumberFormatException and the String you're trying to convert to int is null. Integer.parseInt(content_id) works only if content_id is not null as well it's a valid integer.

So in your situation, you can add a check to see if the string is null. If the string is not null and still gives a NumberFormatException, then the string is not a valid integer and can be handled with a try catch statement.

if (content_id != null) {
    try {
        ids[0] = Integer.parseInt(content_id);
    } catch(NumberFormatException e) {
        // Deal with the situation like
        ids[0] = 0;
    }
}
capt.swag
  • 10,335
  • 2
  • 41
  • 41
3

You can use TextUtils utility method like this,

if (!TextUtils.isEmpty(content_id) && TextUtils.isDigitsOnly(content_id)) {
    ids[0] = Integer.parseInt(content_id);
} else {
    ids[0] = 0;
}

and while setting content_id you can set like this, content_id = message.getContentId().toString(); instead of content_id.valueOf(message.getContentId().toString()) ;

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
2

Put the statement in try and catch block to handle the exception.

try{
 ids[0] = Integer.parseInt(content_id); 
}catch(NumberFormatException ex){ // handle your exception
   ...
}

or simply integer matching-

String input=...;
String pattern ="-?\\d+";
if(input.matches("-?\\d+")){ // any positive or negative integer or not!
... 
}
0
String str = arraylist.get(position); //check your value from Arraylist
if (!TextUtils.isEmpty(str) && TextUtils.isDigitsOnly(str)) {
    yourTextview.setText(str);
} else {
    yourTextview.setText("Your default value");
}
sumit baflipara
  • 209
  • 3
  • 4