Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Null Reference  RSS feed

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I don't understand the logic of above program:
I will tell what i have understood:

1. Creating the ref var Q07 and it is not assigned to any object.
2. Invoking the throwDice() by Q07 ref var.
3. In throwDice(), we generating a random number.
Math.random() = will generate number between 0 and 1
Math.random() * 6 = will have number between 0 and 6
Doubt:1+(int)(Math.random()*6) = will have number between 0 and 7
4. So, we have the value of i between 0 and 6 or 0 and 7.
5. I don't understand the line 2

But the options given for this program:
a. Compilation error
b. The code compiles but throws a NullPointerException at line 1 when run.
c. The code compiles but throws a NullPointerException at line 2 when run.
d. The code compiles fine.
e. The program will always print result: x,y, where x and y are two numbers between 1 and 6.

Sorry, the option e is forgot to add. It was added later.
[ August 12, 2005: Message edited by: Niyas Ahmed Sheikh ]
 
Jean-Sebastien Abella
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a int be null?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niyas Ahmed Sheikh:


I don't understand the logic of above program:
I will tell what i have understood:

1. Creating the ref var Q07 and it is not assigned to any object.
2. Invoking the throwDice() by Q07 ref var.
3. In throwDice(), we generating a random number.
Math.random() = will generate number between 0 and 1
Math.random() * 6 = will have number between 0 and 6
Doubt:1+(int)(Math.random()*6) = will have number between 0 and 7
4. So, we have the value of i between 0 and 6 or 0 and 7.
5. I don't understand the line 2

But the options given for this program:
a. Compilation error
b. The code compiles but throws a NullPointerException at line 1 when run.
c. The code compiles but throws a NullPointerException at line 2 when run.
d. The code compiles fine.
e. The program will always print result: x,y, where x and y are two numbers between 1 and 6.

Sorry, the option e is forgot to add. It was added later.

[ August 12, 2005: Message edited by: Niyas Ahmed Sheikh ]


2. No. throwDice is a static method so you are invoking it using the class name
3. Math.randow() returns a double between 0.0 and 0.9999...
Math.random() * 6 gives you a double between 0.0 and 5.9999....
(int ) (Math.random() * 6) casts it to an int between 0 and 5
1 + (int ) (Math.random() * 6) adds one to give a number between 1 and 6
5 Think of it as

Answer is c because getDice() returns null and so if you refer to my answer to 5, tmp will be null, so tmp.throwDice() will throw a NPE.

J
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to answer after a casual reading. These things are often trick questions so I may be missing something.

Let's start with these two lines:

The first line declares a variable named "q" that can point to objects of type Q7, but for now says it points to no object at all. We never use "q" again so it does no harm or good.

The second line calls a static method on the class Q7. There is still no object instance of Q7, but that's ok because we're only working with the Class.


This line calls the throwDice() method on some object that we get from getDice(). We saw before that we don't need an object instance of Q7 to call throwDice() but it is acceptable to call throwDice() on a real instance. (Your compiler or IDE may give you a warning, but it's not serious.)

getDice() promises to return a Q7 object, so the compiler is satisfied that the method will be there and it should compile. But getDice() lies. It really returns a null. We will try to call the throwDice() method on a null, and should expect a null pointer exception.

Have you tried it? Let me know if I was close!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:

getDice() promises to return a Q7 object, so the compiler is satisfied that the method will be there and it should compile. But getDice() lies. It really returns a null.


<slightly pedantic>
getDice() returns a reference to a Q7 object. Unfortunately, the reference isn't yet 'pointing' to a Q7 object, so an NPE is thrown.
</slightly pedantic>

J
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, I am getting the answer:
Result: 5,5

2nd time running
Result: 1,1

3rd time running
Result: 3,2
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting.
It appears that the compiler sees that the throwDice() method is static and so makes the call to it without dereferencing the object returned from getDice().
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 2 is


Since throwDice() is static, the only thing that matters is the type of the reference, namely the return type of getDice(). Even if this is null. Don't think of it as
Think of it as

I agree with Stan's choice of graemlin, and would add one of my own:

-- Phil
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I knew it was a trick. That's pretty cool. Actually it rather violates the "principle of least astonishment" enough I'd call it maybe a bad idea from the language designers, but they were smarter than me a great percentage of the time so I'll let it go.
[ August 12, 2005: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!