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.
forloop, astitleMoviesis not set to any variable, theforloop will not execute and hence themoviesarray is null, which results in an error.prepare()method run first beforeaddItem()to have an array for each item in theaddItem()