Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Samuel Andermatt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Samuel Andermatt
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both, It runs now.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic