Results 1 to 5 of 5
- 05-20-2010, 01:47 PM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Drawing Rectangles - NullPointerExceptions
Hi. I have a problem with java.lang.NullPointerException.
This is my program.
class: GUI.java
class: Rectangle.javaJava 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); } }
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; }
- 05-20-2010, 01:52 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
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.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); } //....
These scoping rules must be learnt before trying to write GUI programs.
- 05-20-2010, 02:06 PM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerExceptionJava Code:You must read the message when you get an exception to see the line number reported and try to fix it yourself.
at Rectangle.<init>(Rectangle.java:13)
at DrawPanel.paintComponent(GUI.java:58)
Rectangle.java 13th line:
GUI.java 58th line:Java Code:leftUpPoint.x = x;
So, I don't see any nullPointException problem here. My opinion is that constuctor is nice defined.Java Code:Rectangle rect = new Rectangle(80,80,70,89);
- 05-20-2010, 02:16 PM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Do you suggest me something like that:Java Code:So what you declare inside the constructor is not the same as what you declared outside it.
Java Code:public DrawFrame() { mainPanel = new JPanel() { @Override public mainPanel() { } } }
- 05-20-2010, 02:37 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Similar Threads
-
When I resize window color of rectangles is changed
By cselic in forum Java 2DReplies: 3Last Post: 05-05-2010, 01:15 PM -
Rectangles method
By bdario1 in forum New To JavaReplies: 31Last Post: 03-31-2010, 09:32 PM -
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
How to Draw Round Rectangles in SWT
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:25 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks