将String转换为int。如果String为null,则将int设置为0

将String转换为int。如果String为null,则将int设置为0

问题描述:

我有一个在 sqlite 中保存Android数据的功能,但我必须将 String 数据转换为整数。

I have a function which saves Android data in sqlite but I have to convert the String data to an Integer.

每当字符串 null 我想保存为 0

以下是我的代码每当值 null时失败

The following is my code which fails whenever the value is null

 int block_id = Integer.parseInt(jsonarray.getJSONObject(i).getString("block_id"));

上面的 block_id 转换为整数。

这是我决定要做但仍无法将字符串值转换为 0 每当 null

This is what i have decided to do but still it fails to convert the string value to 0 whenever its null.

int block_id = Converttoint(jsonarray.getJSONObject(i).getString("block_id"));

然后函数 convertToInt

 public static Integer convertToInt(String str) {
    int n=0;
  if(str != null) {
      n = Integer.parseInt(str);
  }
    return n;
}

我应该如何改变它,使其有效?

How should I change it, to make it work?

使用try-catch的inbuild构造而不是编写自己的函数。你的问题是, jsonarray jsonarray.getJSONObject(i)或者值本身是 null 并在null引用上调用方法。请尝试以下方法:

Instead of writing your own function use the inbuild construction of try-catch. Your problem is, that jsonarray or jsonarray.getJSONObject(i) or the value itself is a null and you call a method on null reference. Try the following:

int block_id = 0;        //this set's the block_id to 0 as a default.
try {
    block_id =  Integer.parseInt(jsonarray.getJSONObject(i).getString("block_id"));    //this will set block_id to the String value, but if it's not convertable, will leave it 0.
} catch (Exception e) {};

在Java中,异常用于标记意外情况。例如,将非数字 String 解析为数字( NumberFormatException )或在 null reference( NullPointerException )。您可以通过多种方式捕获它们。

In Java Exceptions are used for marking unexpected situations. For example parsing non-numeric String to a number (NumberFormatException) or calling a method on a null reference (NullPointerException). You can catch them in many ways.

try{
    //some code
} catch (NumberFormatException e1) {
    e.printStackTrace()     //very important - handles the Exception but prints the information!
} catch (NullPointerException e2) {
    e.printStackTrace();
}

或使用这一事实,他们都扩展例外

or using the fact, that they all extend Exception:

try {
    //somecode
} catch (Exception e) {
    e.printStackTrace;
};

或自Java 7以来:

or since Java 7:

try {
    //somecode
} catch (NullPointerException | NumberFormatException e) {
    e.printStackTrace;
};

注意

我相信,你会仔细阅读答案,请记住,在*上我们需要最小,完整和可验证示例,其中包括您的异常的StackTrace。在你的情况下,它可能从以下开始:

As I believe, that you'll read the answer carefully, please have in mind, that on * we require the Minimal, Complete, and Verifiable example which include the StackTrace of your exception. In your case it probably starts with the following:

Exception in thread "main" java.lang.NullPointerException

然后,调试就容易多了。没有它,它只是在猜测。

Then, debugging is much easier. Without it, it's just guessing.

编辑 根据接受的答案

接受的答案很好,并且工作时间很长,因为使用密钥存储的值: block_id 将是数字。如果它不是数字,你的应用程序将崩溃。

The accepted answer is good and will work as long, as the value stored with key: block_id will be numeric. In case it's not numeric, your application will crash.

而不是:

JSONObject jObj = jsonarray.getJSONObject(i);
int block_id = jObj.has("block_id") ? jObj.getInt("block_id") : 0;

应该使用:

int block_id;
try{
    JSONObject jObj = jsonarray.getJSONObject(i);
    block_id = jObj.has("block_id") ? jObj.getInt("block_id") : 0;
} catch (JSONException | NullPointerException e) {
    e.printStackTrace();
}