views:

35

answers:

2
 private void jButtonStiahniActionPerformed(java.awt.event.ActionEvent evt) {                                               
  //start the Rengine (JRI)
  String src,symbol1,symbol2 = null,title;
  REXP exp2;
  Rengine re = new Rengine(null, false, null);

  re.eval("library('quantmod')");
            if(!boolOanda){
               src="yahoo";
               symbol1=jComboBoxSymbols.getSelectedItem().toString();
               re.eval("kurz=getSymbols('"+symbol1+"',src='"+src+"')"); 
                exp2 = re.eval(symbol1+"$"+symbol1+".Close"); 
            }
            else {
                src="oanda";
                symbol1=jComboBoxSymbols.getSelectedItem().toString();
                symbol2=jComboBoxSymbols2.getSelectedItem().toString();

                re.eval("kurz=getSymbols('"+symbol1+"/"+symbol2+"',src='"+src+"')"); 
                exp2 = re.eval(symbol1+symbol2); 

            }

                   double[] kurz = exp2.asDoubleArray(); 

                        re.end();

       }       

I got java.lang.NullPointerException at double[] kurz = exp2.asDoubleArray(); when I try to push this jButton second time . First time everything goes well, but for the second time I have exception. It seems like variable exp2 is null, but I dont understand why. Thank you in advance

A: 

Well, the (perhaps flippant) answer is that yes, exp2 is null. Without knowing anything about this REngine, or what the logic of its eval method is, it's hard to add much more than that. I can't even tell what the value of boolOanda is on the second invocation (or whether it changes from the first), so it's not clear which branch is executed to set exp2.

What I'd suggest is attaching a debugger to the process, then stepping through the method calls and seeing what's actually being invoked, and at what point the execution diverges from your expectations.

I would guess that maybe you mean to eval symbol1 + '.' + symbol2 (currently both symbols are directly concatenated). Or perhaps the input is simply entirely wrong, and the symbol variables are completely null on the second invocation. But as I said above, I don't really know what the dynamic model looks like here so ultimately you'd have to be the one to spot the discrepancy.

Andrzej Doyle
A: 

If exp2 is null, it's the result of one of these two assignments:

exp2 = re.eval(symbol1+"$"+symbol1+".Close");

or

exp2 = re.eval(symbol1+symbol2); 

So, you need to do some debugging to figure out (a) which branch of the if(!boolOanda) was being executed, (b) what values were being passed to re.eval, and (c) why re.eval was rejecting them.

David Gelhar