0

I'm very new to android development as I just took a class of it only now, so I'm very confused with my current situation as I am writing the codes using the references I currently have at the moment.

I have also been trying to use references from other sources, though, sadly, I can't really comprehend how those really works.

My current assignment is that I have to make an application that serves as a catalog for movies and tv shows using fragments, and the following is the codes of one of the fragments:

public class MovieFragment extends Fragment {

    View view;
    private String[] titleMovie;
    private String[] descMovie;
    private TypedArray posterMovie;
    private String[] genreMovie;
    private String[] castMovie;
    private String[] duration;
    private String[] directorMovie;
    private MovieAdapter adapter;
    private ArrayList<Movie> movies;
    private RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.mv_fragment,container,false);
        recyclerView = view.findViewById(R.id.mv_list);
        adapter = new MovieAdapter(getContext(), movies);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()) {});
        recyclerView.setAdapter(adapter);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prepare();
        addItem();
    }

    private void prepare() {
        titleMovie = getResources().getStringArray(R.array.name_mv);
        descMovie = getResources().getStringArray(R.array.desc_mv);
        posterMovie = getResources().obtainTypedArray(R.array.poster_mv);
        genreMovie = getResources().getStringArray(R.array.genre_mv);
        castMovie = getResources().getStringArray(R.array.cast_mv);
        directorMovie = getResources().getStringArray(R.array.director_mv);
        duration = getResources().getStringArray(R.array.duration);
    }

    private void addItem() {
        movies = new ArrayList<>();

        for (int i = 0; i < titleMovie.length; i++){
            Movie movie = new Movie();
            movie.setTitleMovie(titleMovie[i]);
            movie.setDescMovie(descMovie[i]);
            movie.setPosterMovie(posterMovie.getResourceId(i,-1));
            movie.setGenreMovie(genreMovie[i]);
            movie.setDuration(duration[i]);
            movie.setDirectorMovie(directorMovie[i]);
            movie.setCastMovie(castMovie[i]);
            movies.add(movie);
        }
        adapter.setMovie(movies);
    }

}

And when I try to run the application from the emulator provided in Android Studio, I got the following error:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.wisnu_1605450.utsmobpro, PID: 18250
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.wisnu_1605450.utsmobpro.MovieAdapter.setMovie(java.util.ArrayList)' on a null object reference
    at com.wisnu_1605450.utsmobpro.MovieFragment.addItem(MovieFragment.java:74)
    at com.wisnu_1605450.utsmobpro.MovieFragment.onCreate(MovieFragment.java:47)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2586)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:838)
    at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1197)
    at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1080)
    at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:119)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)

Any explanation on why I screwed up is very much appreciated, will also give more of the codes if necessary for the context.

2
  • This is because in your for loop, as titleMovies is not set to any variable, the for loop will not execute and hence the movies array is null, which results in an error. Commented Nov 5, 2019 at 2:38
  • I had the prepare() method run first before addItem() to have an array for each item in the addItem() Commented Nov 5, 2019 at 6:23

2 Answers 2

1

From the Fragment Life cycle documentation, onCreate event will be called before onCreateView event. That mean when you call adapter.setMovie(movies), the adapter is not created. It'll cause a NullPointerException.

You should call addItem in onViewCreated or onStart event.

Sign up to request clarification or add additional context in comments.

Comments

0

OnCreate() method should be called before onCreateView() and after that onViewCreated() will be called. Whereas in your code prepare(),addItem() methods are called in onCreate() so that instance not create for your MovieAdapter().please remove prepare(),addItem() methods in onCreate() and place it OnViewCreated()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.