class to return an object array with member attributes that can be accessed from main class -
i'm new java, concepts , terminology fuzzy, i'm trying! need create class take data in string, parse , return object (array) member attributes can accessed main class. i've read better solution having multiple indexed arrays pointx[], pointy[], pointz[], etc.., if need perform operations swapping or sorting.
so, i'd access array object's members main test[0].x, test[100].y, etc. however, i'm frustratingly getting exception in thread "main" java.lang.nullpointerexception , don't understand how proceed.
here's how i'm calling parse main:
parse = new parse(); parse[] test = a.convert("1 2 3 4 1 2 3 4 1 2 3 4"); // <- ** error here ** system.out.printf("%.2f %.2f %.2f %d\n", test[0].x, test[0].y, test[0].z, test[0].r); here's parse class:
public class parse { parse[] point = new parse[1000]; public float x; public float y; public float z; public int r; parse() { } public parse[] convert(string vertices) { // parse string vertices -> object point[0].x = 10; // <- ** error here ** point[0].y = 100; point[0].z = 50; point[0].r = 5; return point; } } thanks in advance parse class & related java pointers continue learning java , enjoyment of programming!
when create array of parse objects array empty , doesn't contain objects, null references. need create objects , store them in array.
furthermore, point member of parse class when should local variable of convert method, should static, since doesn't rely on particular instance.
you invoke conversion follows:
parse[] test = parse.convert("this string not used yet"); system.out.printf("%.2f %.2f %.2f %d\n", test[0].x, test[0].y, test[0].z, test[0].r); here's parse class:
public class parse { public float x; public float y; public float z; public int r; parse() { } public static parse[] convert(string vertices) { // parse string vertices -> object parse[] point = new parse[1000]; point[0] = new parse(); point[0].x = 10; point[0].y = 100; point[0].z = 50; point[0].r = 5; return point; } }
Comments
Post a Comment