开发者

Error in loop of sax parser

开发者 https://www.devze.com 2023-02-21 21:38 出处:网络
I\'m trying to use a SAX parser to parse a HTTP response. Everything seems to be working correctly, except that the program isn\'t entering my for loop. Here is my code:

I'm trying to use a SAX parser to parse a HTTP response. Everything seems to be working correctly, except that the program isn't entering my for loop. Here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.playerinfo);

    TableLayout t1 = (TableLayout) findViewById(R.id.myTable);

    Intent i = getIntent();
    Bundle b = i.getExtras();
    final String str = b.getString("ARRIVING_FROM");

    d=d+"";
    d = tryLogin(str);
    System.out.println("Value of D"+d.substring(0, 1)); 

    this.tryLogin(str);

    final ScoreList scorelist = XMLHandler.scorelist ;

    id = new TextView[scorelist.getName().size()];
    name = new TextView[scorelist.getName().size()];

    for (int j = 0; j < scorelist.getName().size(); j++) {
        TableRow tr = new TableRow(this);

        id[j]= new TextView(this);
        id[j].setText(scorelist.getId().get(j));
        id[j].setTextColor(Color.WHITE);
        id[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr.addView(id[j]);

        name[j]= new TextView(this);
        name[j].setText(scorelist.getName().get(j));
        name[j].setTextC开发者_如何学Pythonolor(Color.WHITE);
        name[j].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        tr.addView(name[j]);

        t1.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    }
}

protected String tryLogin(String str) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://ip address/test/players_detail.php?id="+str);
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
    nvps.add(new BasicNameValuePair("id", str));

    try  {
        UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
        httppost.setEntity(p_entity);
        HttpResponse response = client.execute(httppost);
        Log.v("MyPlayerInfo", response.getStatusLine().toString());
        HttpEntity responseEntity = response.getEntity();
        InputStream in=responseEntity.getContent();
        byte[] bData = new byte[1024];
        in.read(bData);
        System.out.println("In Data"+in.toString());
        String st=new String (bData);
        d=st;
        System.out.println("Response String from server"+st);
        Log.v("MyPlayerInfo", "Set response to responseEntity");

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        System.out.println("Response from server"+responseEntity);
        XMLHandler myXMLHandler = new XMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(retrieveInputStream(responseEntity));

        System.out.println("Server Response"+responseEntity);
        return d;
    } catch(Exception e) {
        Log.i("Catch","Exception generate in Post"+e);
        e.printStackTrace();
    }

    return "0";
}

private InputSource retrieveInputStream(HttpEntity httpEntity) {
    InputSource insrc = (InputSource) httpEntity;

    try {
        insrc = new InputSource(httpEntity.getContent());
    } catch (Exception e) {}

    return insrc;
}

And the logcat response is:

04-01 12:04:21.746: ERROR/AndroidRuntime(808): FATAL EXCEPTION: main
04-01 12:04:21.746: ERROR/AndroidRuntime(808): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example/android.example.MyPlayerInfo}: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.os.Looper.loop(Looper.java:123)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at dalvik.system.NativeStart.main(Native Method)
04-01 12:04:21.746: ERROR/AndroidRuntime(808): Caused by: java.lang.NullPointerException
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.example.MyPlayerInfo.onCreate(MyPlayerInfo.java:67)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 12:04:21.746: ERROR/AndroidRuntime(808):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-01 12:04:21.756: WARN/ActivityManager(59):   Force finishing activity android.example/.MyPlayerInfo
04-01 12:04:21.766: WARN/ActivityManager(59):   Force finishing activity android.example/.MyParsingExample
04-01 12:04:22.266: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{43ff6188 android.example/.MyPlayerInfo}


Okay, so you've got a NullPointerException at line 67 of MyPlayerInfo.java. Look at that line, work out why you're getting the exception, and fix it.

Without knowing which line is line 67, it's tricky to help further - you've presented a lot of code to look at, and we don't know where it's breaking, but you should.

(As an aside, you should be closing the input stream in a finally block, and only catching more specific exceptions, but one thing at a time...)

0

精彩评论

暂无评论...
验证码 换一张
取 消