Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Java Bean Event Listener Callback Problem

Subject: Java Bean Event Listener Callback Problem

From: Wolfgang

Date: 8 Feb, 2010 16:14:03

Message: 1 of 5

Hi,

I'm trying to create a Java class with a custom event class. But if I try to set the event's Callback property in Matlab, I get the following error:

>> t=handle(EventTest,'Callbackproperties')
>> get(t)
        Class: [1x1 java.lang.Class]
        BereichsFehlerCallback: []
        BereichsFehlerCallbackData: []
>> set(t,'BereichsFehlerCallback',@(h,e)disp(h))
        java.lang.NullPointerException
at com.mathworks.jmi.bean.MatlabBeanInterface.addCallback(MatlabBeanInterface.java:765)
at com.mathworks.jmi.bean.MatlabCallbackInterface.addCallback(MatlabCallbackInterface.java:128)

My EventTest.java file looks like this:

public class EventTest {

private java.util.Vector Abhoerer = new java.util.Vector();

public synchronized void addBereichsFehlerListener(BereichsFehlerListener l)
{
Abhoerer.addElement(l);
}

public synchronized void removeBereichsFehlerListener(BereichsFehlerListener l)
{
Abhoerer.removeElement(l);
}

public interface BereichsFehlerListener extends java.util.EventListener {
void bereichsFehler(BereichsFehlerEvent e);
}

public class BereichsFehlerEvent extends java.util.EventObject
{
private static final long serialVersionUID = 1L;
public float alterWert,neuerWert;

BereichsFehlerEvent(Object Ereignisquelle, float alterWert, float neuerWert)
{
super(Ereignisquelle);
this.alterWert=alterWert;
this.neuerWert=neuerWert;
}
}

public void notifyBereichsFehler()
{
java.util.Vector AbhoererKopie;
synchronized(this)
{
AbhoererKopie= (java.util.Vector)Abhoerer.clone();
}
for (int i=0; i<AbhoererKopie.size(); i++)
{
BereichsFehlerEvent einEreignis = new BereichsFehlerEvent(this, 0, 1);
((BereichsFehlerListener)AbhoererKopie.elementAt(i)).bereichsFehler(einEreignis);
}
}
}

Using the same procedure with standard swing classes works so the problem must be due to my class implementation. Can anyone help me with this ? TIA.

Wolfgang

Subject: Java Bean Event Listener Callback Problem

From: Yair Altman

Date: 8 Feb, 2010 21:26:02

Message: 2 of 5

Wolfgang - the following works perfectly for me on R2008a, WinXP SP3:

1. EventTest.java:
============

public class EventTest {
    private java.util.Vector data = new java.util.Vector();
    
    public synchronized void addMyTestListener(MyTestListener l) {
        data.addElement(l);
    }
    
    public synchronized void removeMyTestListener(MyTestListener l) {
        data.removeElement(l);
    }
    
    public interface MyTestListener extends java.util.EventListener {
        void MyTest(MyTestEvent e);
    }
    
    public class MyTestEvent extends java.util.EventObject {
        private static final long serialVersionUID = 1L;
        public float oldValue,newValue;
        
        MyTestEvent(Object obj, float oldValue, float newValue) {
            super(obj);
            this.oldValue = oldValue;
            this.newValue = newValue;
        }
    }
    
    public void notifyMyTest() {
        java.util.Vector dataCopy;
        synchronized(this) {
            dataCopy = (java.util.Vector)data.clone();
        }
        for (int i=0; i<dataCopy.size(); i++) {
            MyTestEvent event = new MyTestEvent(this, 0, 1);
            ((MyTestListener)dataCopy.elementAt(i)).MyTest(event);
        }
    }
}

2. Now compile with a JDK that is compatible with your Matlab JVM (as reported by version -java) => you get EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class files.

3. Now place these 3 files in your Matlab's *static* Java classpath (as reported by javaclasspath)

4. Restart Matlab and run the the following, which show that all works ok:

>> which EventTest
EventTest is a Java method % EventTest constructor

>> evt = EventTest
evt =
EventTest@16166fc

>> evt.get
Class = [ (1 by 1) java.lang.Class array]
MyTestCallback =
MyTestCallbackData = []
        ...

>> set(evt,'MyTestCallback',@(h,e)disp(h))
>> get(evt)
Class = [ (1 by 1) java.lang.Class array]
MyTestCallback = [ (1 by 1) function_handle array]
MyTestCallbackData = []
        ...

>> evt.notifyMyTest
              0.0009765625

5. Note that there is no specific need to use handle(,'CallbackProperties'), although it also works (and is generally advisable):

>> hevt = handle(evt,'CallbackProperties')
hevt =
javahandle_withcallbacks.EventTest

>> set(hevt,'MyTestCallback',@(h,e)disp(h))

>> hevt.get
                 Class: [1x1 java.lang.Class]
        MyTestCallback: @(h,e)disp(h)
    MyTestCallbackData: []


Yair Altman
http://UndocumentedMatlab.com

Subject: Java Bean Event Listener Callback Problem

From: joe

Date: 5 Jul, 2010 13:48:05

Message: 3 of 5

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hkpvha$sq0$1@fred.mathworks.com>...
> Wolfgang - the following works perfectly for me on R2008a, WinXP SP3:
>
> 1. EventTest.java:
> ============
>
> public class EventTest {
> private java.util.Vector data = new java.util.Vector();
>
> public synchronized void addMyTestListener(MyTestListener l) {
> data.addElement(l);
> }
>
> public synchronized void removeMyTestListener(MyTestListener l) {
> data.removeElement(l);
> }
>
> public interface MyTestListener extends java.util.EventListener {
> void MyTest(MyTestEvent e);
> }
>
> public class MyTestEvent extends java.util.EventObject {
> private static final long serialVersionUID = 1L;
> public float oldValue,newValue;
>
> MyTestEvent(Object obj, float oldValue, float newValue) {
> super(obj);
> this.oldValue = oldValue;
> this.newValue = newValue;
> }
> }
>
> public void notifyMyTest() {
> java.util.Vector dataCopy;
> synchronized(this) {
> dataCopy = (java.util.Vector)data.clone();
> }
> for (int i=0; i<dataCopy.size(); i++) {
> MyTestEvent event = new MyTestEvent(this, 0, 1);
> ((MyTestListener)dataCopy.elementAt(i)).MyTest(event);
> }
> }
> }
>
> 2. Now compile with a JDK that is compatible with your Matlab JVM (as reported by version -java) => you get EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class files.
>
> 3. Now place these 3 files in your Matlab's *static* Java classpath (as reported by javaclasspath)
>
> 4. Restart Matlab and run the the following, which show that all works ok:
>
> >> which EventTest
> EventTest is a Java method % EventTest constructor
>
> >> evt = EventTest
> evt =
> EventTest@16166fc
>
> >> evt.get
> Class = [ (1 by 1) java.lang.Class array]
> MyTestCallback =
> MyTestCallbackData = []
> ...
>
> >> set(evt,'MyTestCallback',@(h,e)disp(h))
> >> get(evt)
> Class = [ (1 by 1) java.lang.Class array]
> MyTestCallback = [ (1 by 1) function_handle array]
> MyTestCallbackData = []
> ...
>
> >> evt.notifyMyTest
> 0.0009765625
>
> 5. Note that there is no specific need to use handle(,'CallbackProperties'), although it also works (and is generally advisable):
>
> >> hevt = handle(evt,'CallbackProperties')
> hevt =
> javahandle_withcallbacks.EventTest
>
> >> set(hevt,'MyTestCallback',@(h,e)disp(h))
>
> >> hevt.get
> Class: [1x1 java.lang.Class]
> MyTestCallback: @(h,e)disp(h)
> MyTestCallbackData: []
>
>
> Yair Altman
> http://UndocumentedMatlab.com

Hallo and thank you for your info. I like your java code and I would like to use it. I have only small experience programming java and it is hard for me to understand your code. Would it be possible for you to comment it, or to make another simple coding. Something like: JavaEventProducerClass - class that fires event and sends for example string as eventData. And another MatlabEventListenerClass - that recieve this evet object and displays the string.

Thanks Joe

Subject: Java Bean Event Listener Callback Problem

From: joe

Date: 5 Jul, 2010 15:02:04

Message: 4 of 5

"joe " <b574988@tyldd.com> wrote in message <i0snql$sc$1@fred.mathworks.com>...
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hkpvha$sq0$1@fred.mathworks.com>...
> > Wolfgang - the following works perfectly for me on R2008a, WinXP SP3:
> >
> > 1. EventTest.java:
> > ============
> >
> > public class EventTest {
> > private java.util.Vector data = new java.util.Vector();
> >
> > public synchronized void addMyTestListener(MyTestListener l) {
> > data.addElement(l);
> > }
> >
> > public synchronized void removeMyTestListener(MyTestListener l) {
> > data.removeElement(l);
> > }
> >
> > public interface MyTestListener extends java.util.EventListener {
> > void MyTest(MyTestEvent e);
> > }
> >
> > public class MyTestEvent extends java.util.EventObject {
> > private static final long serialVersionUID = 1L;
> > public float oldValue,newValue;
> >
> > MyTestEvent(Object obj, float oldValue, float newValue) {
> > super(obj);
> > this.oldValue = oldValue;
> > this.newValue = newValue;
> > }
> > }
> >
> > public void notifyMyTest() {
> > java.util.Vector dataCopy;
> > synchronized(this) {
> > dataCopy = (java.util.Vector)data.clone();
> > }
> > for (int i=0; i<dataCopy.size(); i++) {
> > MyTestEvent event = new MyTestEvent(this, 0, 1);
> > ((MyTestListener)dataCopy.elementAt(i)).MyTest(event);
> > }
> > }
> > }
> >
> > 2. Now compile with a JDK that is compatible with your Matlab JVM (as reported by version -java) => you get EventTest.class, EventTest$MyTestEvent.class and EventTest$MyTestListener.class files.
> >
> > 3. Now place these 3 files in your Matlab's *static* Java classpath (as reported by javaclasspath)
> >
> > 4. Restart Matlab and run the the following, which show that all works ok:
> >
> > >> which EventTest
> > EventTest is a Java method % EventTest constructor
> >
> > >> evt = EventTest
> > evt =
> > EventTest@16166fc
> >
> > >> evt.get
> > Class = [ (1 by 1) java.lang.Class array]
> > MyTestCallback =
> > MyTestCallbackData = []
> > ...
> >
> > >> set(evt,'MyTestCallback',@(h,e)disp(h))
> > >> get(evt)
> > Class = [ (1 by 1) java.lang.Class array]
> > MyTestCallback = [ (1 by 1) function_handle array]
> > MyTestCallbackData = []
> > ...
> >
> > >> evt.notifyMyTest
> > 0.0009765625
> >
> > 5. Note that there is no specific need to use handle(,'CallbackProperties'), although it also works (and is generally advisable):
> >
> > >> hevt = handle(evt,'CallbackProperties')
> > hevt =
> > javahandle_withcallbacks.EventTest
> >
> > >> set(hevt,'MyTestCallback',@(h,e)disp(h))
> >
> > >> hevt.get
> > Class: [1x1 java.lang.Class]
> > MyTestCallback: @(h,e)disp(h)
> > MyTestCallbackData: []
> >
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
>
> Hallo and thank you for your info. I like your java code and I would like to use it. I have only small experience programming java and it is hard for me to understand your code. Would it be possible for you to comment it, or to make another simple coding. Something like: JavaEventProducerClass - class that fires event and sends for example string as eventData. And another MatlabEventListenerClass - that recieve this evet object and displays the string.
>
> Thanks Joe

One another question: I did all you wrote in your last post.
After running:
set(evt,'MyTestCallback',@(h,e)disp(h))

I got following error:
java.lang.NullPointerException
at com.mathworks.jmi.bean.MatlabBeanInterface.addCallback(MatlabBeanInterface.java:765)
at com.mathworks.jmi.bean.MatlabCallbackInterface.addCallback(MatlabCallbackInterface.java:128)

Subject: Java Bean Event Listener Callback Problem

From: Ken

Date: 1 Dec, 2010 13:21:02

Message: 5 of 5

> One another question: I did all you wrote in your last post.
> After running:
> set(evt,'MyTestCallback',@(h,e)disp(h))
>
> I got following error:
> java.lang.NullPointerException
> at com.mathworks.jmi.bean.MatlabBeanInterface.addCallback(MatlabBeanInterface.java:765)
> at com.mathworks.jmi.bean.MatlabCallbackInterface.addCallback(MatlabCallbackInterface.java:128)


Yair has published a new article on this topic at:
http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/

When I implemented his approach placing the class files in a *dynamic* java class path folder I received the java.lang.NullPointerException error you mention above.

However, when it is correctly placed in a *static* class path folder (as suggested in this thread and in the article) then it works as Yair describes.

The problem with using the static folder is that every time you want to edit and re-compile the class, Matlab needs to be restarted. This is a real pain when trying to develop/test the java class code. Does anyone know why using the dynamic path causes the java.lang.NullPointerException? Is there a way to fix this problem?

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us