如何解决;java.lang.NumberFormatException:s==null位于java.lang.Integer.parseInt";

如何解决;java.lang.NumberFormatException:s==null位于java.lang.Integer.parseInt";,java,android,adapter,numberformatexception,Java,Android,Adapter,Numberformatexception,我尝试了不同的方法来解决问题,但由于失败了,我需要知道我的代码出了什么问题,应该是什么 这是我的日志错误 E/AndroidRuntime: FATAL EXCEPTION: main Process: freemig.freemigmessenger, PID: 31699

我尝试了不同的方法来解决问题,但由于失败了,我需要知道我的代码出了什么问题,应该是什么

这是我的日志错误

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)
它发生在一个按钮点击事件上

    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;
        }
                        }

}

如前所述,我尝试了几种方法,但我的代码应该是什么?。还有一件事我正在处理一个适配器类。

您可以像这样使用
TextUtils
实用方法

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

在设置content_id时,您可以这样设置,
content_id=message.getContentId().toString()
而不是
content\u id.valueOf(message.getContentId().toString())

将语句放入
try
catch
块以处理异常

try{
 ids[0] = Integer.parseInt(content_id); 
}catch(NumberFormatException ex){ // handle your exception
   ...
}
或者简单的整数匹配-

String input=...;
String pattern ="-?\\d+";
if(input.matches("-?\\d+")){ // any positive or negative integer or not!
... 
}

日志显示这是一个NumberFormatException,您试图转换为int的字符串为null。parseInt(content_id)仅在content_id不为null且为有效整数时才起作用

因此,在您的情况下,您可以添加一个检查来查看字符串是否为null。如果字符串不为null并且仍然给出NumberFormatException,则该字符串不是有效的整数,可以使用try-catch语句处理

if (content_id != null) {
    try {
        ids[0] = Integer.parseInt(content_id);
    } catch(NumberFormatException e) {
        // Deal with the situation like
        ids[0] = 0;
    }
}

非常感谢你!它非常有效。我的适配器中有另一个问题,我可以在这里与您共享吗?@user7310707:为此提出一个新问题,而不是将太多的工作放在一个助手上
:-)
您可以检查“content\u id”包含哪些值。之后,您决定如何格式化。可能的副本
if (content_id != null) {
    try {
        ids[0] = Integer.parseInt(content_id);
    } catch(NumberFormatException e) {
        // Deal with the situation like
        ids[0] = 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");
}