Results 1 to 3 of 3
  1. #1
    Aggror is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default NullPointerException error

    Hello,

    I have 2 classes: Point and Hook. The Point class has 2 fields: x and y.
    In the Hook class I can create an array of Points. However I get a NullPointerException error when assigning a value to this x value via the Hook class.

    here is the Point class:
    Java Code:
    public class point{
            int x;
            int y;
    
            //default constructor
            point(){
                x = 0;
                y = 0;
            }
    here is the hook class:
    Java Code:
    public class Hook{
            //declare array
            point array1[];
    
            //make the default constructor
            Hook(){
                //make a new array existing out of points from the point class.
                array1 = new point [3];
                array1[0].x = 90;
    
            }
    This line "array1[0].x = 90;" gives me the exception error.

    According to this exception 'array1[0].x ' does not exist. Can someone explain to me what is going wrong here?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    An array declaration creates an array of references, but, until you assign a value to these references, they all point to null. You need to add an array1[index] = new point(); before you start assigning the x and y values at that index.

  3. #3
    Aggror is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Ah, so obvious now that you explained it to me. Thank you very much for a quick and accurate response

Similar Threads

  1. java.lang.NullPointerException Error
    By Manfizy in forum NetBeans
    Replies: 3
    Last Post: 02-24-2011, 06:27 AM
  2. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  3. awt TextField nullPointerException error
    By k2k in forum AWT / Swing
    Replies: 3
    Last Post: 02-24-2009, 04:24 AM
  4. ERROR: nullPointerException in applet
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:11 AM
  5. ERROR: nullPointerException
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 06:54 AM

Tags for this Thread

Posting Permissions

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