0

在检索值时,我无法检索值。为什么我在下面给出的代码中得到 IOexcetion?处理程序有问题,但我无法弄清楚。

public SitesList opCodeRequest(String code)
    {
        SitesList objsitelist =null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet method= new HttpGet();
            method.setURI(new URI(AppConstatnt.OPCODE_URL+code));

            HttpResponse response=client.execute(method);           
            HttpEntity entity=response.getEntity();         
            InputStream stream=entity.getContent();

            Log.e("b4 execute in login","aditya");

            SAXParserFactory spf = SAXParserFactory.newInstance(); 
            SAXParser sp = spf.newSAXParser(); 
            XMLReader xr = sp.getXMLReader(); 
            OPCodeHandler myopcodeHandler = new OPCodeHandler();
            xr.setContentHandler(myopcodeHandler); 

            xr.parse(new InputSource(stream)); 
            objsitelist = myopcodeHandler.getSiteList();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();//error
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return objsitelist;


    }

处理程序

public class OPCodeHandler extends DefaultHandler {

    boolean currentElement = false;
    String currentValue = "";
    SitesList siteobject;

    public SitesList getSiteList() {
        return siteobject;
    }

    public void setSiteList(SitesList objsite) {
        siteobject = objsite;
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        currentElement = true;
        currentValue = "";
        if (localName.equalsIgnoreCase("opcode")) {
            siteobject = new SitesList();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = new String(ch, start, length);
            currentElement = false;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        currentElement = false;
        if (localName.equalsIgnoreCase("tsip")) {
            siteobject.setTsip(currentValue);
        }
        if (localName.equalsIgnoreCase("ssip")) {
            siteobject.setSsip(currentValue);
        }
        if (localName.equalsIgnoreCase("brand")) {
            siteobject.setBrand(currentValue);
        }
        if (localName.equalsIgnoreCase("balance")) {
            siteobject.setBalance(currentValue);
        }
        if (localName.equalsIgnoreCase("rates")) {
            siteobject.setRates(currentValue);
        }

        if (localName.equalsIgnoreCase("domain")) {
            siteobject.setDomain(currentValue);
        }
        if (localName.equalsIgnoreCase("operator")) {
            siteobject.setOperator(currentValue);
        }
        if (localName.equalsIgnoreCase("active")) {
            siteobject.setActive(currentValue);
        }
        if (localName.equalsIgnoreCase("opcode")) {
            setSiteList(siteobject);
        }

    }

}

我正在检索值的活动。

@Override
        protected void onPostExecute(SitesList result) 
        {
            if(this.objprogress.isShowing())
            {
                this.objprogress.dismiss();
            }

            tsip = result.getTsip();
            ssip = result.getSsip();
            brand = result.getBrand(); 
            balance = result.getBalance();
            rates = result.getRates();
            domain = result.getDomain();
            operator = result.getOperator();
            active = result.getActive();
        }

日志猫:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.sample.test.SampleAppActivity$Communicator.onPostExecute(SampleAppActivity.java:161)
at com.sample.test.SampleAppActivity$Communicator.onPostExecute(SampleAppActivity.java:1)

at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:130)

at android.app.ActivityThread.main(ActivityThread.java:3687)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:507)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

你得到一个空指针异常。这意味着一个变量或某些东西在它不应该是空的地方。我认为首先,您应该尝试正确初始化所有变量。

例如:

SitesList objsitelist =null;

应该像这样初始化:

SitesList objsitelist =new SitesList();

如果您已完成此操作,请回复,但仍然无法正常工作。

于 2012-09-07T07:11:19.367 回答