Null Pointer Exception in array passed class -
so have project requires generic class extends number , finds largest , smallest value in array, average of values, , size of array. seems easy enough implement, have problem before putting generic part of in place, runtime error of null pointer exception @ x.length, regardless of method call, in same place.
import java.util.comparator; public class test { public int x[]; public test(int x[]) { } public void setx(int newx[]) { x = newx; } public int[] getx() { return x; } public int findsmallest() { int = 0; int temp = x[i]; while (i < x.length) { i++; if(x[i] < temp) { temp = x[i]; } else { } } return temp; } public int findlargest() { int = 0; int temp = x[i]; while (i < x.length) { i++; if(x[i] > temp) { temp = x[i]; } else { } } return temp; } public double findmean() { int = 0; double sum = 0.0; double avg = 0.0; while (i < x.length) { sum += x[i]; i++; } avg = sum / x.length; return avg; } public int findtotal() { int = x.length; return i; } public static void main (string args[]) { int[] ia = {1, 2, 3, 4, 5, 6}; test inttest = new test(ia); system.out.println(inttest.findtotal()); } } on how fix amazing.
you forgot use setx method in constructor. you're passing integer array constructor not initializing integer array inside class. can calling setx method in constructor , passing integer array x setx method. hope helps.
Comments
Post a Comment