Search code examples
javaandroidadapternumberformatexception

How to solve "java.lang.NumberFormatException: s == null at java.lang.Integer.parseInt"


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.


Solution

  • 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()) ;