# Null Pointer (a curious case)

• April 8th, 2014, 10:24 AM
KAJLogic
Null Pointer (a curious case)
I get a null pointer at:

Code :

`n[Array.getLength(n)-1] = cur;`

not an arrayIndexOutOfBounds, but a null pointer? I didn't think you get get a null pointer on assignment, because after all aren't you supposed to be able to assign any object a null?

EDIT: n is a non primitive type. It is actually an array of objects.
• April 8th, 2014, 10:31 AM
KevinWorkman
Re: Null Pointer (a curious case)
Is n itself null?
• April 8th, 2014, 10:38 AM
KAJLogic
Re: Null Pointer (a curious case)
It shouldn't be:

Code :

``` n=utilities.increment(n); for(Tile cur:t.nodes){ if(cur!=null){ n[Array.getLength(n)-1] = cur;```

Code :

``` public static Tile[] increment(Tile[] k){ int i = 0; int len = k==null ? 1:Array.getLength(k)+1; Tile[] tmp = new Tile[len]; if(len==1)return tmp; for(Tile kk: k){ tmp[i] = kk; ++i; } return tmp; }```

I actually preform a null check on cur for operations that occur later.

n is a local variable, but was passed as a parameter. So to my knowledge as soon as I reassigned n it lost its connection to the passed array.

Post Script: I do understand your question though, because if I am being pragmatic I can concede that the system doesn't lie. So the only option here is that n is null? I guess my question for this thread is: could there be any other way I would get a null pointer there besides the fact that n is null?
• April 8th, 2014, 10:53 AM
aussiemcgr
Re: Null Pointer (a curious case)
Is the type for n explicitly an array, or is it just an object?
• April 8th, 2014, 11:04 AM
KAJLogic
Re: Null Pointer (a curious case)
What do you mean by explicitly an array?
• April 8th, 2014, 11:04 AM
KevinWorkman
Re: Null Pointer (a curious case)
Can you post an SSCCE that demonstrates exactly what you're doing?
• April 8th, 2014, 11:17 AM
KAJLogic
Re: Null Pointer (a curious case)
May I ask first, is there any other way I could get a null pointer there? Or, do you need more of the code to know for sure? In my head I keep getting this nagging urge to just stop myself and realize that an assignment cannot get a null pointer; the only way is if the array is null (). Is this not true? (assuming n is a non-primitive type)
• April 8th, 2014, 11:31 AM
Norm
Re: Null Pointer (a curious case)
Another way is that Array isn't the class but a local variable that is null.
• April 8th, 2014, 11:42 AM
KAJLogic
Re: Null Pointer (a curious case)
There is a level of incursion, but I believe that as soon as I reassign n it is no longer referencing the argument. Norm, do you mean a local variable that wasn't initialized?
• April 8th, 2014, 11:53 AM
Norm
Re: Null Pointer (a curious case)
Quote:

variable that wasn't initialized
That is the most common cause of a NPE.
• April 8th, 2014, 11:58 AM
aussiemcgr
Re: Null Pointer (a curious case)
Quote:

Originally Posted by KAJLogic
What do you mean by explicitly an array?

Like, is it declared as:
Tile[] n;
or it is declared as:
Object n;

I ask because if it is the first one (explicitly an array), you could get the length by:
n.length;
Array.getLength(n);

NOTE: notice no parentheses at the end of n.length. It is a variable reference, not a method call.
• April 8th, 2014, 12:04 PM
KAJLogic
Re: Null Pointer (a curious case)
Yes aussie your right I guess I never really reviewed the array API very much. In any case I appreciate your help. I will delve into this with your suggestions.
• April 8th, 2014, 12:09 PM
aussiemcgr
Re: Null Pointer (a curious case)
I suggested this because if you use n.length; and still get the null pointer, it means two things:
1. the problem is independent from the Array class
2. n is null
• April 8th, 2014, 12:32 PM
KAJLogic
Re: Null Pointer (a curious case)
What are you suggesting that the Array class is inaccurate or flawed?
• April 8th, 2014, 12:44 PM
Norm
Re: Null Pointer (a curious case)
Without seeing a complete (small) program that compiles, executes and shows the problem, most of this discussion is pretty useless.
• April 8th, 2014, 12:45 PM
KevinWorkman
Re: Null Pointer (a curious case)
Quote:

Originally Posted by KAJLogic
What are you suggesting that the Array class is inaccurate or flawed?

No.

Quote:

Originally Posted by Norm
Without seeing a complete (small) program that compiles, executes and shows the problem, most of this discussion is pretty useless.

Yes.
• April 8th, 2014, 12:47 PM
aussiemcgr
Re: Null Pointer (a curious case)
No, but perhaps that the getLength() method does something which, due to the way your array is set up or something, unexpectedly causes a null pointer to be thrown. The Array.getLength() method is a native method, so I don't have a clue what it actually does.
• April 8th, 2014, 12:51 PM
KAJLogic
Re: Null Pointer (a curious case)
Aussie using the length variable produces the same error.

Code :

```      }```
• April 8th, 2014, 01:09 PM
aussiemcgr
Re: Null Pointer (a curious case)
Well that means that n is null.
n is set by the utilities.increment() method. Unfortunately, it doesn't appear that the increment() method can return null. Are you 100% sure the line you are getting the null pointer at is correct?
• April 8th, 2014, 01:37 PM
KAJLogic
Re: Null Pointer (a curious case)
Yeah at the top of the exception it cites that line then it cites a stream larger than my command prompt buffers capabilities here:

Code :

` nO = t.getOptimalNodes(n,utilities.invert(a));`
• April 8th, 2014, 02:35 PM
aussiemcgr
Re: Null Pointer (a curious case)
Can you provide the full stack trace?
• April 9th, 2014, 08:40 AM
KevinWorkman
Re: Null Pointer (a curious case)
This will be trivial to debug as soon as you provide the SSCCE. Before that, this is all just guesswork.