0

I have a ScrollView, and I'm trying to add TextViews to the LinearLayout in it but i keep getting an error.

TextView l = new TextView(StopWatch.this);
l.setText("Didn't work");
l.setTextSize(20);
llLocations.addView(l);

I was trying to use this code to see if the ArrayList i was using was empty, but it wasnt that. It says the error is caused by a NullPointerException at...

llLocations.addView(l);

I have other code that does the same thing, and i tried comparing them but there wasn't a difference so i'm confused. How do i fix this?

07-10 21:14:42.687: E/AndroidRuntime(5241): FATAL EXCEPTION: main
07-10 21:14:42.687: E/AndroidRuntime(5241): java.lang.IllegalStateException: Could not execute method of the activity
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View$1.onClick(View.java:3599)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View.performClick(View.java:4204)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View$PerformClick.run(View.java:17355)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.os.Handler.handleCallback(Handler.java:725)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.os.Looper.loop(Looper.java:137)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at dalvik.system.NativeStart.main(Native Method)
07-10 21:14:42.687: E/AndroidRuntime(5241): Caused by: java.lang.reflect.InvocationTargetException
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 21:14:42.687: E/AndroidRuntime(5241):     at android.view.View$1.onClick(View.java:3594)
07-10 21:14:42.687: E/AndroidRuntime(5241):     ... 11 more
07-10 21:14:42.687: E/AndroidRuntime(5241): Caused by: java.lang.NullPointerException
07-10 21:14:42.687: E/AndroidRuntime(5241):     at com.TBJsoft.runprogress.StopWatch.done(StopWatch.java:506)
07-10 21:14:42.687: E/AndroidRuntime(5241):     ... 14 more

StopWath.java:506 is the

llLocations.addView(l);

line.

I'm trying to do this in an alert dialog that has a custom view with the scroll view in it. Do you think that could be causing the problem?

0

4 Answers 4

2

llLocations is null. Read up on findViewById. The end.

Sign up to request clarification or add additional context in comments.

3 Comments

Well, (s)he could always show the rest of the code where llLocations is initialized... anyway, maybe Dalvik just is allergic to hungarian notation
llLocations = (LinearLayout) findViewById(R.id.llLocations); I have this in the onCreate mehtod
Really? Now you tell me. Either the stack trace is your friend.
1

llLocations is null , so I guess you did something like that:

LinearLayout llLocations = (LinearLayout)findViewById(R.id.<here_the_id_of_the_linearleayout_in_your_xml_layout>);

If you did this, and the id is the correct one, and you setContentView of your activity with this layout which contains the linearlayout, then it should work.

Comments

0

normally you have to bind the TextView to an id! Like that:

example = findViewById(R.id.yourid);

For your own knowledge, R is dynamically generated by android out of your values-file(IDs are mostly assigned in the layout-files)

Comments

0

If you have a variable which is an instance of a class, you need to initialize it. If you do not initialize it, it will have a null pointer.

llocations was not initialized properly before you try to access its method. But you cannot access the method of a null pointer, right? It would be like shaking hands with nobody. So, this is an exceptional behavior and that is why the NullPointerException is thrown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.