• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

NullPointerException

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both, It runs now.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic