Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

NullpointerException on calling account.getEquity()
 Post subject: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Fri 03 Sep, 2010, 07:57 
User avatar

User rating: 5
Joined: Fri 02 Sep, 2011, 10:08
Posts: 157
Location: FranceFrance
Hi,

I'm trying to get the equity in order to update a textField.
My first thought was to call a function that updates my textfield from
public void onAccount(IAccount acount) like this :

public void onAccount(IAccount account) throws JFException {

         equityUpdate = account.getEquity();
         myPanel.updateLabel(Double.toString(equityUpdate));

    }



But I get a nullPointerException on running the strategy :

java.lang.NullPointerException @ jforex.SwingTest.onAccount(SwingTest.java:45)


Then I tried to update a variable from onAccount(IAccount account) and
then update the value of my textField from onTick(Instrument instrument, ITick tick)
and it works !

public void onAccount(IAccount account) throws JFException {
        equityUpdate = account.getEquity();
    }



public void onTick(Instrument instrument, ITick tick) throws JFException {
         myPanel.updateLabel(Double.toString(equityUpdate));
    }



My question is why ??? I don't need to refresh the equity on each tick...

Thanks


Here is the whole code inspired from de swing test exemple.

package jforex;

import java.util.*;
import javax.swing.*;
import com.dukascopy.api.*;
import java.awt.BorderLayout;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.concurrent.Callable;

import javax.swing.*;

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IOrder.State;

public class SwingTest implements IStrategy {
    private IUserInterface userInterface;
    private JPanel myTab;
    private IConsole console;
    private MySpecialPanel myPanel;
    private double equityUpdate;
    private int counter = 0;
   
   
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.userInterface = context.getUserInterface();
       
        myTab = userInterface.getBottomTab("Test");
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                myPanel = new MySpecialPanel();
                myTab.add(myPanel);
            }                       
        });
    }

    public void onAccount(IAccount account) throws JFException {

//
// I want to do this here but I have a null pointer exception
//
         equityUpdate = account.getEquity();
         myPanel.updateLabel(Double.toString(equityUpdate));
    }

    public void onMessage(IMessage message) throws JFException {
    }

    public void onStop() throws JFException {
       
    }

    public void onTick(Instrument instrument, ITick tick) throws JFException {
   
       
    }
   
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
    }
   
   /* private String getLabel(int counter){
        StringBuffer newLabel = new StringBuffer();
        for (int i=1; i<=13; i++){
            if (counter != i)
                newLabel.append("1");
            else
                newLabel.append(i);
        }
        return newLabel.toString();
    }*/
   
    class MySpecialPanel extends JPanel{           
       // JLabel label;
        JTextField equityField;
       
        MySpecialPanel(){
        /*    this.label = new JLabel();
            this.add(label);*/
           
               this.equityField = new JTextField();
               equityField.setColumns(10);
               this.add(equityField);
           
        }
       
        public void updateLabel(final String value){
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    if (equityField!=null)
                    //console.getOut().println("updateLabel() : "+MySpecialPanel.this.label);
                        MySpecialPanel.this.equityField.setText(value);   
                }                       
            });
        }
    }
}


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Fri 03 Sep, 2010, 12:00 

User rating: 0
Joined: Tue 24 Aug, 2010, 20:50
Posts: 15
the problem is that you are starting the thread that initializes myPanel by using SwingUtilities.invokeLater(...).
this method returns, before the code in run() has been executed and it seems that the first call to onAccount() happens before the execution of that thread is finished.
one possible solution is to use SwingUtilities.invokeAndWait(...) instead of invokeLater(...) but the big question is: why do you initialize your panel in a separate thread in the first place?
you could just write the following in onStart():
        this.console = context.getConsole();
        this.userInterface = context.getUserInterface();
       
        myTab = userInterface.getBottomTab("Test");
        myPanel = new MySpecialPanel();
        myTab.add(myPanel);


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Fri 03 Sep, 2010, 14:56 
User avatar

User rating: 5
Joined: Fri 02 Sep, 2011, 10:08
Posts: 157
Location: FranceFrance
Hi Ocire, thanks for the response.

I understand.

Quote:
but the big question is: why do you initialize your panel in a separate thread in the first place?
you could just write the following in onStart()


Since I'm not good at java programing, I took the swing exemple from the support here :
https://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=31&t=4830

He starts the panel in a news thread, I guess it's better when you have some calculation in it.

best regards


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Fri 03 Sep, 2010, 16:48 

User rating: 0
Joined: Tue 24 Aug, 2010, 20:50
Posts: 15
ok, i see...
doing the update in a thread is ok if there is something that might take a long time... however the initialization should NOT be done in a thread as you expect it to be done when you leave onStart().
the reason why it works in the example is that the first tick will arrive quite a few ms, if not even a second or more after onStart() is called. and that gives the thread enough time to initialize everything.


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Tue 07 Sep, 2010, 10:25 
User avatar

User rating: 5
Joined: Fri 02 Sep, 2011, 10:08
Posts: 157
Location: FranceFrance
ok thanks, I'll keep it in mind.

Best regards


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Wed 08 Sep, 2010, 22:32 

User rating: 0
Joined: Fri 07 May, 2010, 02:59
Posts: 61
You could just add a loop that waits until account is not null, and have it do a Thread.sleep(). It's a little dirty, but it works.

-Brian


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Mon 20 Sep, 2010, 12:34 
User avatar

User rating: 3
Joined: Wed 18 May, 2011, 16:25
Posts: 331
Location: SwitzerlandSwitzerland
Hi,

as an alternative to waiting until account is not null by using a thread.sleep()
you also could skip the processing of the ticks.

This has the advantage that you will not start processing on old/outdated tick and possibly missing newer ticks issued since, and you will always start your strategy processing with the newest tick after account initialization has taken place.

Best,
RR.


 
 Post subject: Re: NullpointerException on calling account.getEquity() Post rating: 0   New post Posted: Wed 06 Oct, 2010, 12:34 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
To avoid null pointer exception you need to initialize panel in invokeAndWait method. Please consider the following code:

    
        try {
        SwingUtilities.invokeAndWait(new Runnable(){
            @Override
            public void run() {
                myPanel = new MySpecialPanel();
                myTab.add(myPanel);
            }                       
        });
        } catch (Exception e) {
           console.getOut().println(e.getMessage());
        }



 

Jump to:  

cron
  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com