So I have this small program that draws circles of different colors depending on "velocity" Below is my code, btw I haven't actually implemented velocity yet. When I run it, I get these"

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
	at Mouse.paintComponent(Mouse.java:35)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at javax.swing.JLayeredPane.paint(Unknown Source)
	at javax.swing.JComponent.paintChildren(Unknown Source)
	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
	at javax.swing.RepaintManager.paint(Unknown Source)
	at javax.swing.JComponent.paint(Unknown Source)
	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
	at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
	at java.awt.Container.paint(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

My code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Mouse extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Point point1 = null, point2 = null;
	
		
	
	

	
	public Mouse()
	{
		
		CircleListener listen = new CircleListener();
		addMouseListener (listen);
		addMouseMotionListener (listen);
		
		setBackground (Color.white);
	}
	
	public void paintComponent (Graphics page)
	{
		super.paintComponent (page);
		

		int velocity = (((point2.x+point2.y)-(point1.x+point1.y)));
		velocity = Math.abs(velocity); 
		 
		if (point1.x >255||point1.y>255||velocity>255){
		while (velocity>255){velocity=velocity-255;} //if velocity is over 255, keep subtracing 255 from the velocity to get an acceptable number
		page.setColor (new Color(point1.x/3, point1.y/3, velocity)); //colors cant cross the 0-255 range
		}
		else
			page.setColor (new Color(point1.x,point1.y,velocity));////////////////
		
		if (point1 != null && point2 != null)
		{				
				int radius = (int) (Math.sqrt(Math.pow(point2.x-point1.x, 2)+Math.pow(point2.y-point1.y, 2)));
				int top = (int) (point1.x - radius);
				int left = (int) (point1.y - radius);
				page.drawOval (top, left, 2*radius, 2*radius);
		}
	
	}
	
	




	private class CircleListener implements MouseListener, MouseMotionListener
	{
		
	
		public void mousePressed (MouseEvent event)
		{
			point1 = event.getPoint();	
			
		}
		
		public void mouseDragged (MouseEvent event)
		{
			point2 = event.getPoint();
			repaint();
		}
		
		public void mouseClicked (MouseEvent event) {

		}
		public void mouseReleased (MouseEvent event) {}
		public void mouseEntered (MouseEvent event) {}
		public void mouseExited (MouseEvent event) {}
		public void mouseMoved (MouseEvent event) {}
		
	}
}

Maybe paintComponent is being called before you initialise point1 and point2?
You could test for them being not null before you try to use them at line 34.

found the problem. it wansnt that james, I just needed a try catch.

try{
		int velocity = (((point2.x+point2.y)-(point1.x+point1.y)));//used distance instead of velocity (has a cooler effect)
		velocity = Math.abs(velocity); 
		 
		if (point1.x >255||point1.y>255||velocity>255){
		while (velocity>255){velocity=velocity-255;} //if velocity is over 255, keep subtracing 255 from the velocity to get an acceptable number
		page.setColor (new Color(point1.x/3, point1.y/3, velocity)); //colors cant cross the 0-255 range
		}
		else
			page.setColor (new Color(point1.x,point1.y,velocity));////////////////
		}catch (Exception ex){}

Thanks though.

By using a try with an empty catch you are not stopping the problem, you are just hiding it. You still get the exception, but the empty catch means you ignore it. The only problem with this is that if you get some other error in the same block of code you will never find out what/why it was.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.