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
Sayed Ahmad Behbehani
3,360 PointsTrying to create array of Points
Hello there, I'm trying to create array of points, but for some reason I get this error message.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Point.set(int, int)' on a null object reference at absoft.test.MainActivity.onCreate(MainActivity.java:31)
This is an example of what I'm trying to do.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
Point[] point = new Point[5];
TextView view = new TextView(this);
point[0].set(1, 5);
point[1].set(2,4);
point[2].set(5,3);
point[3].set(3,2);
point[4].set(1,3);
linearLayout.addView(view);
}
First of all I'm curious, is it possible to create array of Point? I'm trying to create graph using some calculations that are already done in the program. I'm not sure whether I'm going on the right track or not. I tried graphView API, but it looks like it doesn't support loops. So do you recommend any solution?
Thanks, Sayed
1 Answer
Ben Deitch
Treehouse TeacherYep, you can create an array of pretty much anything. You're issue is that the default values for an array of Objects (i.e. non-primitives) is null. So you're array is: [null, null, null, null, null]. And when you call set() on null, you get a Null Pointer Exception.
Try this:
point[0] = new Point();
point[0].set(1, 5);