About Question enthuware.ocajp.i.v7.2.1092 :
Moderator: admin
-
- Posts: 15
- Joined: Fri Jul 12, 2013 12:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Problem arise when it's invoked the setIt method.
a.link points to the same object b points.
b.link points to the object returned by method setIt.
setIt takes two objects, (referenced by a and b) BUT sets a.link (x.link) to b.link(y.link). But b.link points to nothing!
So, a.link now also points to nothing. So nullpointerexception.
nice question!
a.link points to the same object b points.
b.link points to the object returned by method setIt.
setIt takes two objects, (referenced by a and b) BUT sets a.link (x.link) to b.link(y.link). But b.link points to nothing!
So, a.link now also points to nothing. So nullpointerexception.
nice question!
-
- Posts: 4
- Joined: Thu Jan 09, 2014 3:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
@baxhurli The method setIt(a,b) does get executed.
Code: Select all
public static Holder setIt(final Holder x, final Holder y){
x.link = y.link; //makes a.link to null so now a is an object with value=5, link = null
return x; //returns a. This is important because it is being assigned to b. So b.link.value gives a.value, i.e. 5 because of this return statement.
}
-
- Posts: 25
- Joined: Thu Mar 27, 2014 11:35 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
The java content of this question is simply "What happens if you use a null pointer." Null "reference" I mean.
It's sooo frustrating to miss such a simple question because it's buried under a half-mile of knots. The worst is working through nested loops with nonsense breaks and continues with pencil and paper to figure out what it will print.
I suppose they're trying to test something more than java knowledge, and I know this example is typical of the real tests, so it speaks well of the example. I feel almost like it's a twisted fraternity initiation.... Oh well, it's all in fun I guess. - Bill
It's sooo frustrating to miss such a simple question because it's buried under a half-mile of knots. The worst is working through nested loops with nonsense breaks and continues with pencil and paper to figure out what it will print.
I suppose they're trying to test something more than java knowledge, and I know this example is typical of the real tests, so it speaks well of the example. I feel almost like it's a twisted fraternity initiation.... Oh well, it's all in fun I guess. - Bill
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Code: Select all
class Holder{
int value = 1;
Holder link;
public Holder(int val){ this.value = val; }
public static void main(String[] args){
final Holder a = new Holder(5);
Holder b = new Holder(10);
a.link = b;
b.link = setIt(a, b); //(1)
System.out.println(a.link.value+" "+b.link.value);
}
public static Holder setIt(final Holder x, final Holder y){
x.link = y.link; //(2)
return x; //(3)
}
}
1. At //(1) setIt() is called with 'a' and 'b'.
2. At //(2) x.link which is pointing the same location as a.link. a.link points to the Object reference by 'b'. The object reference by 'x' which is the same object referenced by 'a'.
Therefore,
3. At //(1) b.link is point to the object reference by 'a'.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
When you create b = new Holder(); b.link is null. It is not being assigned a value anywhere. So it is null.
I have no idea what you are saying at 2. At //2, when you execute x.link = y.link; you assign y.link to x.link. y.link is null (because b.link is null as explained above), so x.link becomes null. x is nothing but a reference to the same object referred to by a. so a.link becomes null. Hence, NPE is thrown when you do a.link.value.
HTH,
Paul.
I have no idea what you are saying at 2. At //2, when you execute x.link = y.link; you assign y.link to x.link. y.link is null (because b.link is null as explained above), so x.link becomes null. x is nothing but a reference to the same object referred to by a. so a.link becomes null. Hence, NPE is thrown when you do a.link.value.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
at //3 we are returning x which is the object referenced by a. When the method setIt returns, b.link is pointing a. Where am I going wrong?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
b.link is null before the invocation of setIt. b.link is null even when you are inside the setIt method. That is why null is assigned to a.link.
b.link becomes non-null (pointing to a) AFTER setIt returns i.e. AFTER //3 (not at //3).
You might want to run it in a debugger and see the values step by step.
b.link becomes non-null (pointing to a) AFTER setIt returns i.e. AFTER //3 (not at //3).
You might want to run it in a debugger and see the values step by step.
If you like our products and services, please help us by posting your review here.
-
- Posts: 31
- Joined: Thu Feb 19, 2015 8:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Tell me please if I understand it right
When creates b = new Holder(), field b.link = null
than fetching to method setIt() reference b and y are both reference to
same object, which field link is null
By assigning y.link to x.link implicitly a.link became null
and when the program tries to get a field a.link.value of a.link we get NullPointerException
Thanks,
Sergey
When creates b = new Holder(), field b.link = null
than fetching to method setIt() reference b and y are both reference to
same object, which field link is null
By assigning y.link to x.link implicitly a.link became null
and when the program tries to get a field a.link.value of a.link we get NullPointerException
Thanks,
Sergey
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
I got the question right, so think I understand the main points, but....
I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);
does that make sense?
thanks,
Nick
I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);
does that make sense?
thanks,
Nick
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
You need to understand that accessing a null reference always causes a NullPointerException to be generated. Always. No exceptions.NickWoodward wrote:I got the question right, so think I understand the main points, but....
I can't see why trying to access a.link.value would through an NPE. Yes it's null, but
String s = null; System.out.println(s); doesn't throw that exception.
I thought it might be applying the + operator, but it also occurs with System.out.println(a.link.value);
does that make sense?
thanks,
Nick
Basically, "access" means, the use of the dot operator. someReferenceDOTsomeMember. If you try a.link.value and if a is null or a.link is null, it will generate a NPE because you are trying to access a null.
Now, regarding why System.out.println(s); does not throw NPE, you have to ask yourself, are you accessing null here? The fact that it does not throw a NPE itself tells you that it is not being accessed. Then how does it print "null"? The only way that can happen is if the code inside the print method checks whether the reference is null before trying to access it. Something like this: if(ref == null) print("null") else print(ref.toString());
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 29
- Joined: Mon Mar 30, 2015 6:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
ah of course, i'm confusing access with passing the variable as an argument.
thanks, great answer!
thanks, great answer!
-
- Posts: 3
- Joined: Wed Nov 16, 2016 10:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
Holder link;
This 'link' is declared, but ininitialized. So (if I am correct): If an Object reference has been declared but not instantiated, its value is null.
So this is why b.link; is 'null' (and a.link if it was not assigned to the Object reference 'b').
Took me a while...
This 'link' is declared, but ininitialized. So (if I am correct): If an Object reference has been declared but not instantiated, its value is null.
So this is why b.link; is 'null' (and a.link if it was not assigned to the Object reference 'b').
Took me a while...
-
- Posts: 4
- Joined: Sun Jan 08, 2017 12:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
I have read the previous comments and still don't understand a.link = b. Does this mean a.link = b.link?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
No, a.link = b does not mean a.link = b.link. It means a.link points to the same object to which b is pointing.bniky1 wrote:I have read the previous comments and still don't understand a.link = b. Does this mean a.link = b.link?
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
"x.link = y.link, x.link becomes null because y.link is null so a.link.value throws NullPointerException."
Does it mean you do: null = null; will throw a NPE ?
Does it mean you do: null = null; will throw a NPE ?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
No, null = null is an invalid statement. It will not even compile.
NullPointerException is thrown whenever you try to access an instance field or call an instance method using a reference that is pointing to null.
NullPointerException is thrown whenever you try to access an instance field or call an instance method using a reference that is pointing to null.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Fri Sep 11, 2020 4:49 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1092 :
I think having a mental picture of a linked list actually helps to understand and follow the code in this question even for someone who has not had to work with linked lists. Thank you for mentioning it!admin wrote: ↑Wed Aug 01, 2012 11:02 pmThe Holder class is actually a very basic (and well known) building block of a linked list. It is usually named "Node" and not Holder. Please google linked list to learn more.
Though not important for this exam, understanding how linked lists work is something that a programmer must know. You mentioned that you've been programming for years and I am surprised to know that you didn't recognize it.
HTH,
Paul.
-
- Posts: 9
- Joined: Mon Jun 28, 2021 4:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1092 :
a.link = b does not mean a.link = b.link. It means a.link points to the same object to which b is pointing.
I imagined a and b - these are some country, link and value there are some lakes. a has own lakes and b has own lakes. How lakes of a can be assigned to another country but not to another lake? How is a.link=b works in reality? When I have saw it my brain was broken. How it happened in programming area? Please give me to understand this.
I imagined a and b - these are some country, link and value there are some lakes. a has own lakes and b has own lakes. How lakes of a can be assigned to another country but not to another lake? How is a.link=b works in reality? When I have saw it my brain was broken. How it happened in programming area? Please give me to understand this.
Who is online
Users browsing this forum: No registered users and 79 guests