Search code examples
javaandroidfacebooknullpointerexceptionandroid-simple-facebook

Null pointer exception in Simple Facebook library to getPosts?


I imported THIS LIBRARY to get my all posts i set up all imports and so, I have follow the steps and wirte this code,Actually i wanted to show my page's all public posts in my app.like some apps or website keeps that is looks like facebook's real page. is it possible or not in android? thanks.

package algonation.com.myapplication;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sromku.simple.fb.SimpleFacebook;
import com.sromku.simple.fb.actions.Cursor;
import com.sromku.simple.fb.entities.Post;
import com.sromku.simple.fb.listeners.OnPostsListener;

import java.util.List;


public class FacebookActivity extends ActionBarActivity {
private final static String EXAMPLE = "";
private String mAllPages = "";
OnPostsListener onPostsListener = new OnPostsListener() {
    @Override
    public void onComplete(List<Post> posts) {
        Log.i(EXAMPLE, "Number of posts = " + posts.size());
    }

/*
 * You can override other methods here:
 * onThinking(), onFail(String reason), onException(Throwable throwable)
 */
};

private TextView mResult;
private Button mGetButton;
private TextView mMore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_facebook);
    mResult = (TextView) findViewById(R.id.result);
    mMore = (TextView) findViewById(R.id.load_more);
    mMore.setPaintFlags(mMore.getPaint().getFlags() | Paint.UNDERLINE_TEXT_FLAG);
    mGetButton = (Button) findViewById(R.id.button);
    mGetButton.setText(EXAMPLE);
    mGetButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mAllPages = "";
            mResult.setText(mAllPages);
            SimpleFacebook.getInstance().getPosts(new OnPostsListener() {
                @Override
                public void onThinking() {
                }

                @Override
                public void onException(Throwable throwable) {
                    mResult.setText(throwable.getMessage());
                }

                @Override
                public void onFail(String reason) {
                    mResult.setText(reason);
                }

                @Override
                public void onComplete(List<Post> response) {
                    // make the result more readable.
                    mAllPages += "<u>\u25B7\u25B7\u25B7 (paging) #" + getPageNum() + " \u25C1\u25C1\u25C1</u><br>";
                    mAllPages += com.sromku.simple.fb.utils.Utils.join(response.iterator(), "<br>", new com.sromku.simple.fb.utils.Utils.Process<Post>() {
                        @Override
                        public String process(Post post) {
                            return "\u25CF " + post.getMessage() == null || "null".equalsIgnoreCase(post.getMessage()) ? post.getId() : post.getMessage() + " \u25CF";
                        }
                    });
                    mAllPages += "<br>";
                    mResult.setText(Html.fromHtml(mAllPages));
                    // check if more pages exist
                    if (hasNext()) {
                        enableLoadMore(getCursor());
                    } else {
                        disableLoadMore();
                    }
                }
            });
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_facebook, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void enableLoadMore(final Cursor<List<Post>> cursor) {
    mMore.setVisibility(View.VISIBLE);
    mMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            mAllPages += "<br>";
            cursor.next();
        }
    });
}

private void disableLoadMore() {
    mMore.setOnClickListener(null);
    mMore.setVisibility(View.INVISIBLE);
}
}

Solution

  • If you look at the code of the library, you'll see this note:

    /**
     * Get the instance of {@link com.sromku.simple.fb.SimpleFacebook}. <br>
     * <br>
     * <b>Important:</b> Use this method only after you initialized this library
     * or by: {@link #initialize(android.app.Activity)} or by {@link #getInstance(android.app.Activity)}
     *
     * @return The {@link com.sromku.simple.fb.SimpleFacebook} instance
     */
    

    You should use SimpleFacebook.getInstance(FacebookActivity.this) because the method you're using will return an uninitialised reference (null), unless you have initialised it previously.

    Please spend some time reading the documentation of the library - the guy has clearly spent some time working on huge wiki.