Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Josh Gold
12,207 PointsNull pointer exception in Recommendations App, Google Play Services project
Logcat details: FATAL EXCEPTION: main java.lang.NullPointerException at com.joshbgold.recommendations.ListingAdapter.onBindViewHolder(ListingAdapter.java:43) com.joshbgold.recommendations.ListingAdapter.onBindViewHolder(ListingAdapter.java:21)
Here is my ListingAdapter.java:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.joshbgold.recommendations.model.ActiveListings;
import com.joshbgold.recommendations.model.Listing;
import com.squareup.picasso.Picasso;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class ListingAdapter extends RecyclerView.Adapter<ListingAdapter.ListingHolder>
implements Callback<ActiveListings>{
private MainActivity activity;
private LayoutInflater inflater;
private ActiveListings activeListings;
public ListingAdapter(MainActivity activity){
this.activity = activity;
inflater = LayoutInflater.from(activity);
}
@Override
public ListingHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ListingHolder(inflater.inflate(R.layout.layout_listing, viewGroup, false));
}
@Override
public void onBindViewHolder(ListingHolder listingHolder, int i) {
final Listing listing = activeListings.results[i];
listingHolder.titleView.setText(listing.title);
listingHolder.titleView.setText(listing.price);
listingHolder.titleView.setText(listing.Shop.shop_name);
Picasso.with((listingHolder.imageView.getContext()))
.load(listing.Images[0].url_570xN)
.into(listingHolder.imageView);
}
@Override
public int getItemCount() {
if(activeListings == null) {
return 0;
}
if (activeListings.results == null){
return 0;
}
return activeListings.results.length;
}
@Override
public void success(ActiveListings activeListings, Response response) {
this.activeListings = activeListings;
notifyDataSetChanged();
this.activity.showList();
}
@Override
public void failure(RetrofitError error) {
this.activity.showError();
}
public ActiveListings getActiveListings(){
return activeListings;
}
public class ListingHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public TextView titleView;
public TextView shopNameView;
public TextView priceView;
public ListingHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.listing_image);
titleView = (TextView) itemView.findViewById(R.id.listing_title);
shopNameView = (TextView) itemView.findViewById(R.id.listing_shop_name);
priceView = (TextView) itemView.findViewById(R.id.listing_price);
}
}
}
Here is my activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
<ProgressBar
style="@style/Widget.AppCompat.ProgressBar"
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:id="@+id/error_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
And here is layout_listing.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/listing_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/listing_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sample title"/>
<TextView
android:id="@+id/listing_shop_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/listing_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="sample shop name"/>
<TextView
android:id="@+id/listing_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/listing_title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="sample price"/>
</RelativeLayout>
</LinearLayout>
Shop.java
import android.os.Parcel;
import android.os.Parcelable;
public class Shop implements Parcelable {
public int shop_id;
public String shop_name;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(shop_id);
dest.writeString(shop_name);
}
public static final Creator<Shop> CREATOR = new Creator<Shop>() {
@Override
public Shop createFromParcel(Parcel source) {
Shop shop = new Shop();
shop.shop_id = source.readInt();
shop.shop_name = source.readString();
return shop;
}
@Override
public Shop[] newArray(int size) {
return new Shop[size];
}
};
}
Listing.java
import android.os.Parcel;
import android.os.Parcelable;
public class Listing implements Parcelable {
public int listing_id;
public String title;
public String description;
public String price;
public String url;
public Shop Shop;
public Image[] Images;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(listing_id);
dest.writeString(title);
dest.writeString(description);
dest.writeString(price);
dest.writeString(url);
dest.writeParcelable(Shop, 0);
dest.writeParcelableArray(Images, 0);
}
public static final Creator<Listing> CREATOR = new Creator<Listing>() {
@Override
public Listing createFromParcel(Parcel source) {
Listing listing = new Listing();
listing.listing_id = source.readInt();
listing.title = source.readString();
listing.description = source.readString();
listing.price = source.readString();
listing.url = source.readString();
listing.Shop = source.readParcelable(Shop.class.getClassLoader());
listing.Images = (Image[]) source.readParcelableArray(Image.class.getClassLoader());
return listing;
}
@Override
public Listing[] newArray(int size) {
return new Listing[size];
}
};
}
3 Answers
Test Test
21,581 Pointspublic static void getActiveListings(Callback<ActiveListings> callback) { getApi().activeListings("Images,Shop", callback); }
PS: you are not supposed to have white spaces in the String "Images,Shop".
Darnell Cephus
4,208 Pointsomg all over a space??!!!?? this is worse than forgetting a semicolon lol.
This actually worked! I was going through crap trying to figure out how the shop name should appear after JavaLangNullPointer errors...thanks!
James Simshaw
28,738 PointsHello,
Without seeing everything included in the Listing class, I cannot be certain, but the fact that listing.Shop.shop_name has a capital in it sends up red flags for myself since something being capitalized tends to indicate that it is a class. This could be as simple as changing Shop to shop or even removing it alltogether. To further help you beyond this, we would need to see your Listing model definition.
Josh Gold
12,207 PointsShop is supposed to be a class. I will post Shop class code and Listing class code as well, and hopefully that will help.
James Simshaw
28,738 PointsI would still recommend changing
public Shop Shop;
to
public Shop shop;
Afterwards, you should update all of the references to follow the same lowercasing. I'd also repeat the same for Images into images.
At the very least, this will make things less confusing when reading through the code trying to figure out if you're referring to the shop variable or the Shop class.
Another thing that you might want to try is casting the result of readParcelable to a Shop object:
listing.Shop = source.readParcelable(Shop.class.getClassLoader());
to
listing.shop = (Shop) source.readParcelable(Shop.class.getClassLoader());
Josh Gold
12,207 PointsHello James,
I did a refactor for the Shop member variable to mShop. I agree that definitely makes things less confusing and makes the code more readable!
When I cast the result of readParcelable to a Shop object, Android Studio says that doing so I redundant. Doesn't hurt anything though.
Nevertheless still getting same null pointer exception. I will run in debug mode to verify that Shop.shop_name is the item that is actually the thing that is null.
Josh Gold
12,207 PointsOk after setting a breakpoint and running debug mode, I see that mShop (which is an instance of a Shop object) is null for some reason. I'm looking at the Etsy API documentation to figure out why I am not getting a Shop object, so that I can access all the shop properties (i.e .shop_name in this case).
Curtis Cook
8,264 PointsJosh Gold where you ever able to find what was causing the error? I'm getting the same exception and on the same lines as well.
Josh Gold
12,207 PointsJosh Gold
12,207 PointsThe logcat indicates there is a problem at line 43 of ListingAdapter.java. FYI this is line 43:
listingHolder.titleView.setText(listing.Shop.shop_name);Maybe I have used the wrong ID or name to set the text?