Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
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:
bash-2.05b$ java TestArrys      
Length=5
0 10 20 30 40

Length=12
3 5 9 11 15 18 22 23 30 31 35 39 null null null null null null null null null nullbash-2.05b$
bash-2.05b$


After changing  1 line (I will leave it to you to find out what of your Arrys constructor we get:

Code:
bash-2.05b$ javac Arrys.java
bash-2.05b$ java TestArrys
Length=5
0 10 20 30 40

Length=12
3 5 9 11 15 18 22 23 30 31 35 39 [I@82ba41 [I@923e30 [I@130c19b [I@1f6a7b9 [I@7d772e [I@11b86e7 [I@35ce36 [I@757aef [I@d9f9c3 [I@9cab16bash-2.05b$


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho