stumped by NullPointerException errors

D

David

Anyone up to a challenge? I am using pointers in Java so that one
object can update another. I have done this in the program I am
working on without a problem. Now, I am using it again and
encountering a NullPointerException error. It does not make sense.
Here's the logic I've been using:

Class 1 instantiates Class 2 using a constructor which allows Class 1
to pass a pointer of itself to Class 2

Class 2 saves the address of Class 1's pointer within a private member
variable "pc"

Class 1 has protected member function "memberfunction"

Class 2 is able to access Class 1 through the private member variable
"pc"

In code:

public class1 {
class1() {
class2(this);
}

protected void memberfunction() {
// does something usefull
}
}

public class2 {
class1 pc;
class2(class1 callback) {
pc=callback;
}

private anotherfunction() {
pc.memberfunction()
}
}

Why, when I wrote my program (using threads which can access their
parent's member functions) I had no problem but now I am getting a
null pointer error? Can anyone think of reasons I would receive this
error using the scenerio above? This is a broad question - hopefully
I've given enough information for some answers!

David
 
T

Tony Morris

Why, when I wrote my program (using threads which can access their
parent's member functions) I had no problem but now I am getting a
null pointer error? Can anyone think of reasons I would receive this
error using the scenerio above? This is a broad question - hopefully
I've given enough information for some answers!

David

You are dereferencing a reference to null at the line that the VM tells you
in the stack trace.
Find all the times you dereference on that line and determine which
reference is null that you dereference (with some debug statements perhaps).
Then ask "why is it null ?"
This brings you further from the problem and closer to the solution, because
you can make all these assumptions when then VM gives you verbose
information about what it is complaining about.

http://www.xdweb.net/~dibblego/java/faq/answers.html#q7

Good luck.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
R

Roedy Green

class1() {
class2(this);
}

that does not make any sense.

First capitalise your classes and lower case your methods. It is hard
enough trying to figure out erroneous code.

Do you mean

Class1()
{
x = new Class2();
}

you can't use a constructor without new.

You can say
this( ) to use another constructor of you same class in a constructor.
on super( )

Try compiling things. The compiler will patiently knock any foolish
notions out of your head about what Java should be able to do but
cannot.
 
T

Thomas Weidenfeller

David said:
Anyone up to a challenge? I am using pointers in Java

No, you are using references, like anyone else.
so that one
object can update another. I have done this in the program I am
working on without a problem. Now, I am using it again and
encountering a NullPointerException error.

(a) Sun managed to misname the exception. You tried to access an object
via a reference, but that reference didn't reference an object.

(b) Read the exception error message. See these numbers and file names
in the message? They point to lines in your source code. Check this part
of your source code.

(c) Use a debugger to find out more.

And some additional hints:

Your problem is not that important that you need to post it to three
newsgroups. One (comp.lang.java.developer) being a zombie group (not
officially existing), another one (comp.lang.java.programmer) not
intended for such problems. F'up set.

Also, consider adapting the usual naming convention (e.g. class names
start with an uppercase letter).

In addition, post real, complete code. What you posted can't possibly
even compile. Please see and follow http://www.physci.org/codes/sscce.jsp

And finally, consider using a version control system, so you keep track
of your source code changes. Things usually don't stop working out of
the blue. Something has changed. In the great majority of cases it was
the programmer who changed some source code. A version control system
helps you to go back in time and identify where the fatal change happened.

/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,818
Messages
2,569,736
Members
45,709
Latest member
DRISenaida

Latest Threads

Top