Results 1 to 12 of 12
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default array problem: simple (I'm sure), but defeating me

    I'm trying to do something very simple here: A small array, Point[]. It compiles fine, but why do I get a runtime error?

    Thanks.

    Java Code:
      private Point [] myPoints = new Point[3]; // declared in my class
          ...
          myPoints[0].setX(7); // in my class's constructor; this causes the NullPointerException
        ...
    
        private class Point{            // inner class of my class
            private double x, y;
            
            public Point(){
            }
            
            public void setX(double param){
                x = param;
            }
            public void setY(double param){
                y = param;
            }
            public double getX(){
                return x;
            }
            public double getY(){
                return y;
            }
        } // end of: private class Point

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    I think you forget filling arrays myPoints so when you try get element 0 you get null. Then you try invoke method setX() but this is null object and you have NPE.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can you show me all of the code? I am trying to re-create and test something but Im not sure how ou have your code set up.

    Try
    Java Code:
    myPoint[0] = new Point();
    myPoint[1] = new Point();
    myPoint[2] = new Point();

  4. #4
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    "All" of my code would be silly. Here is some of it:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ACanvas extends JFrame {
      private int frameWidth = 800, frameHeight = 600;
      private int length = 50, x= 30, y= 30;
      private JTextField messagesBox = new JTextField("messages here", 60);
      private JTextField input1 = new JTextField("input length here", 10);
      private JTextField input2 = new JTextField("input2", 10);
      private Point [] myPoints = new Point[3];
    
        public ACanvas()
        {
          int fWid = frameWidth, fHi = frameHeight;
          
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
          setSize(frameWidth, frameHeight);
          setLocationRelativeTo(null);
          exitButton.addActionListener(new ExitButtonWatcher());
          stopButton.addActionListener(new StopButtonWatcher());
          tryThisButton.addActionListener(new TryThisButtonWatcher());
          myPoints[0].setX(7);
        }
            
        public void paint(Graphics g){
            super.paint(g);
        }
        
        private class TryThisButtonWatcher implements ActionListener {
            public void actionPerformed(ActionEvent a) {
            }
        } // end of: private class TryThisButtonWatcher implements ActionListener
        
        private class StartButtonWatcher implements ActionListener {
            public void actionPerformed(ActionEvent e) {
            };
        } // end of: private class StartButtonWatcher implements ActionListener
       
        private class StopButtonWatcher implements ActionListener {
            public void actionPerformed(ActionEvent a) {
            }
        } // end of: private class StopButtonWatcher implements ActionListener
        
        private class ExitButtonWatcher implements ActionListener
        {
            public void actionPerformed(ActionEvent a)
            {
              System.exit(0);
            }
        }
        
        private class Point{
            private double x, y;
            
            public Point(){
            }
            
            public void setX(double param){
                x = param;
            }
            public void setY(double param){
                y = param;
            }
            public double getX(){
                return x;
            }
            public double getY(){
                return y;
            }
        } // end of: private class Point
    
    } // end of: public class ACanvas extends JFrame

  5. #5
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Try
    Java Code:
    myPoint[0] = new Point();
    myPoint[1] = new Point();
    myPoint[2] = new Point();
    I could try that, but what would I do if the array size was really big?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    use a loop
    Java Code:
    for(int i = 0; i < myPoint.length; i++){
      myPoint[i] = new Point();
    }

  7. #7
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    But I need my array (of myPoints) to be declared outside all methods, and you can't do this (say) +outside+ all methods:

    Java Code:
    private Point [] myPoints = new Point[100];
    for(int i = 0; i < myPoint.length; i++){
      myPoint[i] = new Point();
    }

  8. #8
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    But the array, myPoints[], needs to be declared +outside+ all methods. How could you fill the array at that stage of the code?

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Do it in your ACanvas() constructor. Note that your default constructor for Point will set x and y to 0. It would be better style to make that explicit in the Point() constructor.

    -Gary-

  10. #10
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    I put this above my ACanvas() constructor:

    Java Code:
    private Point [] myPoints;
    And I put this in my constructor:

    Java Code:
    int myPointsSize = 10;
    
    ...
    ...
    
    for(int i = 0; i < myPointsSize; i++){
       myPoints[i] = new Point(); // this line causes NullPointerException
    }
    And I still get a NullPointerException (NPE)

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    You have to initialize both the array, and the individual Point elements. You keep showing little bits of your code that keep changing, so it's impossible to tell exactly where you're going wrong. So here's a simplified Point class and PointDemo class (I don't want to get into any side issues involving ACanvas or JFrame) to demonstrate the principle:
    Java Code:
    public class PointDemo {
    	private static final int MY_POINTS_SIZE = 10;
    	private Point[] myPoints = new Point[MY_POINTS_SIZE];
    
    	public PointDemo() {
    		for (int i = 0; i < MY_POINTS_SIZE; i++) {
    			myPoints[i] = new Point();
    		}
    		myPoints[0].setX(7);
    	}
    	
    	public String toString() {
    		StringBuilder sb = new StringBuilder();
    		for (Point p : myPoints) {
    			sb.append(p + "\n");
    		}
    		return sb.toString();
    	}
    
    	public static void main(String[] args) {
    		System.out.println(new PointDemo());
    	}
    	
    	private class Point{
    		private double x, y;
    
    		public Point() {
    			this.x = 0;
    			this.y = 0;
    		}
    
    		public void setX(double param){
    			x = param;
    		}
    
    		public void setY(double param){
    			y = param;
    		}
    
    		public double getX(){
    			return x;
    		}
    
    		public double getY(){
    			return y;
    		}
    		
    		public String toString() {
    			return "x = " + x + "; y = " + y;
    		}
    	}
    }
    -Gary-

  12. #12
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

Similar Threads

  1. Simple Programming problem loop in array
    By katon in forum New To Java
    Replies: 1
    Last Post: 12-20-2009, 04:14 PM
  2. Simple array comparison
    By jigglywiggly in forum New To Java
    Replies: 11
    Last Post: 11-12-2009, 02:27 AM
  3. Two simple array problems.
    By DMiller in forum New To Java
    Replies: 4
    Last Post: 11-06-2009, 12:50 PM
  4. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 03:43 PM
  5. Simple array questions
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 06:57 AM

Posting Permissions

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