-2

I had a previous question similar to my current problem, and that was when I retrieved null values from my String arrays, now my problem is that I think I'm also getting null values from my integar arrays, but I do not know how to handle such an exception, for example:

for int(i=0;i<n;i++)
    {
        if(taskArr[i] != null){
        taskArr[i] = taskArray[i].getText().toString();
        taskId[i] = taskArray[i].getId();
    }
    }

the if(taskArr[i] != null) solved my problem for getting null values from my String Array, but it does not seem to working on my integar arrays. taskArr[] is my string array, taskId[] is my integar array and taskArray[] is an edittext array

Anthony Wu
  • 41
  • 5
  • An integer array cannot contains nulls (unless you used Integer[] instead of integer[]). If these are integer arrays, how does this work? `taskArray[i].getText()` – Simon Mar 08 '13 at 13:00
  • sorry I forgot to mention that taskArray[] is an edittext array – Anthony Wu Mar 08 '13 at 13:02
  • Don't only mention this in the comments, edit the question and add it. – m0skit0 Mar 08 '13 at 14:55

6 Answers6

2

The conditional if statement only applies to the first line you need to use brackets. In general its a good practice to use brackets with your if statements, it makes things more intuitive and saves you from encountering errors like this. You will also want to check that getText() is not null.

for int(i=0;i<n;i++)
    {
        if(taskArr[i] != null && taskArr[i].getText() != null)
        {
           taskArr[i] = taskArray[i].getText().toString();
           taskId[i] = taskArray[i].getId();
        }
    }

On a side note you mentioned that your array contained Integer objects. I'm not seeing Integer.getText() or Integer.getId() on the API documentation for Integer is another type being stored in the array?

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

You commented:

i'm still getting a syntax error for that ...

This is not valid Java:

for int(i=0;i<n;i++) {
}

It should be:

for (int i = 0; i < n; i++) {
}

As for the rest, if you told us exactly which line the NPE was being thrown on, we could probably tell you what it is causing it. You seem to be implying that it is this line:

    taskId[i] = taskArray[i].getId();

If that is the case, and if taskId and taskArray have types int[] and EditText[] then there are 3 possibilities:

- `taskId` is `null`,
- `taskArray` is `null`, or 
- `taskArray[i]` is `null`.

If we assume that the previous line:

    taskArr[i] = taskArray[i].getText().toString();

did not throw an NPE, that eliminates two possibilities, leaving taskId as the culprit.


The other comment I want to make is that all of this testing for nulls to avoid NPE's is most likely the wrong approach. You should be finding out where these null values are coming from and changing the logic of the rest of the code so that it doesn't happen.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You should have both the lines enclosed in if condition:

if(taskArr[i] != null)
{
    taskArr[i] = taskArray[i].getText().toString();
    taskId[i] = taskArray[i].getId();
}
Azodious
  • 13,752
  • 1
  • 36
  • 71
0

This is because an int cannot be null, but an Integer can. I assume your taskArray.getId() returns an Integer, but taskId is int[]. Thus you're casting directly to int, which will fail if the Integer is null. This line

taskId[i] = taskArray[i].getId();

is equivalent with

taskId[i] = taskArray[i].getId().intValue();

What happens here if taskArray[i].getId() is null?

You need to get an Integer, check if it's null, then assign to an int if you want if it's not null. For example:

Integer id = taskArray[i].getId();
if (id != null) {
    taskId[i] = id;
}
m0skit0
  • 25,268
  • 11
  • 79
  • 127
0

Please use curly brackets ({}) in your if clause. If you don't, just the line under the condition is recognized:

if(taskArr[i] != null) {
    taskArr[i] = taskArray[i].getText().toString();
    taskId[i] = taskArray[i].getId();
}
webmonkey
  • 1,083
  • 1
  • 15
  • 33
0

Well, you check if taskArr is empty, then try to extract a value from taskId. It is possible taskId is null, or at least the value from that. You need to check that likewise.

Try this:

for int(i=0;i<n;i++)
{
    if(taskArray[i].getText() != null) //Check if Text from taskArray[i] is null
        taskArr[i] = taskArray[i].getText().toString();
    if(taskArray[i].getId != null) //Check if ID from taskArray[i] is null
        taskId[i] = taskArray[i].getId();
}
Joetjah
  • 6,292
  • 8
  • 55
  • 90