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 trialVictor Cuc
7,281 PointsNullPointerException for setLayoutManager, even if I updated build.gradle dependencies
I updated all the dependencies and it does't seem to be working. Changed @Bind to @BindView, but apparently there still are problems.
Really don't understand why there is a null object. Isn't lm initialised as a new StaggeredGridLayoutManager just at the beginning at the populate() method?
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.teamtreehouse.albumcover.AlbumListActivity.populate(AlbumListActivity.java:59)
at com.teamtreehouse.albumcover.AlbumListActivity.onCreate(AlbumListActivity.java:27)
private void populate() {
StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mAlbumList.setLayoutManager(lm);
This is the complete exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jimulabs.googlemusicmock, PID: 4432
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jimulabs.googlemusicmock/com.teamtreehouse.albumcover.AlbumListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.teamtreehouse.albumcover.AlbumListActivity.populate(AlbumListActivity.java:59)
at com.teamtreehouse.albumcover.AlbumListActivity.onCreate(AlbumListActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
AlbumListActivity:
public class AlbumListActivity extends Activity {
@BindView(R.id.album_list) RecyclerView mAlbumList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_album_list);
initTransitions();
ButterKnife.bind(this);
populate();
}
private void initTransitions() {
getWindow().setExitTransition(null);
getWindow().setReenterTransition(null);
}
interface OnVHClickedListener {
void onVHClicked(AlbumVH vh);
}
static class AlbumVH extends RecyclerView.ViewHolder implements View.OnClickListener {
private final OnVHClickedListener mListener;
@BindView(R.id.album_art)
ImageView albumArt;
public AlbumVH(View itemView, OnVHClickedListener listener) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
mListener = listener;
}
@Override
public void onClick(View v) {
mListener.onVHClicked(this);
}
}
private void populate() {
StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
mAlbumList.setLayoutManager(lm);
final int[] albumArts = {
R.drawable.mean_something_kinder_than_wolves,
R.drawable.cylinders_chris_zabriskie,
R.drawable.broken_distance_sutro,
R.drawable.playing_with_scratches_ruckus_roboticus,
R.drawable.keep_it_together_guster,
R.drawable.the_carpenter_avett_brothers,
R.drawable.please_sondre_lerche,
R.drawable.direct_to_video_chris_zabriskie };
RecyclerView.Adapter adapter = new RecyclerView.Adapter<AlbumVH>() {
@Override
public AlbumVH onCreateViewHolder(ViewGroup parent, int viewType) {
View albumView = getLayoutInflater().inflate(R.layout.album_grid_item, parent, false);
return new AlbumVH(albumView, new OnVHClickedListener() {
@Override
public void onVHClicked(AlbumVH vh) {
int albumArtResId = albumArts[vh.getLayoutPosition() % albumArts.length];
Intent intent = new Intent(AlbumListActivity.this, AlbumDetailActivity.class);
intent.putExtra(AlbumDetailActivity.EXTRA_ALBUM_ART_RESID, albumArtResId);
startActivity(intent);
}
});
}
@Override
public void onBindViewHolder(AlbumVH holder, int position) {
holder.albumArt.setImageResource(albumArts[position % albumArts.length]);
}
@Override
public int getItemCount() {
return albumArts.length * 4;
}
};
mAlbumList.setAdapter(adapter);
}
}
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.jimulabs.googlemusicmock"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:recyclerview-v7:27.1.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife:8.8.1'
compile 'com.jimulabs.mirrorsandbox:mirror-sandbox:0.2.1'
compile 'com.android.support:palette-v7:27.1.0'
}
Victor Cuc
7,281 PointsBen Jakuben is there a problem with how I am using butterknife? I barely changed the starter files...
2 Answers
Victor Cuc
7,281 PointsFINALLY figured it out!
If anyone else is having this issue, and probably is, you now need to also add the annotationProcessor with butterknife-compiler!
Add this:
dependencies {
implementation 'com.jakewharton:butterknife:8.8.1' //8.8.1 is the latest version atm
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
Most importantly, check the README on the ButterKnife Github page! It is the most up to date and tells you exactly how to implement the latest version.
https://github.com/JakeWharton/butterknife
Btw I love how this whole post I've been talking to myself
Nataly Rifold
12,432 PointsHi Victor Cuc Thank you!! It works now!!! or I hope it does
the build app gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.jimulabs.googlemusicmock"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'InvalidPackage'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.jakewharton:butterknife:7.0.1'
implementation 'com.jimulabs.mirrorsandbox:mirror-sandbox:0.2.1'
implementation 'com.android.support:palette-v7:27.1.1'
implementation 'com.jakewharton:butterknife:8.8.1' //8.8.1 is the latest version atm
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
the build modle gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
Victor Cuc
7,281 PointsVictor Cuc
7,281 PointsI think I should initialise mAlbumList as that is null, but how should I do that exactly?