android


java.lang.NullPointerException:Attempt to invoke virtual method on a null object reference


i'm working on an app where the user can display movie posters in a grid view and see movie details..my problem is that when i run the app and click on a poster i get this error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Long com.example.android.movies.Movies.getMposter_id()' on a null object reference
at this line id = movie.getMposter_id();
i don't know how can i solve it.
Sorry if I'm incompetent but I couldn't understand how to fix it. Thanks.
here is DetailsFragment.java
package com.example.android.movies;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.example.android.movies.R.id.favoriteButton;
import static com.example.android.movies.R.id.iv_poster;
/**
* A placeholder fragment containing a simple view.
*/
public class DetailsFragment extends Fragment{
Long id;
String title, overview, poster, date;
String vote;
ImageView posterIV;
TextView titleTV, dateTV, overviewTV, voteTV;
static Context mContext;
ExpandableListAdapter listAdapter;
static ExpandableListView expListView;
static List<String> listDataHeader;
static HashMap<String, List<Trailers>> listDataChild;
static ArrayList<Trailers> arrTrailers;
static ArrayList<Trailers> arrReviews;
ToggleButton favorite;
private MoviesDB db;
Trailers t;
public static DetailsFragment newInstance() {
return new DetailsFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
// get the listview
expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);
// receiving intent
Intent gIntent = getActivity().getIntent();
Movies movie = null;
if (gIntent != null && gIntent.hasExtra("movie")) {
movie = gIntent.getParcelableExtra("movie");
id = movie.getMposter_id();
title = movie.getMtitle();
titleTV = (TextView) rootView.findViewById(R.id.tv_title);
titleTV.setText(title);
date=movie.getMdate();
dateTV = (TextView) rootView.findViewById(R.id.tv_date);
dateTV.setText(date);
vote=String.valueOf(movie.getMvote());
voteTV= (TextView) rootView.findViewById(R.id.tv_vote);
voteTV.setText(vote);
overview=movie.getMoverview();
overviewTV = (TextView) rootView.findViewById(R.id.tv_overview);
overviewTV.setText(overview);
poster = movie.getMposter_path();
posterIV = (ImageView) rootView.findViewById(iv_poster);
Picasso.with(getActivity())
.load("http://image.tmdb.org/t/p/w185".concat(poster)).into(posterIV);
favorite= (ToggleButton) rootView.findViewById(favoriteButton);
}
favorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
db.insert(id,title,poster,date, Double.parseDouble(vote),overview,t.getContent1());
}
});
return rootView;
}
private void settingAdapter() {
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<Trailers>>();
// Adding child data
listDataHeader.add("Reviews");
listDataHeader.add("Trailers");
listDataChild.put(listDataHeader.get(0), arrReviews); // Header, Child data
listDataChild.put(listDataHeader.get(1), arrTrailers);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext=getActivity();
new TrailersFetcher(getActivity(), id,this).execute();
}
public void updateTrailers(ArrayList<Trailers> trailers){
arrTrailers = new ArrayList<>();
for (int i = 0; i <trailers.size() ; i++) {
arrTrailers.add(trailers.get(i));
}
new ReviewsFetcher(mContext, id,this).execute();
}
public void updateReviews(ArrayList<Trailers> reviews){
arrReviews = new ArrayList<>();
for (int i = 0; i <reviews.size() ; i++) {
arrReviews.add(reviews.get(i));
}
settingAdapter();
}
}
here is Movies.java
package com.example.android.movies;
import android.os.Parcel;
import android.os.Parcelable;
/**
*/
public class Movies implements Parcelable {
String Mposter_path;
Long Mposter_id;
String Mtitle;
String Moverview;
String Mdate;
Double Mvote;
Long id;
public Movies(String mposter_path, Long mposter_id, String mtitle,
String moverview, String mdate, Double mvote) {
Mposter_path = mposter_path;
Mposter_id = mposter_id;
Mtitle = mtitle;
Moverview = moverview;
Mdate = mdate;
Mvote = mvote;
}
public Movies() {
}
public String getMposter_path() {
return Mposter_path;
}
public Long getMposter_id() {return Mposter_id;}
public String getMtitle() {
return Mtitle;
}
public String getMoverview() {
return Moverview;
}
public String getMdate() {
return Mdate;
}
public Double getMvote() {
return Mvote;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.Mposter_path);
dest.writeValue(this.Mposter_id);
dest.writeString(this.Mtitle);
dest.writeString(this.Moverview);
dest.writeString(this.Mdate);
dest.writeValue(this.Mvote);
}
private Movies(Parcel in) {
this.Mposter_path = in.readString();
this.Mposter_id = (Long) in.readValue(Long.class.getClassLoader());
this.Mtitle = in.readString();
this.Moverview = in.readString();
this.Mdate = in.readString();
this.Mvote = (Double) in.readValue(Double.class.getClassLoader());
}
public static final Parcelable.Creator<Movies> CREATOR = new Parcelable.Creator<Movies>() {
#Override
public Movies createFromParcel(Parcel source) {
return new Movies(source);
}
#Override
public Movies[] newArray(int size) {
return new Movies[size];
}
};
}
MovieAdapter.java
package com.example.android.movies;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
/**
*
*/
public class MovieAdapter extends BaseAdapter{
ArrayList<Movies> MovieList=new ArrayList<Movies>();
Context mContext;
public MovieAdapter(Context context,ArrayList<Movies> movieArrayList) {
this.mContext=context;
this.MovieList=movieArrayList;
}
#Override
public int getCount() {
return MovieList == null ? 0 : MovieList.size();
}
#Override
public Object getItem(int position) {
return MovieList.get(position);
}
#Override
public long getItemId(int id) {return MovieList.get(id).getMposter_id(); }
#Override
public View getView(int position, View ConvertedView, ViewGroup viewGroup) {
View view=ConvertedView;
if (view==null)
{
LayoutInflater inflater=(LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.grid_item,null);
}
ImageView ivMovieName= (ImageView) view.findViewById(R.id.image_view);
Picasso.with(mContext)
.load("http://image.tmdb.org/t/p/w185"+MovieList.get(position).getMposter_path()).into(ivMovieName);
return view;
}
}
You may have a movie in your intent but the mPoster_id is null. You need to check that your movie has an mPoster_id that isn't null, or set it to a not null value by default.
Where is your activity code? Did you actually put this movie object to your activity's intent like getIntent().putExtra(movie)? Looks like there is no problem with Movie class.
Here is the working code I have tested:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
Movie movie;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView test = (TextView) findViewById(R.id.test);
movie = new Movie("Awesome", (long) 123);
String about = movie.getName() + "\n" + movie.getId();
test.setText(about);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getIntent().putExtra("movie", movie);
if (savedInstanceState == null)
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container_view, new DetailFragment())
.commit();
}
});
}
}
DetailFragment.java:
public class DetailFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_detail,container,false);
TextView textView = (TextView)root.findViewById(R.id.textView2);
Movie movie = getActivity().getIntent().getParcelableExtra("movie");
String about = movie.getName()+"\n"+movie.getId();
textView.setText(about);
return root;
}
}
Movie.java:
public class Movie implements Parcelable{
private Long id;
private String name;
public Movie(String n, Long i){
name = n;
id = i;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected Movie(Parcel in) {
this.name = in.readString();
this.id = (Long) in.readValue(Long.class.getClassLoader());
}
public static final Creator<Movie> CREATOR = new Creator<Movie>() {
#Override
public Movie createFromParcel(Parcel in) {
return new Movie(in);
}
#Override
public Movie[] newArray(int size) {
return new Movie[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(this.name);
parcel.writeValue(this.id);
}
}

Related Links

Android GridView clickable ImageView
Restoring SQLite DB file
How to set a background image for each list item in the navigation drawer.
FragmentTabhost no activity illegalstateexception error
Swipe to confirm layout in Android
Letter Spacing in EditText for Android
how to display image and description from database in Android
Eclipse is using Gingerbread style Form Widgets, Text Fields etc. instead of JellyBean style
How to change Android checkbox style
HttpURLConnection and JSONParser simultaneous connection
setOnClickListener on imageview needs to be clicked twice to work
How do I manage a stack of previous SearchView searches and access them via the Up button?
C++ and Android NDK interaction for COCOS2D-X
Android Facebook SDK SSO “the page you requested cannot be displayed”
Issue is “The specified child already has a parent. You must call removeView() on the child's parent first.”
Facebook SDK 3 for Android - Login using facebook app

Categories

HOME
service-worker
c++11
apache-jena
spring-data-redis
pclxl
static-site
xbee
soa
nested-if
jquery-ui
crystal-lang
bing-maps
cmdb
php-5.6
tput
eip
android-intent
null-pointer
feathersjs
smartclient
identity-management
symfony-console
articulate-storyline
guzzle
string-concatenation
exploit
ckfinder
dapper-extensions
corpus
angularjs-components
angular-translate
ceylon
openmdao
cppreference
bioinformatics
userdefaults
aws-codecommit
powershell-ise
deltaspike
jbutton
memory-address
fossil
installscript
reverse
roblox
docfx
openalpr
xdocreport
p4merge
mcustomscrollbar
comparison-operators
android-softkeyboard
monkey-testing
dynamic-linq
xbmc
builder
linter
cnc
opengl-4
excel-dna
universal-analytics
objectscript
reachability
pure-css
hadoop-2.7.2
preprocessor
dotnetnuke-7
file-uri
phpwebsocket
cortex-m
maybe
r-package
pypiserver
extjs-grid
atoi
alloy-ui
diff3
fits
shopizer
java-security
qtwebengine
pseudo-class
sip-server
appserver
tweenlite
gii
matrix-inverse
extjs2
getopt
bin
ie8-compatibility-mode
mvc-editor-templates
process-explorer
actiondispatch
sqlclr
qtconcurrent
jquery-tools
promotion-code
fsevents
massive
unattended-processing
object-database
blender-2.49
mathematical-notation
business-model
wcf-proxy

Resources

Encrypt Message