0
I’m creating a Activity
on Android, whose code is here, and I’m getting a NullPointerException
near the line
final TextView txtResult = new TextView(this);
I got the following dump from the stack:
05-07 09:12:57.180: E/AndroidRuntime(887): FATAL EXCEPTION: main
05-07 09:12:57.180: E/AndroidRuntime(887): Process: br.com.colorcalc, PID: 887
05-07 09:12:57.180: E/AndroidRuntime(887): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.colorcalc/br.com.colorcalc.Calculator}: java.lang.NullPointerException
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.os.Looper.loop(Looper.java:137)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:515)
05-07 09:12:57.180: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-07 09:12:57.180: E/AndroidRuntime(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-07 09:12:57.180: E/AndroidRuntime(887): at dalvik.system.NativeStart.main(Native Method)
05-07 09:12:57.180: E/AndroidRuntime(887): Caused by: java.lang.NullPointerException
05-07 09:12:57.180: E/AndroidRuntime(887): at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.view.View.<init>(View.java:3429)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.view.View.<init>(View.java:3496)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.widget.TextView.<init>(TextView.java:622)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.widget.TextView.<init>(TextView.java:617)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.widget.TextView.<init>(TextView.java:613)
05-07 09:12:57.180: E/AndroidRuntime(887): at br.com.colorcalc.Calculator.<init>(Calculator.java:26)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.Class.newInstanceImpl(Native Method)
05-07 09:12:57.180: E/AndroidRuntime(887): at java.lang.Class.newInstance(Class.java:1208)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-07 09:12:57.180: E/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
05-07 09:12:57.180: E/AndroidRuntime(887): ... 11 more
Does it have something to do with my method onCreate
?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtResult = (TextView) findViewById(R.id.txtResult);
btnCalc.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
result = "#" + rst1 + remainderR + rst2 + remainderG + rst3 + remainderB;
txtResult.setText(result);
}
});
}
final
means that you are declaring a constant instead of a variable, so you can only assign the value to thetxtResult
in your creation. About NPE, you have Textview calledtxtResult
in your xml?– Math
Try it like this:
final TextView txtResult = new TextView(ATuaActivity.this);
– Jorge B.
I do have Math. I even used it to show the result of an equation. I will include the line in the main post.
– Leonardo Saldanha
Jorge, even with the change the problem continued.
– Leonardo Saldanha
If I understand your dump,
br.com.colorcalc.Calculator
is yourActivity
, correct? The<init>
means the code is running in the constructor. You’re actually doing all of this inside the constructorActivity
or did I get something wrong? If you are creating the views inside the constructor, the concept is wrong, you should create the views (manually, or through the methodsetContentView
) within theonCreate
ofActivity
, or another similar place.– carlosrafaelgn
Yes, Calculator is my Activity. At the beginning of the project, I was creating my code inside onCreate, but I had a double "" error that according to what I found in the English OS, can happen when a very large code is inserted inside it. Therefore, I chose to create the code within another method, object or void.
– Leonardo Saldanha
So, @leo.Mind you, I don’t know a limit for lines of code within a method. Maybe your error was from a
String
very long? The correct is to even leave the creation of the views inside theonCreate
. If you can, send a larger example of the code, even if by link.– carlosrafaelgn
Do you know any site to upload code so I can give you the link?
– Leonardo Saldanha
How many lines do you have? If you don’t have many (<= 200), you can ask the question. Otherwise, I know the http://pastebin.com/ where you can paste codes to share. But put the link in the question, too.
– carlosrafaelgn
You are here: http://pastebin.com/sjT80S2P
– Leonardo Saldanha
Just remember to format the question at the end of the discussion to avoid external links. And try to ask a succinct question that is useful to future visitors and does not escape from the original question. @leo.Saddle, I recommend reading two links: How to ask a good question? and How to create a Minimum, Complete and Verifiable example
– Math