2015-06-09 78 views
-7

請幫忙 即時通訊新手爲Android 即時通訊嘗試使用此代碼與微調解析器json解析,但我有一個錯誤消息「java.lang.NullPointerException」,請幫助修復我的代碼..json解析器到微調器

@SuppressWarnings("deprecation") 
public class evaluasi extends Activity{ 

InputStream is = null; 
String result = null; 
String line = null; 
JSONObject jsonobject; 
JSONArray jsonarray; 
ArrayList<String> worldlist; 

@Override 
protected void onCreate (Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.lay_evaluasi); 

    /*parsing ke broker*/ 
    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.androidbegin.com/tutorial/jsonparsetutorial.txt"); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
     Log.e("Pass 1", "Sucsess.."); 
    }catch (Exception e){ 
     Log.e("Fail 1", e.toString()); 
     Toast.makeText(getApplicationContext(), "Invalid IP Address", Toast.LENGTH_SHORT).show(); 
    } 

    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     while((line = reader.readLine()) !=null) 
     { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
     Log.e("Pass 2", "Connection Sucess"); 
    }catch (Exception e){ 
     Log.e("Fail 2", "Failed Connection"); 
    } 

    try{ 

     worldlist =new ArrayList<String>(); 
     jsonarray = jsonobject.getJSONArray("worldpopulation"); 
     for (int i = 0; i < jsonarray.length(); i++) { 
      worldlist.add(jsonobject.optString("country")); 
     } 

    Spinner spinbro = (Spinner) findViewById(R.id.spinbrokced); 
    spinbro.setAdapter(new ArrayAdapter<String>(evaluasi.this, 
      android.R.layout.simple_spinner_dropdown_item, 
      worldlist)); 

    }catch (Exception e){ 
     Log.e("Fail 3", e.toString()); 
     Toast.makeText(getApplicationContext(), "cant load data", Toast.LENGTH_SHORT).show(); 
    } 

} 
     //end parsin broker 
+0

這款C已解決 – atata

回答

0

你好,你已經做了聲明jsonobject但是你有沒有初始化它使你的代碼應該是...

try{ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
    StringBuilder sb = new StringBuilder(); 
    while((line = reader.readLine()) !=null) 
    { 
     sb.append(line + "\n"); 
    } 
    is.close(); 
    result = sb.toString(); 
    // your object should be initialized like this 

jsonobject= new JSONObject(result); 
      Log.e("Pass 2", "Connection Sucess"); 
     }catch (Exception e){ 
      Log.e("Fail 2", "Failed Connection"); 
     } 
+0

yess已經解決了,謝謝! :) – atata