Friday, 15 June 2012

java - Calling the constructor of a class through its own main method and initializing variables -



java - Calling the constructor of a class through its own main method and initializing variables -

this question has reply here:

what null pointer exception, , how prepare it? 12 answers

i new java , have origin doing exercises friend sent me. exercise in question asks create class term terms of polynomial, , class polynomial itself, polynomial represented fixed size array , values of fields provided. have written next code:

class term { int coeff; int exponent; } public class polynomial { static int size=5; term[] poly; public polynomial() { poly = new term[size]; for(int i=0;i<size;i++) { poly[i].coeff=0; poly[i].exponent=0; } } public static void main(string args[]) throws exception { polynomial p = new polynomial(); } }

and maintain getting next exception:

exception in thread "main" java.lang.nullpointerexception @ prac.polynomial.<init>(polynomial.java:25) @ prac.polynomial.main(polynomial.java:34)

please help me doing wrong here.

array elements object array nullby default. create sure initialized before attempting access fields

for (int = 0; < size; i++) { poly[i] = new term(); ... }

java exception exception-handling nullpointerexception

No comments:

Post a Comment