APS Excellence in Physics Education Award
November 2019

Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

Access view objects from Custom java code post and replies

Return to the Access view objects from Custom java code thread
Login to post to this thread

Custom Page : code to access object properties
7 Posts

I am trying to write custom code that can interact with view objects.

For example:

// =====================
public double getmode () {
  double getmode;

getmode = 111.23;

getmode = _view.plate.getX();    

//  The view contains a 3d drawing panel with a cylinder named "plate"

  return getmode;

// =====================

This gives a run time error like this:

Exception in thread "main" java.lang.NullPointerException
at users.__psb.simple_plate_pkg.simple_plate.getmode(simple_plate.java:458)
at users.__psb.simple_plate_pkg.simple_plate.(simple_plate.java:251)
at users.__psb.simple_plate_pkg.simple_plate.(simple_plate.java:137)
at users.__psb.simple_plate_pkg.simple_plate.main(simple_plate.java:126)

-- Praveen

Attached File: simple_plate.zip

Replies to Custom Page : code to access object properties

Re: Custom Page : code to access object properties -
Francisco Esquembre
228 Posts

Hi Praveen,

The problem is that you are calling getmode() too early.

Ejs first declares the variables, then the view elements. In the variable table you try to call getmode() to give the variable showmode its initial value.

But getmode() calls _view.plate.getX(), and at that time, plate does not exist.

The getmode() function can be called in the initialisation and later on. But calling it that early can give no real value.


OSP Projects:
Open Source Physics - EJS Modeling
Physlet Physics
Physlet Quantum Physics
STP Book