Results 1 to 5 of 5
  1. #1
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Drawing Rectangles - NullPointerExceptions

    Hi. I have a problem with java.lang.NullPointerException.
    This is my program.

    class: GUI.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JToolBar;
    import javax.swing.SwingUtilities;
    
    public class GUI {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				DrawFrame frame = new DrawFrame();
    				frame.showGUI();
    			}
    		});
    	}
    }
    
    class DrawFrame extends JFrame {
    	MainPanel mainPanel;
    	
    	public DrawFrame() {
    		MainPanel mainPanel = new MainPanel();
    		Container content = getContentPane();
    		content.add(mainPanel);
    	}
    	
    	public void showGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setPreferredSize(new Dimension(500,500));
    		pack();
    		setVisible(true);
    	}
    }
    
    class MainPanel extends JPanel {
    	DrawPanel drawPanel = new DrawPanel();
    	JToolBar toolBar = new JToolBar();
    	
    	public MainPanel() {
    		add(toolBar, BorderLayout.NORTH);
    		add(drawPanel, BorderLayout.CENTER);
    	}
    }
    
    class DrawPanel extends JPanel {
    	public DrawPanel() {
    		
    	}
    	
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Rectangle rect = new Rectangle(80,80,70,89);
    		rect.drawGeomObj(g);
    	}
    }
    class: Rectangle.java
    Java Code:
    import java.awt.Point;
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    
    public class Rectangle {
    	public Rectangle(Point p1, Point p2) {
    		leftUpPoint = p1;
    		width = (int)(Math.abs(p1.x - p2.x));
    		height = (int)(Math.abs(p1.y - p2.y));
    	}
    	
    	public Rectangle(int x, int y, int width, int height) {
    		leftUpPoint.x = x;
    		leftUpPoint.y = y;
    		this.width = width;
    		this.height = height;
    	}
    	
    	public void drawGeomObj(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D) g;
    		g2.drawOval(leftUpPoint.x, leftUpPoint.y, width, height);
    	}
    	
    	public boolean containPoint(Point point) {
    		if(leftUpPoint.x < point.x && (leftUpPoint.x+width > point.x)
    			&& (leftUpPoint.y < point.y) && (leftUpPoint.y + height) > point.y)
    				return true;
    		
    		return false;
    	}
    	
    	public boolean isPointOnBorder(Point point) {
    		Rectangle interiorRect = new Rectangle(leftUpPoint.x - toleranceInPixels, leftUpPoint.y - toleranceInPixels,
    				width - toleranceInPixels, height - toleranceInPixels);
    		Rectangle exteriorRect = new Rectangle(leftUpPoint.x + toleranceInPixels, leftUpPoint.y + toleranceInPixels,
    				width + toleranceInPixels, height + toleranceInPixels);
    		
    		if(!interiorRect.containPoint(point) && exteriorRect.containPoint(point))
    			return true;
    		
    		return false;
    	}
    
    	Point leftUpPoint;
    	int width, height;
    	int toleranceInPixels = 4;
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You must read the message when you get an exception to see the line number reported and try to fix it yourself.

    Anyway, you have
    Java Code:
    //...
           //let's call this panelA
            MainPanel mainPanel;
    	
    	public DrawFrame() {
                    //this is a brand new mainPanel
                    //not the same as panelA
    		MainPanel mainPanel = new MainPanel();
    		Container content = getContentPane();
    		content.add(mainPanel);
    	}
    //....
    So what you declare inside the constructor is not the same as what you declared outside it. That means what you declared outside the constructor is never initialized inside the constructor.
    These scoping rules must be learnt before trying to write GUI programs.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    You must read the message when you get an exception to see the line number reported and try to fix it yourself.
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Rectangle.<init>(Rectangle.java:13)
    at DrawPanel.paintComponent(GUI.java:58)

    Rectangle.java 13th line:
    Java Code:
    leftUpPoint.x = x;
    GUI.java 58th line:
    Java Code:
    Rectangle rect = new Rectangle(80,80,70,89);
    So, I don't see any nullPointException problem here. My opinion is that constuctor is nice defined.

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    So what you declare inside the constructor is not the same as what you declared outside it.
    Do you suggest me something like that:
    Java Code:
    public DrawFrame() {
                  mainPanel = new JPanel() {
    			@Override
                            public mainPanel() {
                             
                            }
    	      }
    	}

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Problem is solved. I've changed constructor:

    Java Code:
    public Rectangle(int x, int y, int width, int height) {
    	leftUpPoint = new Point(x,y);
    	this.width = width;
    	this.height = height;
    }

Similar Threads

  1. Replies: 3
    Last Post: 05-05-2010, 02:15 PM
  2. Rectangles method
    By bdario1 in forum New To Java
    Replies: 31
    Last Post: 03-31-2010, 10:32 PM
  3. Replies: 1
    Last Post: 07-16-2009, 03:15 PM
  4. How to Draw Round Rectangles in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 10:25 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 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
  •