0

I am having trouble with returning string array on a client and server environment. The result I getting is nothing when I compiled the client application.

server application

public String[] getFlight() throws Exception {

     AvailableFlights todayFlight = new AvailableFlights();
    List<Flight> flights_today =  todayFlight.getFlightDetail();

    List<String> flights = new ArrayList<String>(); 

    try {
        flights_today = this.unmarshal(new File("Flights.xml"));

            for (Flight flight : flights_today) {
        String flightDetail = flight.getJourney() 
                    + " " + flight.getAirline() 
                    + " "+ String.valueOf(flight.getConnections())
                    + " "+ String.valueOf(flight.getCost())
                    + " "+ flight.getDestination()
                    + " "+ flight.getOrigin()
                    + " "+ String.valueOf(flight.getSeats()); 
                flights.add(flightDetail);
                System.out.println(flightDetail);
                }

      } catch (Exception e) {

      }

    return (String[]) flights.toArray();
 }

client java application

import org.me.kettravel.*;


public class JavaApplication5 {   

    public static void main(String[] args) {

        try {
    System.out.println(getFlight());
        } catch (Throwable ex) {          
        }   
    }

    private static java.util.List<java.lang.String> getFlight() throws Exception_Exception {
        org.me.kettravel.ReadFlightService service = new org.me.kettravel.ReadFlightService();
        org.me.kettravel.ReadFlight port = service.getReadFlightPort();
        return port.getFlight();
    }

Additionally I have tried a small experiment with "hello" like below on the server app and it worked fine, so I know that the web service is working fine but I just can't seem to pass/return the flights String array to the client app.

String i = "hello"; 
return i;

PS: When I try to run the server app with public static void main (String[] args) { constructor and without return, the app printed out the arraylist perfectly from unmarshalling xml convert it to arraylist and do system.out.print.

I would be grateful if anyone could shed some light as I am really stuck on this. Thanks.

04/01/2012 (19:16) - Adjustment has been made suggested by Genzer, the client app still not getting any response from server app.

04/01/2012 (23:24) - Adjustment has been made suggested by Bohemian can be seen below, the client app is now getting an error checking javax.xml.ws.soap.SOAPFaultException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

public static void main(String[] args) {
    try {
        Object obj = getFlight();
System.out.println(obj);
    } catch (Throwable ex) { 
        System.out.println(ex);
    }   
}

06/01/2013 (16:20) - I have just addressed my mistake as the XML file was empty from tests to tests, however it is now have data in the xml file. I have just created a test class to see if readFlight returns anything to a class that it's in a same project/source package. Result is a success... really running out of ideas =/ as I have tested the web service by sending a simple hello string over to client app and worked no problem.

test class public class test {

public static void main(String[] args) {

readFlight rF = new readFlight();

    try {
System.out.println(rF.getFlight());
    } catch (Throwable ex) { 
        System.out.println(ex);
    }       
}

}

Output from the test class: [London to Amsterdam KLM 1 200.0 Amsterdam London 100, London to Kuala Lumper Malaysia Airline 1 750.0 Kuala Lumper London 100, London to Manchester British Airway 1 50.0 Manchester London 56]

10/01/2013 (18:13) - PROBLEM SOLVED. You have to give full directory to the unmarshall file. Example: C:/Users/User/Documents/NetBeansProjects/WebService/booking.xml

4
  • what does the code block starting with String[] flights = new String[] { and ending with for (String s: flights) do? Commented Jan 4, 2013 at 15:58
  • I guess is to print the array string for myself to check it to see if its working right or not. So without the String s: flights and print it out, I will get this " [Ljava.lang.String;@312a47fe [Ljava.lang.String;@4edc8de9 [Ljava.lang.String;@71e8e471" Commented Jan 4, 2013 at 16:03
  • Your code compiled? Since the method getFlight() is declared to return a String, but you are returning flights which is a String[]. Commented Jan 4, 2013 at 16:15
  • Ye the code compiled fine. Commented Jan 4, 2013 at 16:44

3 Answers 3

1

The problem is that you have two different variables named flights. You populate one and return the other.

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

3 Comments

Ah but when I remove the public declaration "public static String[] flights;" a redline comes up on the "return flights;". But didn't I already declared in the body of getFlight? As you can see "String[] flights = new String[]" why doesn't the return pick that up D:
@Ket: return doesn't pick that up because by that time the second variable is out of scope.
Like you said the return is out of the scope. So I guess I have to use the public static one, and I've changed to "String[] flights = {nextFlight.getJourney() etc... } " but for some reason the flight doesn't pick up the public one which I have declared on the top "public static String[] flights = new String[]{};"
0

You could remove public static String[] flights and modify the method like this:

public List<String> getFlight() throws Exception {

    Flight nextFlight = new Flight();
    AvailableFlights todayFlight = new AvailableFlights();
    List<Flight> flights_today =  todayFlight.getFlightDetail();

    // Since you you List for Flight, why not here
    List<String> flights = new ArrayList<String>(); 

    try {
        flights_today = readFlight.unmarshal(new File("Flights.xml"));

        for (Flight flight : flights_today) {
            String flightDetail = flight.getJourney() 
                    + " " + flight.getAirline() 
                    + " "+ String.valueOf(flight.getConnections())
                    + " "+ String.valueOf(flight.getCost())
                    + " "+ flight.getDestination()
                    + " "+ flight.getOrigin()
                    + " "+ String.valueOf(flight.getSeats());
            flights.add(flightDetail);
        }
      } catch (Exception e) {

      }

    return flights;
 }

2 Comments

Client side returned nothing, not even the "[]"
Could this be causing the problem? stackoverflow.com/questions/6609770/…
0

You have committed a "no no", which may be hiding the problem:

catch (Exception e) {
}

You should never (well, rarely) catch Exception. Especially when your catch block is empty.

There could be an unchecked exception, like NullPointerException, being thrown within your loop, but you wouldn't know.

Try removing the catch and leaving only soecific Exceptions (if any) that are declared to be thrown.

If one of the method is declared as throwing Exception, then at the very least, you should do this:

catch (Exception e) {
    System.out.println(e);
}

7 Comments

Thanks for the input, I have tried to put "System.out.println(e);", refreshed the web service reference on client app, re-add the reference and still nothing. Also I have inserted a Throwable print on client app, now when I compiled it, the app say "javax.xml.ws.soap.SOAPFaultException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;", does it have any meaning to anyone?
That error means you are getting an Object[] returned, not a String[]. Try accepting a Object[] at the client at examine its contents to see what's going on
Not sure if I have done it right, please check on the edited post. I am still getting the same message.
how about you modify the getFlight to return List<String> instead of String[], see my editted answer. I doubt the exception caused by this reason.
Still returns "[]". I just made a quick java class within the same web service source package with readFlight and the new java class still can't get any returns from readFlight. In the new class I have declared every classes in the project and imported everything.
|

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.