"this" is a really difficult thing to find help on...

notfred

Lifer
Feb 12, 2001
38,241
4
0
So I'm writing a Java program that's using the "this" pointer. It's not working properly and I'm trying to look up documentation on "this" to make sure I'm not doing something wrong. Have you ever tried to search for "this"? It's really hard to find relevant results.
 

lukatmyshu

Senior member
Aug 22, 2001
483
1
0
It might be very hard to 'search' for this ... so why don't you post your error and us humans can take a look at it? :)
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81
Generally you use "this" whenever you want an object to call its own methods that require a reference to the original object.

but a little more info on your problem would make it easier to help you.
 

Scarpozzi

Lifer
Jun 13, 2000
26,391
1,780
126
I know this may seem obvious, but go to your local bookstore too and see what java books they have there....take a pen and paper with you and see if you can teach yourself at Barnes & Noble... Of course, you could do the right thing and just buy the book if it has a good enough section on that call. ;)
 

Jzero

Lifer
Oct 10, 1999
18,834
1
0
In Java you rarely have to use "this" because it's generally implied to use "this" object unless you specify otherwise.
What exactly are you working on?
 

FrogDog

Diamond Member
Jan 12, 2000
4,761
0
0
The only time I've ever used the 'this' command is to call one constructor from inside another. I would imagine it could be used in other situations though. How are you using it, notfred?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I have two classes LeafNode and SearchNode. Both of those classes are based on the abstract Node class.

Inside the Node class I have the line:
public Node parent;

Inside the SearchNode class I have the line:

subNodes[0].parent = this; (where subNodes is an array of LeafNodes)

inside the LeafNode class I have the line:

parent.insert();

after the SubNodes[0].parent = this; line, I run the parent.insert() line from the subNodes[0] object, and get a null pointer exception.
 

Rainsford

Lifer
Apr 25, 2001
17,515
0
0
Ok, I've just gone through this in my C++ class, so assuming it's the same in Java (it should be), it works like this (ahem, pardon the pun ;))

this is a pointer to the memory location occupied by the object that called the function you are currently running. In C++, if you actually want to access the data that the pointer "this" is pointing to, you have to use *this. I'm not sure I get what your program is trying to do, but hopefully this will give you an idea.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Rainsford
Ok, I've just gone through this in my C++ class, so assuming it's the same in Java (it should be), it works like this (ahem, pardon the pun ;))

this is a pointer to the memory location occupied by the object that called the function you are currently running. In C++, if you actually want to access the data that the pointer "this" is pointing to, you have to use *this. I'm not sure I get what your program is trying to do, but hopefully this will give you an idea.

Dereferencing pointers in java is different, you don't need the *.
 

Rainsford

Lifer
Apr 25, 2001
17,515
0
0
Originally posted by: notfred
Originally posted by: Rainsford
Ok, I've just gone through this in my C++ class, so assuming it's the same in Java (it should be), it works like this (ahem, pardon the pun ;))

this is a pointer to the memory location occupied by the object that called the function you are currently running. In C++, if you actually want to access the data that the pointer "this" is pointing to, you have to use *this. I'm not sure I get what your program is trying to do, but hopefully this will give you an idea.

Dereferencing pointers in java is different, you don't need the *.

Ah, well sorry, guess I won't be too much help :(
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
Originally posted by: notfred
I have two classes LeafNode and SearchNode. Both of those classes are based on the abstract Node class.

Inside the Node class I have the line:
public Node parent;

Inside the SearchNode class I have the line:

subNodes[0].parent = this; (where subNodes is an array of LeafNodes)

inside the LeafNode class I have the line:

parent.insert();

after the SubNodes[0].parent = this; line, I run the parent.insert() line from the subNodes[0] object, and get a null pointer exception.
Looks to me like you are using it properly. Is the null pointer exception happening on the parent.insert () call itself? Have you tried printing the value of parent prior to the call to see if it is indeed null (Java has a null checking function, right? don't program in it much myself)? Haven't tried to mock something up, but it appears to be in order. The only thing I could think of is if the "parent" object went out of scope and was cleaned off the heap, but I wouldn't think that would generate a null pointer exception.
 

lukatmyshu

Senior member
Aug 22, 2001
483
1
0
Did you actually make an object to put into subNodes[0]? The line subNodes[0].parent = this runs properly right? That would imply that there is an object .. subNodes[0] is in scope in all places? The syntax looks okay so it might be a silly programming error somewhere else ... I think you're using this correctly though ... i often use this when I am making multi-windowed application so that I can tell a child who it's parent is (i.e. in the constructor).
 

manly

Lifer
Jan 25, 2000
12,839
3,614
136
Originally posted by: notfred
I have two classes LeafNode and SearchNode. Both of those classes are based on the abstract Node class.

Inside the Node class I have the line:
public Node parent;

Inside the SearchNode class I have the line:

subNodes[0].parent = this; (where subNodes is an array of LeafNodes)

inside the LeafNode class I have the line:

parent.insert();

after the SubNodes[0].parent = this; line, I run the parent.insert() line from the subNodes[0] object, and get a null pointer exception.
You should drop into a symbolic debugger and trace your code.

It's not obvious from your problem description, but it sure sounds like LeafNode.parent.insert() is unexpectedly getting called *before* parent is assigned, so you just have to figure out why.

Where is this work being done? In constructors or public methods?

I don't remember what the official behavior is, but it could be dicey if within the SearchNode constructor, you are assigning the this reference to a member field's (subNodes[0]) member field (parent), then immediately calling a method on the reference. Constructors generally should be limited to setting member fields, without calling any methods doing any work since the object being constructed has a transitional state.

As an aside, public fields are generally frowned upon but that's an entirely different issue. Public fields are *occasionally* acceptable.