Wiki JStore Search Login

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

    Submit JForex API bug reports in this forum only.
    Submit Converter issues in Converter Issues.
    Off topics are strictly forbidden.

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

NullPointerException on initialised Client instance
 Post subject: NullPointerException on initialised Client instance Post rating: 0   New post Posted: Mon 06 Dec, 2021, 17:51 

User rating: 0
Joined: Thu 02 Jul, 2020, 14:15
Posts: 7
Location: Russian FederationRussian Federation
Hi, i'am thinking this is the bug or some kind of unexpected behaviour.
JForex version: 3.6.39
JForex API version: 2.13.98

1. Save this code as jforex terminal plugin with file name as TestPlugin.java:
package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.plugins.IPluginContext;
import com.dukascopy.api.plugins.Plugin;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.IStrategyExceptionHandler;

@RequiresFullAccess // don't know is this needed here
public class TestPlugin extends Plugin {

    public void onStart(IPluginContext context) throws JFException {
        try {
            final IClient client = ClientFactory.getDefaultInstance();
            if (client == null) {
                context.getConsole().getErr().println("null client instance");
            } else {
                final DeadSimpleStrategy strategy = new DeadSimpleStrategy();
                client.startStrategy(strategy, strategy);
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {

    public void onStop() throws JFException {
    public static class DeadSimpleStrategy implements IStrategy, IStrategyExceptionHandler {

        IContext ctx;
        IConsole console;

        public void onStart(IContext context) throws JFException {
            console.getOut().println("Strategy started");

        public void onTick(Instrument instrument, ITick tick) throws JFException {


        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {


        public void onMessage(IMessage message) throws JFException {


        public void onAccount(IAccount account) throws JFException {


        public void onStop() throws JFException {
            console.getOut().println("Strategy stopped!");

        public void onException(long strategyId, Source source, Throwable t) {
            console.getErr().printf("Strategy: %d Error: %s%n", strategyId, t.toString());

2. Compile plugin.
3. Activate plugin.
4. Open forex terminal error log and find something like this:
java.lang.NullPointerException @ com.dukascopy.api.impl.connect.DCClientImpl.a(L:1248)

null: java.lang.NullPointerException
   at com.dukascopy.api.impl.connect.DCClientImpl.a(L:1248)
   at com.dukascopy.api.impl.connect.DCClientImpl.startStrategy(L:1226)
   at jforex.TestPlugin.onStart(TestPlugin.java:22)
   at com.dukascopy.api.plugins.Plugin.onStart(Plugin.java:9)
   at com.dukascopy.api.impl.execution.s.call(L:30)
   at com.dukascopy.api.impl.execution.s.call(L:15)
   at com.dukascopy.api.impl.connect.at.a(L:140)
   at com.dukascopy.api.impl.connect.JForexTaskManager.a(L:887)
   at com.dukascopy.api.impl.execution.j.call(L:34)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at com.dukascopy.api.impl.execution.f$a.e(L:904)
   at com.dukascopy.api.impl.execution.f$a.run(L:926)
   at java.lang.Thread.run(Thread.java:748)

No NullPointerException

I assume I wrote correct code from java and forex api perspective.
Inside the plugin onStart method i'am trying to create instance of IClient class on line 16, because of its singleton nature, I assume that I will get same instance that is auto create when i'am log in into the terminal application. Checking null condition successfully print to the console IClient instance (line 21), but executing startStrategy method (line 22) resulting in NullPointerException in error log. I also tried with DeadSimpleStrategy implementation from separate file (not as nested class) - same behaviour.
If I change IClient to ITesterClient (line 16 + appropriate import):
final ITesterClient client = TesterFactory.getDefaultInstance();

- everything is fine, but of course strategy will not started because client not connected (Connection error).

So, in my opinion this is incorrect behaviour, if usage of IClient inside plugin or maybe totally inside terminal app - is not supported it would be better if it would be more transparent or explicit to the client, for example: ClientFactory.getDefaultInstance() call could return InstantiationException when in terminal context - this is bad, I understand, i'am not experienced in jforex sdk internals, but just as idea. Also improved java docs maybe enough, just expose some restrictions of IClient interface in docs.

Conceptually, I'am trying to create plugin that continuously working as server (zmq, http, grpc or whatever, doesn't matter) and spawn new strategies per client (or several per client, could be complex logic). I'am trying started new strategies via IClient and ITesterClient instances (not via IStrategyManager) because I want to eliminate needs to compile strategy in runtime or precompiled on artifact (jar) building stage. Is i'am on the right way or maybe somebody could help me to get better direction?


 Post subject: Re: NullPointerException on initialised Client instance Post rating: 0   New post Posted: Mon 13 Dec, 2021, 14:10 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6119

NPE will be fixed in next platform release.
java.lang.IllegalStateException: Not connected
will be thrown instead.

Client has to make connection before starting the strategy.


 Post subject: Re: NullPointerException on initialised Client instance Post rating: 0   New post Posted: Mon 13 Dec, 2021, 14:37 

User rating: 0
Joined: Thu 02 Jul, 2020, 14:15
Posts: 7
Location: Russian FederationRussian Federation
Thanks for reply!
Could you clarify this: "Client has to make connection before starting the strategy." - you mean that in my code I need add client.connect() call (with appropriate code for handling state via ISystemListener) after instantiation of IClient instance inside of my plugin code? Is this means that platform (terminal app runtime) has its own "connection" and didn't share it with IClient that I acquire myself? This is coming to my mind from the assumption that IClient encapsulates some "connection" itself, so if IClient is singleton -> "connection" is shared via IClient instance, that's why I think that creating IClient inside terminal app runtime already provides me connection in connected state...

 Post subject: Re: NullPointerException on initialised Client instance Post rating: 0   New post Posted: Thu 06 Jan, 2022, 14:37 
Visual JForex expert at Dukascopy
User avatar

User rating:
Joined: Mon 22 Apr, 2013, 11:30
Posts: 604
Location: UkraineUkraine
IClient is designed for using from SDK - it is not used inside JForex platform, therefore it can't share connection with platform.



Jump to:  

  © 1998-2011 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