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
Farouk Charkas
1,957 PointsThis is kind of urgent! Help if you can!
This is my code:
//This is the package name 'everingapplications.blue_jay'
package everingapplications.blue_jay;
//Importing the necessary
import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
//Importing the Wi-Fi files
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
//Importing the JSON files
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
//Importing ButterKnife
import butterknife.ButterKnife;
import butterknife.InjectView;
//Importing the Toast Widget
import static android.widget.Toast.LENGTH_LONG;
//Declaring the class
public class MainActivity extends AppCompatActivity {
//Declaring the TAG String
public static final String TAG = MainActivity.class.getSimpleName();
//Declaring CurrentWeather
private CurrentWeather mCurrentWeather;
//Inject views using ButterKnife
@InjectView(R.id.timeLabel)
TextView mTimeLabel;
@InjectView(R.id.temperatureLabel)
TextView mTemperatureLabel;
@InjectView(R.id.humidityValue)
TextView mHumidityValue;
@InjectView(R.id.precipValue)
TextView mPrecipValue;
@InjectView(R.id.summaryLabel)
TextView mSummaryLabel;
//What will happen
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
ImageButton advancedButton = (ImageButton) findViewById(R.id.advancedButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.advanced);
}
};
advancedButton.setOnClickListener(listener);
ImageButton imageButtonRip = (ImageButton) findViewById(R.id.imageButtonRip);
View.OnClickListener mamans = new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
};
imageButtonRip.setOnClickListener(mamans);
}
This is my logcat:
08-22 16:12:04.250 27577-27577/? I/art﹕ Not late-enabling -Xcheck:jni (already on)
08-22 16:12:04.557 27577-27577/everingapplications.blue_jay D/AndroidRuntime﹕ Shutting down VM
08-22 16:12:04.564 27577-27577/everingapplications.blue_jay E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: everingapplications.blue_jay, PID: 27577
java.lang.RuntimeException: Unable to start activity ComponentInfo{everingapplications.blue_jay/everingapplications.blue_jay.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at everingapplications.blue_jay.MainActivity.onCreate(MainActivity.java:90)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-22 16:12:07.933 27577-27577/everingapplications.blue_jay I/Process﹕ Sending signal. PID: 27577 SIG: 9
Please please help!
7 Answers
Steve Hunter
57,712 PointsHi there,
I've edited your post so that the code and Logcat are more readable; you used the backticks correctly, but they like to have a clear line around them too else they don't work too well - anyway; that's now fixed.
So, you've chucked a huge chunk of code in there. Are you unsure where the issue is? What have you tried to track down the problem? When does the problem occur; runtime, on a button click? Does the project build with no warnings? What did you last change from when the application worked previously? What happens when you run it? What did you expect to happen? Do you have a git-type of version control that you can share with us?
You've got a null pointer exception. It looks like line 90 inside of onCreate() is a starting point to look for that. Which line is that? It seems that you've set an onClickListener to listen to nothing/null. This may be a code thing or a connection issue. Have you tried adding a breakpoint near where the app fails to see what the current context looks like? Doing that allows you to see whether something you expect to be something does, in fact, contain null which is what seems to be happening here.
Apologies for the questions but you asked none. You gave us your code and your logcat - I'm assuming that the error in your logcat was unexpected so I've asked the questions above to commence a problem-solving thought-process.
Steve.
Farouk Charkas
1,957 PointsNo, no I meant that we were tackling the problem, turned out to be a false alarm. You werent negative/offensive at all!
Steve Hunter
57,712 Points:-) All cool.
I can't see the problem in your code snippet above. What error are you getting?
Steve.
Farouk Charkas
1,957 PointsSay, I could skip fragments, but I will need to know how to program Scroll Views.
Steve Hunter
57,712 PointsHave you tried Recycler Views? They're pretty intuitive. I don't know what you're creating so I'm not sure what would be the best to use. In truth, I probably wouldn't know - I'm no expert!
Farouk Charkas
1,957 PointsOk thank you, I have fixed the snippet! Have a nice day! And thanks again for bearing with me!
Steve Hunter
57,712 PointsAlways around to help as best I can, so just shout if you think I can. I'm no expert, though!
Best of luck with your project.
Farouk Charkas
1,957 PointsThank you!
Farouk Charkas
1,957 Pointswait, actually, in the code snippet, it is not letting me initialize the 'eatButtton' anywhere
Steve Hunter
57,712 PointsWhere is that declared and what error are you getting when you initialize it?
Farouk Charkas
1,957 PointsIn the OnCreate method
Steve Hunter
57,712 PointsThe top code snippet has an onCreate() method, the second doesn't, unless that is the onCreate()` code. What error are you getting?
It might be worth getting this into Github or Bitbucket so the whole project can be shared. They are very good ways of working collaboratively.
Farouk Charkas
1,957 PointsNo on the second snippet, I will paste that part
Farouk Charkas
1,957 PointsHere you go:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_main);
Button eatButton = (Button) findViewById(R.id.eatButton);
}
Random randomGeneratorDeath = new Random();
int deathNumber = randomGeneratorDeath.nextInt(3);
int lives = 3;
int buttonsClicked = 0;
Random randomGeneratorFood = new Random();
int foodNumber = randomGeneratorFood.nextInt(11);
View.OnClickListener eatListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (foodNumber != 0) {
lives = lives + 1;
foodNumber = foodNumber - 1;
buttonsClicked = buttonsClicked + 1;
} else {
setContentView(R.layout.bye_bye);
}
}
};
Farouk Charkas
1,957 PointsNever mind, I got it!
Steve Hunter
57,712 Points:-)
Farouk Charkas
1,957 PointsFarouk Charkas
1,957 PointsSorry for the absence of the question, you can lay your attack. Sorry again, but I have decided to work with Pagers and Fragments for my multiple pages! Though it is still crashing for my fragments.
Farouk Charkas
1,957 PointsFarouk Charkas
1,957 PointsI was working on this game and this is my code, I mean why is it crashing:
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsThere was no attack - I was merely establishing where we were at with your thought process and what had been identified/excluded already.
What issue are you having with your Fragments? It might be worth using something like Github or Bitbucket so you can share entire projects and seek solutions to the code. It's very hard to paste the relevant code in the Community pages as there can be so many linked files & resources.
Steve.