Link to home
Start Free TrialLog in
Avatar of gbcbr
gbcbrFlag for Cyprus

asked on

Getting variable value

I try to get variable boolean bc[0] by calling instance:
public class BorderControl {

    private static BorderControl instance = new BorderControl();

    public static BorderControl getInstance() {
        return instance;
    }
    boolean bc[] = new boolean[10];
    double bidPx[] = new double[10];
    double askPx[] = new double[10];
.............
    public void borderControl(boolean[] bc, double[] bidPx, double[] askPx) {

        if (bidPx[0] != 0 && askPx[0] != 0)
            
            try {
                formatter = new DecimalFormat("#0.0");

                resist[0] = DCC.getInstance().getTextresistEURUSD();
                rt[0] = Double.parseDouble(resist[0]);
                drt[0] = (rt[0] - askPx[0]) * 10000;
                dresist[0] = formatter.format(drt[0]);
                DCC.getInstance().setEURUSD_resist(dresist[0]);

                support[0] = DCC.getInstance().getTextsupportEURUSD();
                st[0] = Double.parseDouble(support[0]);
                srt[0] = (bidPx[0] - st[0]) * 10000;
                dsupport[0] = formatter.format(srt[0]);
                DCC.getInstance().setEURUSD_support(dsupport[0]);        

                if (drt[0] <= 5 || srt[0] <= 5) {
                
                    bc[0] = false;
                    
                } else {
                    
                    bc[0] = true;
                }
                System.out.println("    bc[0] = " + bc[0]);

Open in new window

I have it here, but when I call it from other class
public void businessLogic_EURUSD(double[] outY0) throws Exception {

           System.out.println("%%%%%%%%%%%%  BL outY0[0] = " + outY0[0] );
                       
            try {

                if (outY0[0] < 0){

                    bl0 = false;

                    System.out.println("   bl0 = " + bl0 + "\n");

                    dc_EURUSD_S = new DecisionCenter_EURUSD_S();
                    dc_EURUSD_S.decisionCenter_EURUSD_S(bl0);

Open in new window

public void decisionCenter_EURUSD_S(boolean bl0) throws Exception {

        System.out.println("  bl0   " + bl0);
        System.out.println(" &&&&&&&  flag0s   " + flag0s);
        System.out.println(" ^^^^^^^  mflag0s   " + mflag0s);

        BorderControl.getInstance().borderControl(bc, bidPx, askPx);

        System.out.println(" DC   bc[0] = " + bc[0]);
        System.out.println(" DC   bidPx[0] = " + bidPx[0]);

Open in new window

, I get error  
%%%%%%%%%%%%  BL outY0[0] = -47624.27
   bl0 = false => Businesslogic

  bl0   false             => start DecisionCenter
 &&&&&&&  flag0s   false  
 ^^^^^^^  mflag0s   false

=> start call getInstance BorderControl from DecisionCenter

28.02.2011 19:55:20 EURUSD.BusinessLogic_EURUSD businessLogic_EURUSD
SEVERE: null
java.lang.NullPointerException
	at EURUSD.BorderControl.borderControl(BorderControl.java:37)
	at EURUSD.DecisionCenter_EURUSD_S.decisionCenter_EURUSD_S(DecisionCenter_EURUSD_S.java:45)
	at EURUSD.BusinessLogic_EURUSD.businessLogic_EURUSD(BusinessLogic_EURUSD.java:47)
	at EURUSD.AlgoCalcY_EURUSD.outY(AlgoCalcY_EURUSD.java:33)
	at algo.ArrayConverter.arrayConverter(ArrayConverter.java:81)
	at connect.SelectData.run(SelectData.java:66)
	at java.util.TimerThread.mainLoop(Timer.java:512)
	at java.util.TimerThread.run(Timer.java:462)

Open in new window

at EURUSD.BorderControl.borderControl(BorderControl.java:37) => if (bidPx[0] != 0 && askPx[0] != 0)
Why it references to this line? And why it shows error not from DecisionCenter?
Please advice
Avatar of for_yan
for_yan
Flag of United States of America image

Because in your method these instance arrays are overriden by their name in the parameters
list
you ned to say
this.bidPx = bidPx;
this.askPx = askPx;
 first thing inside the method
Avatar of gbcbr

ASKER

   public void borderControl(boolean[] bc, double[] bidPx, double[] askPx) {

        this.bidPx = bidPx;
        this.askPx = askPx;
        if (bidPx[0] != 0 && askPx[0] != 0)

the same
Well, don't know, but if I were you and seeing null pointer at this line - it has to be about the arrays,
so even though it should have worked as before,  I'd still have assigned one by one:

for(int j=0; j<bidPX.length; j++){
this.bidPx[j] = bidPx[j];
 this.askPx[j] = askPx[j];
}

At least you'll see the error moving  a few lines ahead - still some information




Look but waht means if() whwithout {...} after it?
It should not have compiled even?
I mean this.
What does it mean:

    if (bidPx[0] != 0 && askPx[0] != 0)  <----   what's next? it should be either one stateemnt or {} after if
           
            try {
                formatter = new DecimalFormat("#0.0");
Avatar of gbcbr

ASKER

I don't understand at all where is the problem:
 
public void borderControl(boolean[] bc, double[] bidPx, double[] askPx) {

        for (int j = 0; j < bidPx.length; j++) {
            this.bidPx[j] = bidPx[j];
            this.askPx[j] = askPx[j];
        }
        
        if (bidPx[0] != 0 && askPx[0] != 0)
        {
            
            try {

Open in new window

bl0   false
 &&&&&&&  flag0s   false
 ^^^^^^^  mflag0s   false
28.02.2011 20:45:00 EURUSD.BusinessLogic_EURUSD businessLogic_EURUSD
SEVERE: null
java.lang.NullPointerException
	at control.BorderControl.borderControl(BorderControl.java:35)

Open in new window

35.  for (int j = 0; j < bidPx.length; j++) {

BorderControl class works perfect without referencing to it, but I can't call it directly because it get and set data into GUI DCC class and DecisionCenter has to call him only when it need to get confirmation.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gbcbr

ASKER

sorry, I lost your reply at the time.

The whole code is:  
 
public class BorderControl {

    private static BorderControl instance = new BorderControl();

    public static BorderControl getInstance() {
        return instance;
    }


    String resist[] = new String[10];
    String support[] = new String[10];
    double rt[] = new double[10];
    double st[] = new double[10];
    double drt[] = new double[10];
    double srt[] = new double[10];
    String dresist[] = new String[10];
    String dsupport[] = new String[10];
    boolean bc[] = new boolean[10];
    double bidPx[] = new double[10];
    double askPx[] = new double[10];
    NumberFormat formatter;
    

    public void borderControl(boolean[] bc, double[] bidPx, double[] askPx) {

            this.bidPx = bidPx;
            this.askPx = askPx;
        
        
        if (bidPx[0] != 0 && askPx[0] != 0)
        {
            
            try {
                formatter = new DecimalFormat("#0.0");

                resist[0] = DCC.getInstance().getTextresistEURUSD();
                rt[0] = Double.parseDouble(resist[0]);
                drt[0] = (rt[0] - askPx[0]) * 10000;
                dresist[0] = formatter.format(drt[0]);
                DCC.getInstance().setEURUSD_resist(dresist[0]);

                support[0] = DCC.getInstance().getTextsupportEURUSD();
                st[0] = Double.parseDouble(support[0]);
                srt[0] = (bidPx[0] - st[0]) * 10000;
                dsupport[0] = formatter.format(srt[0]);
                DCC.getInstance().setEURUSD_support(dsupport[0]);

                if (drt[0] <= 5 || srt[0] <= 5) {
                
                    bc[0] = false;
                    
                } else {
                    
                    bc[0] = true;
                }
                System.out.println("    bc[0] = " + bc[0]);
            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }
        }
        
        if (bidPx[1] != 0 && askPx[1] != 0)
            
            try {

                formatter = new DecimalFormat("#0.0");
      
                resist[1] = DCC.getInstance().getTextresistEURCHF();
                rt[1] = Double.parseDouble(resist[1]);
                drt[1] = (rt[1] - askPx[1]) * 10000;
                dresist[1] = formatter.format(drt[1]);
                DCC.getInstance().setEURCHF_resist(dresist[1]);

                support[1] = DCC.getInstance().getTextsupportEURCHF();
                st[1] = Double.parseDouble(support[1]);
                srt[1] = (bidPx[1] - st[1]) * 10000;
                dsupport[1] = formatter.format(srt[1]);
                DCC.getInstance().setEURCHF_support(dsupport[1]);

                if (drt[1] <= 5 || srt[1] <= 5) {
                
                    bc[1] = false;
                    
                } else {
                    
                    bc[1] = true;
                }
                System.out.println("    bc[1] = " + bc[1]);
               

            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[2] != 0 && askPx[2] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[2] = DCC.getInstance().getTextresistEURGBP();
                rt[2] = Double.parseDouble(resist[2]);
                drt[2] = (rt[2] - askPx[2]) * 10000;
                dresist[2] = formatter.format(drt[2]);
                DCC.getInstance().setEURGBP_resist(dresist[2]);

                support[2] = DCC.getInstance().getTextsupportEURGBP();
                st[2] = Double.parseDouble(support[2]);
                srt[2] = (bidPx[2] - st[2]) * 10000;
                dsupport[2] = formatter.format(srt[2]);
                DCC.getInstance().setEURGBP_support(dsupport[2]);

                if (drt[2] <= 5 || srt[2] <= 5) {

                    bc[2] = false;

                } else {

                    bc[2] = true;
                }
//                System.out.println("    bc[2] = " + bc[2]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[3] != 0 && askPx[3] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[3] = DCC.getInstance().getTextresistUSDCHF();
                rt[3] = Double.parseDouble(resist[3]);
                drt[3] = (rt[3] - askPx[3]) * 10000;
                dresist[3] = formatter.format(drt[3]);
                DCC.getInstance().setUSDCHF_resist(dresist[3]);

                support[3] = DCC.getInstance().getTextsupportUSDCHF();
                st[3] = Double.parseDouble(support[3]);
                srt[3] = (bidPx[3] - st[3]) * 10000;
                dsupport[3] = formatter.format(srt[3]);
                DCC.getInstance().setUSDCHF_support(dsupport[3]);

                if (drt[3] <= 5 || srt[3] <= 5) {

                    bc[3] = false;

                } else {

                    bc[3] = true;
                }
//                System.out.println("    bc[3] = " + bc[3]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[4] != 0 && askPx[4] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[4] = DCC.getInstance().getTextresistGBPUSD();
                rt[4] = Double.parseDouble(resist[4]);
                drt[4] = (rt[4] - askPx[4]) * 10000;
                dresist[4] = formatter.format(drt[4]);
                DCC.getInstance().setGBPUSD_resist(dresist[4]);

                support[4] = DCC.getInstance().getTextsupportGBPUSD();
                st[4] = Double.parseDouble(support[4]);
                srt[4] = (bidPx[4] - st[4]) * 10000;
                dsupport[4] = formatter.format(srt[4]);
                DCC.getInstance().setGBPUSD_support(dsupport[4]);

                if (drt[4] <= 5 || srt[4] <= 5) {

                    bc[4] = false;

                } else {

                    bc[4] = true;
                }
//                System.out.println("    bc[4] = " + bc[4]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[5] != 0 && askPx[5] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[5] = DCC.getInstance().getTextresistGBPCHF();
                rt[5] = Double.parseDouble(resist[5]);
                drt[5] = (rt[5] - askPx[5]) * 10000;
                dresist[5] = formatter.format(drt[5]);
                DCC.getInstance().setGBPCHF_resist(dresist[5]);

                support[5] = DCC.getInstance().getTextsupportGBPCHF();
                st[5] = Double.parseDouble(support[5]);
                srt[5] = (bidPx[5] - st[5]) * 10000;
                dsupport[5] = formatter.format(srt[5]);
                DCC.getInstance().setGBPCHF_support(dsupport[5]);

                if (drt[5] <= 5 || srt[5] <= 5) {

                    bc[5] = false;

                } else {

                    bc[5] = true;
                }
//                System.out.println("    bc[5] = " + bc[5]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[6] != 0 && askPx[6] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[6] = DCC.getInstance().getTextresistEURJPY();
                rt[6] = Double.parseDouble(resist[6]);
                drt[6] = (rt[6] - askPx[6]) * 100;
                dresist[6] = formatter.format(drt[6]);
                DCC.getInstance().setEURJPY_resist(dresist[6]);

                support[6] = DCC.getInstance().getTextsupportEURJPY();
                st[6] = Double.parseDouble(support[6]);
                srt[6] = (bidPx[6] - st[6]) * 100;
                dsupport[6] = formatter.format(srt[6]);
                DCC.getInstance().setEURJPY_support(dsupport[6]);

                if (drt[6] <= 5 || srt[6] <= 5) {

                    bc[6] = false;

                } else {

                    bc[6] = true;
                }
//                System.out.println("    bc[6] = " + bc[6]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[7] != 0 && askPx[7] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[7] = DCC.getInstance().getTextresistUSDJPY();
                rt[7] = Double.parseDouble(resist[7]);
                drt[7] = (rt[7] - askPx[7]) * 100;
                dresist[7] = formatter.format(drt[7]);
                DCC.getInstance().setUSDJPY_resist(dresist[7]);

                support[7] = DCC.getInstance().getTextsupportUSDJPY();
                st[7] = Double.parseDouble(support[7]);
                srt[7] = (bidPx[7] - st[7]) * 100;
                dsupport[7] = formatter.format(srt[7]);
                DCC.getInstance().setUSDJPY_support(dsupport[7]);

                if (drt[7] <= 5 || srt[7] <= 5) {

                    bc[7] = false;

                } else {

                    bc[7] = true;
                }
//                System.out.println("    bc[7] = " + bc[7]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[8] != 0 && askPx[8] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[8] = DCC.getInstance().getTextresistGBPJPY();
                rt[8] = Double.parseDouble(resist[8]);
                drt[8] = (rt[8] - askPx[8]) * 100;
                dresist[8] = formatter.format(drt[8]);
                DCC.getInstance().setGBPJPY_resist(dresist[8]);

                support[8] = DCC.getInstance().getTextsupportGBPJPY();
                st[8] = Double.parseDouble(support[8]);
                srt[8] = (bidPx[8] - st[8]) * 100;
                dsupport[8] = formatter.format(srt[8]);
                DCC.getInstance().setGBPJPY_support(dsupport[8]);

                if (drt[8] <= 5 || srt[8] <= 5) {

                    bc[8] = false;

                } else {

                    bc[8] = true;
                }
//                System.out.println("    bc[8] = " + bc[8]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }

        if (bidPx[9] != 0 && askPx[9] != 0)

            try {

                formatter = new DecimalFormat("#0.0");

                resist[9] = DCC.getInstance().getTextresistCHFJPY();
                rt[9] = Double.parseDouble(resist[9]);
                drt[9] = (rt[9] - askPx[9]) * 100;
                dresist[9] = formatter.format(drt[9]);
                DCC.getInstance().setCHFJPY_resist(dresist[9]);

                support[9] = DCC.getInstance().getTextsupportCHFJPY();
                st[9] = Double.parseDouble(support[9]);
                srt[9] = (bidPx[9] - st[9]) * 100;
                dsupport[9] = formatter.format(srt[9]);
                DCC.getInstance().setCHFJPY_support(dsupport[9]);

                if (drt[9] <= 5 || srt[9] <= 5) {

                    bc[9] = false;

                } else {

                    bc[9] = true;
                }
//                System.out.println("    bc[9] = " + bc[9]);


            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }
    }
}

Open in new window

so, for each array member I have own try.


What that means:

"so, for each array member I have own try."

Well, maybe I'm old-fashioned, but I kind of like to know when happens what,
especially when it throws ununderstandable error.

So what I do - I can declare variables on top of the class -
that's what you shuold do with instance variables.

Any actions, e.g. creating an array - I'd rather do in
exceutable place - inside the constructor.

So you say

String resist[];

on top.

then inside constructir you say

resist = new String[10];

If that does not haelp you can then at least
put printourt after it and fllow the steps,
when it is created, and what happens with it next.

If you could see the reason immediately, then fine, if not - i'd
at least do it this way and debufg step-by-step.
Especially, when you prefer to use this getInstance and all that static stuff
and all these things which are much more difficult to understand






 
Avatar of gbcbr

ASKER

>>What that means:>>
I mean that when from PMDS class arrive new data and this data is, let's say bidPx[0] and askPx[0] this method try to use this parameters, if it will arrive bidPx[1] and askPx[1], it has to use another parameters.
From my point of view it's absolutely logical.
And when I invoke this method from DecisionCenter class it has to read all variables into BorderControl and get what it's need, means boolean bc[0] value.
This is my logic, tell me me if I'm wrong.

Sorry, for me it is too difficult to understand.

Well the only thing we know is by the time this call happens
these your arrays turn out to be not initialized.

If these pairs appear one by one - you should not pass arrays
you should  rather pass individual values and the arrays should by that time be initialized
and then you shoudl not have these this....=... operators every time you call this method - i thought you call it once,
- arrays should have been pre-created and here you jsut pass a pair of values which you assign to the next element
or you can specify to which elemet to assign, if you know it,
if this is what you are doing.

Again with this static declaration in the beginning when should the constructor forr this class be running?
I guess at the time of the loading. When are your arrays intialized?
Put them into the constructor - you at least can check if they were already created
after their initialization.


I'd still arrange all excutable statetemnt into executable sections.
Then it is easier to follow and understand when the array was created and when it is used
and shchek it is avaialable at every stage - after it is created,
after elemet is populated, when you want to access the element
and make sure it is not used before it is created - I guess, ultimately that is what happened
with all this convoluted logic.

Avatar of gbcbr

ASKER

Yes, problem is into array.
I change two pair to simple and create separate methods for each pair and it works:
public void borderControl_eurusd (boolean bc, double bidPx, double askPx) {
      
    if (bidPx != 0 && askPx != 0)
    {
        
        try {
            formatter = new DecimalFormat("#0.0");

            resist[0] = DCC.getInstance().getTextresistEURUSD();
            rt[0] = Double.parseDouble(resist[0]);
            drt[0] = (rt[0] - askPx) * 10000;
            dresist[0] = formatter.format(drt[0]);
            DCC.getInstance().setEURUSD_resist(dresist[0]);

            support[0] = DCC.getInstance().getTextsupportEURUSD();
            st[0] = Double.parseDouble(support[0]);
            srt[0] = (bidPx - st[0]) * 10000;
            dsupport[0] = formatter.format(srt[0]);
            DCC.getInstance().setEURUSD_support(dsupport[0]);

            if (drt[0] <= 5 || srt[0] <= 5) {
            
                bc = false;
                
            } else {
                
                bc = true;
            }
            System.out.println(" EUR/USD   bc = " + bc);
        } catch (Exception ex) {
            Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                null, ex);
        }
            }
  
  }

Open in new window

But the question is why array doesn't work? I like it more because it's much elegant and professional solution.
But for my practical use I can use also this solution.
Well, it is good that it works.

My take on it is that the thing which works with the minmum
effort and debugging time - that thing is the most elegant.

Well maybe most people think differently, but my mantra worked for me
for many years. Those people who use the programs they
definitely don't care what it does inside - they do care that it works and does
what they want, and most importantly, they want to have it by the time,
 they haven't yet thought about some next thing.
Avatar of gbcbr

ASKER

From one hand I'm 100% agreed with you, as my coach told "doesn't matter how everybody runs, only important -  who wins!".
But from another hand I prefer to find a mistake, so next time I know where is rakes.

I agree with that - and in order to do that I try to write it
all in the simples possible way - and in the way I can remember.

It is good to know about all new stuff - on the peripheral knowledge - when
you encounter some real problem - the you rememeber about it and look up the details - but whatever
can work simple - better to use simple. And of course to debug if you follow all the time
the same template - and the simplest one at that it is much easier.

You can spend days figuring out is Vector more effective than array
but if you write application were you have 100 items in any of them
you'll never see the difference. By the time you'll need application
with 100000 items, they'll invent ArrayList which has still other properties and the days you spent on your research
will be gone in vain.

So you wrirte to accommodate your immediate need the way you remember in the simplest and fastest way possible - once you
see that your users don't like to wait at some point - then
you investigate and find that one place out of thousands
of lines where indeed your use of array vs Vector is crucial and then
you know what you are doing and why.

Unfortunately most of the books do not teach this way probably because
they are written by the minority who program in
the situation much different from situation
of many other folks.

Avatar of gbcbr

ASKER

I found the error. Thank you.
Avatar of gbcbr

ASKER

Now this class works perfect. Error was in array. This is correct code:
public void borderControl(double[] bidPx, double[] askPx) {


        if (bidPx[0] != 0 && askPx[0] != 0) {

            try {
                formatter = new DecimalFormat("#0.0");

                resist[0] = DCC.getInstance().getTextresistEURUSD();
                rt[0] = Double.parseDouble(resist[0]);
                drt[0] = (rt[0] - askPx[0]) * 10000;
                dresist[0] = formatter.format(drt[0]);
                DCC.getInstance().setEURUSD_resist(dresist[0]);

                support[0] = DCC.getInstance().getTextsupportEURUSD();
                st[0] = Double.parseDouble(support[0]);
                srt[0] = (bidPx[0] - st[0]) * 10000;
                dsupport[0] = formatter.format(srt[0]);
                DCC.getInstance().setEURUSD_support(dsupport[0]);
                if (drt[0] <= 10 || srt[0] <= 10) {

                    bc[0] = false;

                    DecisionCenter_EURUSD_S.getInstance().bControl(bc);
                    DecisionCenter_EURUSD_B.getInstance().bControl(bc);

                } else {

                    bc[0] = true;

                    DecisionCenter_EURUSD_S.getInstance().bControl(bc);
                    DecisionCenter_EURUSD_B.getInstance().bControl(bc);
                }

            } catch (Exception ex) {
                Logger.getLogger(BorderControl.class.getName()).log(Level.SEVERE,
                                                                    null, ex);
            }
        }

Open in new window

Avatar of gbcbr

ASKER

please check my question about OHLC