Returning string arrays

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Returning string arrays

Greg Allen-2

I have created a java class that contains a method that returns an array of strings:

 

    public String[] substituteTemplate (String template, Hashtable subs) throws java.io.IOException, java.io.FileNotFoundException {

            Vector v = new Vector<String>();

 

        … some code here ...

 

        String[] result = new String[v.size()];

        return result;

    }

 

I have placed the .jar file containing this class on the classpath and added it to my
beanshell script. I then try to call the method.

 

            try {

                text = substituteTemplatey(macroFile, vars);

                //Macros.message(view, text.toString());

            } catch (Exception e) {

                Macros.message(view, "Exception");

            }

 

The method appears to be called OK, but the return value can’t seem to be accessed. I get
a silent error and my script just ends. I’ve tried various things but can’t seem to access
the returned item.

 

Is there something special I have to do here? It seems pretty straightforward, but I can’t
get it to work.

 

Any suggestions?

 

Thanks,

 

-- Greg

 

 


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Returning string arrays

Daniel Martin-7
"Greg Allen" <[hidden email]> writes:

> I have created a java class that contains a method that returns an array
> of strings:

A couple of things here:

First off, your java code appears to be returning an array that
consists of a bunch of null values:

>         String[] result = new String[v.size()];
>         return result;
>     }

You probably wanted to make that return statement

          return v.toArray(result);

Secondly, when you quote the bsh code that supposedly is calling this
function, I don't see you give a class name.  You also seem to call
"substituteTemplatey" instead of "substituteTemplate".

Finally, it may be that your problem is that you didn't use or declare
"text" outside the try clause, so your "text" variable got limited to
just the try clause.  See if this works for you:

    text = null;
    try {
        text = substituteTemplate(macroFile, vars);
        //Macros.message(view, text.toString());
    } catch (Exception e) {
        Macros.message(view, "Exception: " + e.toString());
    }

Also, I don't recognize this Macros class - are you sure that you'd be
seeing what the exception was if your function blew up?  Could you
add:
        e.printStackTrace()

to the catch clause?

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users
Reply | Threaded
Open this post in threaded view
|

Re: Returning string arrays

Alexey Zinger
In reply to this post by Greg Allen-2
A little hard to tell from the code provided, but I think it may have to do with the scope of the method and the way it's being called.  Is it an instance method or a static one?  If it's an instance method, are you sure you're invoking it as such?  Also, there's a typo in the example: substituteTemplate in definition and substituteTemplatey in invocation.  And finally, how are you trying to use the result object?

Greg Allen <[hidden email]> wrote:
I have created a java class that contains a method that returns an array of strings:
 
    public String[] substituteTemplate (String template, Hashtable subs) throws java.io.IOException, java.io.FileNotFoundException {
            Vector v = new Vector<String>();
 
        … some code here ...
 
        String[] result = new String[v.size()];
        return result;
    }
 
I have placed the .jar file containing this class on the classpath and added it to my
beanshell script. I then try to call the method.
 
            try {
                text = substituteTemplatey(macroFile, vars);
                //Macros.message(view, text.toString());
            } catch (Exception e) {
                Macros.message(view, "Exception");
            }
 
The method appears to be called OK, but the return value can’t seem to be accessed. I get
a silent error and my script just ends. I’ve tried various things but can’t seem to access
the returned item.
 
Is there something special I have to do here? It seems pretty straightforward, but I can’t
get it to work.
 
Any suggestions?
 
Thanks,
 
-- Greg
 
 
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users



Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://azinger.blogspot.com
http://bsheet.sourceforge.net
http://wcollage.sourceforge.net


Ahhh...imagining that irresistible "new car" smell?
Check out new cars at Yahoo! Autos.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Beanshell-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/beanshell-users