aspose file tools*
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Samuel Andermatt
Greenhorn

Joined: Sep 26, 2012
Posts: 3
Hi
I have done some C++ during my studies and today I decided to learn a little about java. by making a simple Molecular dynamics simulation.
In one line I get a NullpointerException and I do not exactly understand what I do wrong.

The error is in the line (in ParticleSystem.java)
this.P[ i * Ny * Nz + j * Nz + k ] = new Particle( pos , v , R.getRN() );

In case the problem lies somewhere else I copy the rest of the code. If you have a hint about a general missunderstanding how I am supposed to use java I would also appreciate it.

MolecularD.java


ParticleSystem.java

Particle.java


RNG.java




Edit: Ok, Sorry the format gets somehow messed up.

[Added code tags - see UseCodeTags for details]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

Hi Samuel. Welcome to the Ranch!

In that line:There are two things that could cause a NullPointerException. If either this.P or R haven't been initialised you'll get the error. So that's where to start looking.

Starting with R...that looks to have been initialised correctly on line 9 of ParticleSystem.java. So that's OK.

What about this.P? What's happened there is actually a common error. On line 6 you're declaring and initialising a local variable P. But this.P is an instance variable. They've got the same name, but they are not the same thing - the local variable "shadows" the instance variable. And your instance variable is never initialised, as far as I can see.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Welcome to the Ranch.

The problem is that in that line, P is null.

The real problem is in line 6 of ParticleSystem.java, where you declare a new local variable P that hides the member variable P that was declared on line 2. So in line 6 you are not initializing the member variable P; you're declaring a new, local variable P that's initialized. The member variable P remains on its default value, null.

Change line 6 to this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Samuel Andermatt
Greenhorn

Joined: Sep 26, 2012
Posts: 3
Thank you both, It runs now.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Samuel Andermatt wrote:The error is in the line (in ParticleSystem.java)
this.P[ i * Ny * Nz + j * Nz + k ] = new Particle( pos , v , R.getRN() );

Right. Well, first, do you understand Java operator precedence? The above line is the equivalent of:
this.P[ (i * Ny * Nz) + (j * Nz) + k ] = new Particle( pos , v , R.getRN() );
Is that what you want?

Other than that, I can't see any other cause off the top of my head; and if it is the problem, I'm surprised it didn't throw ArrayIndexOutOfBoundsException.

Second: Your RNG class is redundant. Java has a class called java.util.Random, and it is a good RNG - and seedable.

Winston

[Edit] Doh-h! The others have seen what I stupidly missed. However, I still reckon that you should read my post, because the other things may be relevant.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Samuel Andermatt wrote:Thank you both, It runs now.

OK, but you're still giving yourself a lot of headaches by ploughing so much into a single statement. Break things up a bit.

Winston
Samuel Andermatt
Greenhorn

Joined: Sep 26, 2012
Posts: 3
Winston Gutkowski wrote:
Samuel Andermatt wrote:The error is in the line (in ParticleSystem.java)
this.P[ i * Ny * Nz + j * Nz + k ] = new Particle( pos , v , R.getRN() );

Right. Well, first, do you understand Java operator precedence? The above line is the equivalent of:
this.P[ (i * Ny * Nz) + (j * Nz) + k ] = new Particle( pos , v , R.getRN() );
Is that what you want?

Other than that, I can't see any other cause off the top of my head; and if it is the problem, I'm surprised it didn't throw ArrayIndexOutOfBoundsException.

Second: Your RNG class is redundant. Java has a class called java.util.Random, and it is a good RNG - and seedable.

Winston

[Edit] Doh-h! The others have seen what I stupidly missed. However, I still reckon that you should read my post, because the other things may be relevant.


About precedence. Yes the statement you wrote is what I wanted. Mathematically I chose a long vector instead of making a 3 dimensional matrix.
Thanks for the help with the RNG.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Samuel Andermatt wrote:Mathematically I chose a long vector instead of making a 3 dimensional matrix.

Hmmm. Not necessarily the best, since Java "matrices" are arrays of arrays. By doing that, you've forced the JVM into allocating contiguous memory, which may cause spikes and/or delays that you couldn't foresee. You also may suffer from 'locality of reference' issues if this matrix can get very large.

Java is NOT C++.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointerException