| ||||||
Urgent Problem Posted by rickysri (rickysri), 13 September 2006 I have created a Java Program named Array.java and a test class named TestArray.java.I have a problem in using the accessor in the TestArray class...I get a java.lang.nullpointerexception...Can someone help me This is urgent!!!! TEST CLASS import java.util.Collections; public class TestArrys { public static void main(String[] args) { int[] ary1 = new int[5]; System.out.print("Length="+ary1.length+"\n"); for (int j=0;j<=40;j=j+10) { ary1[j/10]=j; System.out.print(" " +ary1[j/10]); } int[] ary2= {3,5,9,11,15,18,22,23,30,31,35,39}; System.out.print("\n\nLength="+ary2.length+"\n"); for (int element:ary2) { System.out.print(" " +element); } Arrys myArrayObject[]=new Arrys[10]; for (int i=0;i<myArrayObject.length;i++) { myArrayObject[i]=new Arrys(); System.out.print(" "+myArrayObject[i].getNums()); } // for (int k=0;k<=40;k=k+10) // { // myArrayObject[k].setNums(ary1); // } // for(int element:ary2) // { // myArrayObject[element].setNums(ary2); // System.out.print(myArrayObject[element].getNums()); // } } } import java.lang.*; public class Arrys { int[] nums; int[] args; //convert args to integer using the Integer wrapper class public Arrys()//constructor { int[] nums=new int[100]; for (int n=0;n< 100;n++) { nums[n]=n; } } public int[] getNums() //accessor { return nums; } public void setNums(int[] temp) //mutator { nums=temp; for (int k=0;k<temp.length;k++) { nums[k]=k; } }//end main }//end class Posted by admin (Graham Ellis), 14 September 2006 Rick, we can't do "urgent" here ... see the FAQ. As it's some 8 hours since you posted, I suspect that you've already found a solution elsewhere or the boat has been missed. However, if the question is still live, please post up the complete exception message so that we can see more of where it occurred and not just the type of exception.From a very brief scan of your code I didn't spot anything obvious, but you might to well to reduce the problem to a few lines - that would at least help us help you, and at best help you to isolate the problem yourself ... all the "ary2" stuff appears to be irellevant to the question, as does the chunk of code that's commented out. Posted by Custard (Custard), 14 September 2006 Hi,Looks like you are doing some coursework learning Java? Output from the code as you posted it: Code:
After changing 1 line (I will leave it to you to find out what of your Arrys constructor we get: Code:
Which looks like it is displaying the raw references to an array returned by getNums(). Good luck! This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
| ||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |