1

I've got a strange problem with a try/catch block I'm using. I've got this method, which just gets some data from a remote service and stores it.

public WFSGetCapabilitiesResponse wfsGetCapabilities(String url) {
    WFSGetCapabilitiesResponse response = new WFSGetCapabilitiesResponse();

    try {
        WFSDataStore data = loadWFSCapabilities(url);
        String[] typeNames = data.getTypeNames();
        ArrayList<WFSFeatureTypeBase> wfsLayers = new ArrayList<WFSFeatureTypeBase>();
        for (int i = 0; i < typeNames.length; i++) {
            String typeName = typeNames[i];

            WFSFeatureTypeBase newLayer = new WFSFeatureTypeBase();
            newLayer.setTypeName(typeName);
            newLayer.setName(typeName.split(":")[1]);
            newLayer.setTitle(data.getFeatureTypeTitle(typeName));
            newLayer.setAbstract(data.getFeatureTypeAbstract(typeName));
            newLayer.setServiceUrl(url.split("\\?")[0]);

            wfsLayers.add(newLayer);
        }
        response.setWFSLayers(wfsLayers);
    } catch (IOException e) {
        response.setError(WCSCapabilitiesResponse.IO_EXCEPTION);
        response.setErrorMessage(e.getMessage());
        response.setSuccessful(false);
        e.printStackTrace();
        return response;
    }
    return response;
}

If I run with this code, I get a null pointer exception ('data' is null, but don't really know if it's relevant). However, if I remove the return statement from my catch block everything is fine.

The strange thing is, the IOException is not being caught in either case, so I can't see why it's having such an impact.

So again, with the return in the catch block it doesn't work, without it it does... I really can't see why this would happen.

Any ideas?

DeadPassive
  • 877
  • 3
  • 8
  • 22
  • Where are you trying to access data from? (when its null) – RMT Jun 23 '11 at 11:27
  • Define "it works" .... this could mean that you returned an object of some kind, that you didn't log an exception, or that you have some business case this method handles that occurs perfectly. – maple_shaft Jun 23 '11 at 11:29

1 Answers1

0

Well, my first thought is that the catch block can only make a difference if an IOException is thrown - so we know for sure that that's happening. And likewise we know that without the catch block, this exception will propagate out of the wfsGetCapabilities method (presumably you added an appropriate throws declaration when you removed the catch block).

So chances are, in the "working" case, the calling code is catching the IOException higher up and handling it in such a way that your method appears to work judging by the output. Of course your method didn't work, and threw an exception, but perhaps there's a default fallback (or similar) which is invoked in an error case?

So that's the "strange" part hopefully dealt with. As for the "problem" part, if you're getting a NullPointerException thrown with the catch block, but not without it, it's clear that your catch block logic is not quite right and is causing this problem. From what you've posted it doesn't look like any of those immediate references would be null - take a look at the stack trace and determine from which line the exception is thrown, then fix it as you would any other NPE.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228