2

I have this class now working fine, but I've been struggling with it many hours to end of changing in it on a different logic from my first approach.

 public class MyClass {
        public static MyClass tablas;
        public static String[] GROUPS;
        public static String[] ESTATUS
        public static String[] CLIENTS;


        public void init(){
            this.tablas = new MyClass();

            this.setGroups();

            CLIENTS=this.setAny("/servlets/CLIENTS","page_rows","nombre");

            ESTADO_PEDIDO= new String[]{"str1","str2","str3","str4","str5"};
        }

        private String[] setAny(String sevlet,String bigNode,String smallNode){
            String[] ret=null;
            HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("operation", "4");
            parameters.put("avance", "0");
            InputStream is = Connection.con.processRequest("GET", sevlet, parameters);
            Document dom = null;
            try {
                dom = UtilesDom.parseXML(is);
                NodeList lines = dom.getElementsByTagName(bigNode);
                Element el = (Element)lines.item(0);
                NodeList nlist = el.getElementsByTagName(smallNode);
                ret = new String[nlist.getLength()];
                for (int i = 0; i < nlist.getLength(); i++) {
                    ret[i] = nlist.item(i).getTextContent();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            return ret;
        }

         private void setGroups(){
            HashMap<String, String> parameters = new HashMap<String, String>();
            parameters.put("operation", "4");
            parameters.put("avance", "0");
            InputStream is = Connection.con.processRequest("GET", "/servlets/GROUPS_CLIENTS", parameters);
            Document dom = null;
            try {
                dom = UtilesDom.parseXML(is);
                NodeList lines = dom.getElementsByTagName("lines");
                Element el = (Element)lines.item(0);
                NodeList nlist = el.getElementsByTagName("GROUP");
                GROUPS = new String[nlist.getLength()];
                for (int i = 0; i < nlist.getLength(); i++) {
                    GROUPS[i] = nlist.item(i).getTextContent();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
        }

As you can see there is two similar methods setGroups and setAny these are used to fill the Strings[] on top.setGroups was my original method but when I needed different Strings[] thought that a "less hard-coded" and most flexible method would be nice, so I tried this:

private void setAny(String sevlet,String bigNode,String smallNode,String[] which){
        HashMap<String, String> parameters = new HashMap<String, String>();
        parameters.put("operation", "4");
        parameters.put("avance", "0");
        InputStream is = Connection.con.processRequest("GET", sevlet, parameters);
        Document dom = null;
        try {
            dom = UtilesDom.parseXML(is);
            NodeList lines = dom.getElementsByTagName(bigNode);
            Element el = (Element)lines.item(0);
            NodeList nlist = el.getElementsByTagName(smallNode);

            which = new String[nlist.getLength()];
            for (int i = 0; i < nlist.getLength(); i++) {
                which[i] = nlist.item(i).getTextContent();
                System.out.println(which[i]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

Using the call like:

this.setAny("/principal/Clientes","page_rows","nombre",CLIENTS);

also

this.setAny("/principal/Clientes","page_rows","nombre",this.CLIENTS);

and

this.setAny("/principal/Clientes","page_rows","nombre",this.tablas.CLIENTS);

The problem with it is that the String[] passed as parameter (aka CLIENTS) just stay null , and yes at the en of the for loop its populated properly and the console shows what it supposed to.So the question is:

Why String[] CLIENTS cant be populated when passed as a parameter,and just stay as null?

PD: As you may notice English is not my language so please suggest any grammar/redaction/spelling... corrections.

2
  • 1
    "The problem...is that the String[] passed as parameter (aka CLIENTS) just stay null" No, the problem is that all of those calls will fail to compile, because setAny doesn't accept a String[] in the fourth parameter. It accepts a String. It's true that unless you assign to it, CLIENTS will be null. Also, if setAny did have a String[] parameter, nothing setAny can do with that parameter will let it set the value of CLIENTS if you pass CLIENTS for that parameter. Java is purely pass-by-value, methods cannot reach out and change the variables used to pass them values. Commented Jul 27, 2016 at 8:38
  • CLIENTS is static, don't use this to access it, prefer MyClass.CLIENTS Commented Jul 27, 2016 at 8:40

2 Answers 2

1

Okay so I'm gonna pretend your parameter is a String[] and not a String here.

Your problem is that once you create a new array with the new operator, your reference changes to that new array. So the old one isn't affected.

So yes, you create a new array and fill it properly, but sadly it won't be CLIENTS. If you do it like in your first example and return the String Array to save it, that will work.

Another option would be to create a static HashMap of String Arrays instead of just three different static String Arrays. Then you can pass the key to the method and just replace the Array at the given key. That way you don't need to work with a return value.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice catch, it was String[], already edited, but I also using new in setGroups() and there is working, why is that?
Because you actually use GROUPS in there. You don't pass it as a parameter or create a local variable that points to GROUPS. If you just used the name GROUPS in the other method it would work, too, you just wouldn't be able to use any of the others anymore.
0

It is null at runtime and the compile dont know about that. It strictly checks the type at compile time it self.

 setAny(String sevlet,String bigNode,String smallNode)

That last parameter is a String and you are trying to pass an Array. Probably you need to change the signature as

 setAny(String sevlet,String bigNode,String smallNode[])

So that it receives an array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.