Reputation: 917
I have the following code that creates some views dynamically:
for (int i = 0; i < z; i++) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.tag_table_row, null); //this is the template for each view
// set xyz dynamically
TextView textView = (TextView) v.findViewById(R.id.xyz); //id within the layout
textView.setText(xyz[i]);
//set score dynamically
TextView textView2 = (TextView) v.findViewById(R.id.score);
textView2.setText(scores[i]);
//set bar graph dynamically
TagBar tagBar = (TagBar) findViewById(R.id.tagbar);
double tag1score = Double.parseDouble(scores[i]);
Log.i("score", scores[i]);
double factor = 250;
double len = tag1score*factor;
int length = (int) len;
Log.i("length", Integer.toString(length));
tagBar.setBarLength(length);
// insert into main view
View insertPoint = findViewById(R.id.tagTable);
((ViewGroup) insertPoint).addView(v, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
TagBar
is a class I created that basically draws a horizontal bar with a length that is determined by the "score". All of this code works fine, except for the line tagBar.setBarLength(length);
. When this line runs, I get a NullPointerException
and the app crashes.
As far as I know, length
is defined and is not null, tagBar
is defined and is not null, and the setBarLength()
method is defined within the TagBar
class. Why am I getting a NullPointerException`?
setBarLength()
is defined in the TagBar
class like so:
public void setBarLength(int n) {
barLeng = n;
invalidate();
requestLayout();
}
Here's my Logcat:
07-20 12:33:52.029: E/AndroidRuntime(8075): FATAL EXCEPTION: main
07-20 12:33:52.029: E/AndroidRuntime(8075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tforan.blobtag4/com.tforan.blobtag4.PlaceActivity}: java.lang.NullPointerException
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1852)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1873)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.os.Looper.loop(Looper.java:150)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.ActivityThread.main(ActivityThread.java:4358)
07-20 12:33:52.029: E/AndroidRuntime(8075): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 12:33:52.029: E/AndroidRuntime(8075): at java.lang.reflect.Method.invoke(Method.java:507)
07-20 12:33:52.029: E/AndroidRuntime(8075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-20 12:33:52.029: E/AndroidRuntime(8075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-20 12:33:52.029: E/AndroidRuntime(8075): at dalvik.system.NativeStart.main(Native Method)
07-20 12:33:52.029: E/AndroidRuntime(8075): Caused by: java.lang.NullPointerException
07-20 12:33:52.029: E/AndroidRuntime(8075): at com.tforan.blobtag4.PlaceActivity.onCreate(PlaceActivity.java:169)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
07-20 12:33:52.029: E/AndroidRuntime(8075): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
07-20 12:33:52.029: E/AndroidRuntime(8075): ... 11 more
Any help would be greatly appreciated. Thanks!
Upvotes: 1
Views: 91
Reputation: 14199
check here TagBar tagBar = (TagBar) findViewById(R.id.tagbar);
you are getting java.lang.NullPointerException
because your tagBar
object is null !
Upvotes: 2
Reputation: 93726
A null pointer exception is generated when you dereference a null pointer- when something to the left of a . is null. So the only thing that could cause it is tagbar being null, assuming you have the right line. I'd make sure that the id you're searching for is actually in your xml. If it really is, then I'd run it under a debugger.
Edit: did you mean to search for the tagbar in the activity's view, rather than in the view you inflate in the loop? That seems likely to be the bug. All the other finds are v.findViewById, this one is just findViewById.
Upvotes: 4