CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2011
    Posts
    26

    Unhappy NullPointer Exception

    i'm making an array of Point objects........
    but getting the exception........




    Point[] p = new Point[corpsDestroyed];
    for(int j=ind+3,k=0; j<s.length(); j+=6,k++)
    {
    p[k].x = (int)(s.charAt(j)-48);
    p[k].y = (int)(s.charAt(j+2)-48);

    if(j == s.length() - 4)
    break;
    }




    here "s" is a string of no.s n char........from where i m taking the x & y components.......
    "ind+3"=index of the 1st no. in the string.........

  2. #2
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer Exception

    Code:
    Point[] p = new Point[corpsDestroyed];
    corpsDestroyed has to be an int

    Code:
    p[k].x = (int)(s.charAt(j)-48);
    what type is variable k?


    There are too many unknowns to answer your question accurately. My suggestion would be post all the variables/methods/constructs that this statement uses, if your sure it's this snippet that's causing your problems.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: NullPointer Exception

    As well as kolt007's comments please post the full exception message (it tells us what has gone wrong) and always post code in code tags.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Dec 2011
    Posts
    26

    Re: NullPointer Exception

    Code:
    import java.awt.Point;
    
    public class BananaCorp {
    
        public static double distance(Point p1, Point p2){
        
        double sqX = (p2.x - p1.x)*(p2.x - p1.x);
        double sqY = (p2.y - p1.y)*(p2.y - p1.y);
        double sum = sqX + sqY;
        double dist = Math.sqrt(sum);
        return dist; 
        }
        public static int index(String s){
            int count=0,ind=0;
        for(int i=0; i<s.length(); i++){
        if(s.charAt(i) == ':')
            count++;
        if(count == 3){
          ind = i;  break;
        }}return ind;
        }
        
        public static void getPoints(String s){
        
        int count=0;
        int corpsDestroyed=0; 
         int ind = index(s);    /*index of the 3rd ":"*/
            for(int i=0; i<s.length(); i++)
        {
        if(s.charAt(i) == ':')
            count++;
        if(count == 2)
        {
            if(ind == i+3){  
           corpsDestroyed = (s.charAt(i+1) - 48)*10 + (s.charAt(i+2) - 48);  /*-48 to convert ascii keys*/       
        }
            else corpsDestroyed = (s.charAt(i+1) - 48);
            System.out.println(corpsDestroyed);
            break;
        }}
        Point[] p = new Point[corpsDestroyed];
         for(int j=ind+3,k=0; j<s.length(); j+=6,k++) 
          {
            p[k].x = (int)(s.charAt(j)-48);
            p[k].y = (int)(s.charAt(j+2)-48);
                   
          if(j == s.length() - 4)
              break;
        }
        for(int l=0; l<corpsDestroyed; l++)
            System.out.print("("+p[l].x+","+p[l].y+")"+"\t");
        getDistances(p);
     }
        public static void getDistances(Point[] p)
        {  
          int size = 0;
          for(int k=0; k<p.length; k++)
              size = size + k;
          double[] d = new double[size];
          for(int i=0; i<p.length; i++)
          { 
            for(int j=i+1; j<p.length; j++)
            d[i] = distance(p[i], p[j]);    
          }
          for(int k=0; k<size; k++)
              System.out.print("\n"+d[k]+"\t");
          typeOfDistances(d);
        }
        
        public static void typeOfDistances(double[] d)
        {
         int count = 0;   
         for(int i=0; i<d.length; i++)
         { 
          for(int j=1; j<d.length; j++)
          {
           if(d[i] == d[j])
               continue;
           else
                count++;     
          }
         }
         System.out.println(count);
        }
        
        public static void equalDistances(double[] d)
        {
        
        }
        public static void main(String[] args) {
            // TODO code application logic here
            
            getPoints("{6:7:14:{{2,1},{6,6},{4,2},{2,5},{2,6},{2,7},{3,4},{6,1},{6,2},{2,3},{6,3},{6,4},{6,5},{6,7}}");
        }
    }

    Exception.....

    Code:
    14
    Exception in thread "main" java.lang.NullPointerException
    	at banana.corp.BananaCorp.getPoints(BananaCorp.java:51)
    	at banana.corp.BananaCorp.main(BananaCorp.java:100)
    Java Result: 1
    code where problem exist....is the one i posted earlier....

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: NullPointer Exception

    The problem is you have created a Point array but haven't filled it with any Point objects.

    You need to do something like:

    Code:
          Point[] p = new Point[corpsDestroyed];
         for(int j=ind+3,k=0; j<s.length(); j+=6,k++) 
            {
            p[k] = new Point((int)(s.charAt(j)-48), (int)(s.charAt(j+2)-48));
                   
            if(j == s.length() - 4)
                 break;
            }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Dec 2011
    Posts
    26

    Smile Re: NullPointer Exception

    The problem is you have created a Point array but haven't filled it with any Point objects.
    but that's what i'm doing in the following code.....

    Code:
    p[k].x = (int)(s.charAt(j)-48);
    p[k].y = (int)(s.charAt(j+2)-48);
    this is also a correct assignment.....Right??
    anyways thanks.....i'll try your code and reply......

  7. #7
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: NullPointer Exception

    Quote Originally Posted by darkmaster View Post
    but that's what i'm doing in the following code.....
    No, you are not doing what keang said. You created an array of Point with
    Code:
        Point[] p = new Point[corpsDestroyed];
    but that's just an array with all its elements == null. You still need to set the elements you intend to use to reference an instance of Point.

    In the for following that, you just attempt to use a Point element that is still null and that is why you get the NullPointerException, while if you used what keang suggested it would be creating a Point instance and at the same time assigning the values of x and y as you want.

  8. #8
    Join Date
    Dec 2011
    Posts
    26

    Re: NullPointer Exception

    thanks everyone....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured