2016-04-24 129 views
1

我試圖將我的應用程序與社交網絡(Facebook)集成在一起,並且需要一些幫助。我正在嘗試獲取我的Feed。當我運行該應用程序時,出現致命異常錯誤。Android。嘗試在空對象引用上調用虛擬方法'int java.lang.String.length()'

FATAL EXCEPTION: main 
Process: com.example.vladzakharo.facebookapp, PID: 2428 
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference 
    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) 
    at org.json.JSONTokener.nextValue(JSONTokener.java:94) 
    at org.json.JSONObject.<init>(JSONObject.java:156) 
    at org.json.JSONObject.<init>(JSONObject.java:173) 
    at com.example.vladzakharo.facebookapp.CentralActivity$2.onCompleted(CentralActivity.java:88) 
    at com.facebook.GraphRequest$5.run(GraphRequest.java:1379) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

所以,這是我的課

public class CentralActivity extends AppCompatActivity { 

Button btnPublish; 
ListView listView; 
public ArrayList<HashMap<String, String>> list; 
public String Message; 
public String Name; 
public String Caption; 
public String Description; 
public String Picture; 
public String Link; 
public String Id; 

public static final String MESSAGE = "message"; 
public static final String NAME = "name"; 
public static final String CAPTION = "caption"; 
public static final String DESCRIPTION = "description"; 
public static final String PICTURE = "picture"; 
public static final String LINK = "link"; 
public static final String ID = "id"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getApplicationContext()); 
    setContentView(R.layout.activity_central); 

    list = new ArrayList<>(); 
    listView = (ListView) findViewById(R.id.listView); 
    btnPublish = (Button) findViewById(R.id.btnPublish); 
    btnPublish.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Publish(); 
     } 
    }); 

    getFeed(); 
} 

private void Publish(){ 
    Intent goPublish = new Intent(CentralActivity.this, PublishActivity.class); 
    startActivity(goPublish); 
} 

protected void getFeed(){ 
    new GraphRequest(
      AccessToken.getCurrentAccessToken(), 
      "me/feed?fields=message,name,caption,description,picture,link", 
      null, 
      HttpMethod.GET, 
      new GraphRequest.Callback() { 
       @Override 
       public void onCompleted(GraphResponse response) { 
        JSONObject json; 
        try{ 
         json = new JSONObject(response.getRawResponse()); 
         JSONArray jArray = json.getJSONArray("data"); 
         for(int i = 0; i < jArray.length(); i++){ 
          Message = jArray.getJSONObject(i).get("message").toString(); 
          Name = jArray.getJSONObject(i).get("name").toString(); 
          Caption = jArray.getJSONObject(i).get("caption").toString(); 
          Description = jArray.getJSONObject(i).get("description").toString(); 
          Picture = jArray.getJSONObject(i).get("picture").toString(); 
          Link = jArray.getJSONObject(i).get("link").toString(); 
          Id = jArray.getJSONObject(i).get("id").toString(); 

          HashMap<String, String> hm = new HashMap<>(); 
          hm.put(MESSAGE, Message); 
          hm.put(NAME, Name); 
          hm.put(CAPTION, Caption); 
          hm.put(DESCRIPTION, Description); 
          hm.put(PICTURE, Picture); 
          hm.put(LINK, Link); 
          hm.put(ID, Id); 
          list.add(hm); 

         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
    ).executeAsync(); 
    ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list_item, 
      new String[]{MESSAGE, NAME, CAPTION, DESCRIPTION }, 
      new int[]{R.id.text1, R.id.text2, R.id.text3, R.id.text4}); 
    listView.setAdapter(adapter); 
} 

}

回答

0

你應該看看線88(參見Stasktrace: at com.example.vladzakharo.facebookapp.CentralActivity$2.onCompleted(CentralActivity.java:88) ,你得到的字符串之一:

jArray.getJSONObject(i).get("xxx") 

可能返回null,從而導致NPE。

也可能是

jArray.getJSONObject(i) 

返回null。

相關問題