nullpointerexception at dataoutputstream

Can some please try executing the small code attached below ..

I am not able to understand why it is giving nullpointerexception . where I am trying to write in file

that is dos.writeDouble(canvas.translateX);

thanks
hipalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
CEHJCommented:
Is canvas null? Is dos null?
0
 
hipalAuthor Commented:
No .. CEHJ ..

can you please try running the code .. m troubled too long for this :(

thanks in advance
0
 
CEHJCommented:
There isn't any code ;) You didn't attach it
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
hipalAuthor Commented:
ohh sorry ... hehe

attached now ..
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
 
import javax.swing.JComponent;
import javax.swing.JFrame;
 
public class AffineTranformTest {
 
	private static TransformingCanvas canvas;

	 private static DataOutputStream dos;
	 static FileOutputStream FW;
	public static void main(String[] args) throws IOException {
		
		FileOutputStream FW = new FileOutputStream("resources/out.txt", true);
		
		JFrame frame = new JFrame();
		canvas = new TransformingCanvas();
		TranslateHandler translater = new TranslateHandler();
		canvas.addMouseListener(translater);
		canvas.addMouseMotionListener(translater);
		canvas.addMouseWheelListener(new ScaleHandler());
		frame.setLayout(new BorderLayout());
		frame.getContentPane().add(canvas, BorderLayout.CENTER);
		frame.setSize(500, 500);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
	}
 
	private static class TransformingCanvas extends JComponent {
		private double translateX;
		private double translateY;
		private double scale;
 
		TransformingCanvas() {
			translateX = 0;
			translateY = 0;
			scale = 1;
			setOpaque(true);
			setDoubleBuffered(true);
		}
 
		@Override public void paint(Graphics g) {
		
			AffineTransform tx = new AffineTransform();
			tx.translate(translateX, translateY);
		//	out.write(x);
			tx.scale(scale, scale);
			Graphics2D ourGraphics = (Graphics2D) g;
			ourGraphics.setColor(Color.WHITE);
			ourGraphics.fillRect(0, 0, getWidth(), getHeight());
			ourGraphics.setTransform(tx);
			ourGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
					RenderingHints.VALUE_ANTIALIAS_ON);
			ourGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
					RenderingHints.VALUE_TEXT_ANTIALIAS_ON);			
			ourGraphics.setColor(Color.BLACK);
			ourGraphics.drawRect(50, 50, 50, 50);
			ourGraphics.fillOval(100, 100, 100, 100);
			ourGraphics.drawString("Test Affine Transform", 50, 30);
			ourGraphics.getTransform();
			// super.paint(g);
		}
	}
 
	private static class TranslateHandler implements MouseListener,
			MouseMotionListener {
		private int lastOffsetX;
		private int lastOffsetY;
 
		public void mousePressed(MouseEvent e) {
			// capture starting point
			lastOffsetX = e.getX();
			lastOffsetY = e.getY();
		//	System.out.println("mouse pressed X" + e.getX());
	//		System.out.println("mouse pressed Y" + e.getY());
		}
 
		public void mouseDragged(MouseEvent e) {
			 DataOutputStream dos = new DataOutputStream(FW);
			 System.out.println("dos" + dos);
			 
			// new x and y are defined by current mouse location subtracted
			// by previously processed mouse location
			int newX = e.getX() - lastOffsetX;
			int newY = e.getY() - lastOffsetY;
			
			System.out.println();
			// increment last offset to last processed by drag event.
			lastOffsetX += newX;
			lastOffsetY += newY;
			
			// update the canvas locations
			canvas.translateX += newX;
			canvas.translateY += newY;
			System.out.print("mouse dragged X   " + canvas.translateX);
			System.out.print("mouse dragged Y   " + canvas.translateY  + " ");
	
			// schedule a repaint.
			canvas.repaint();
		}
 
		public void mouseClicked(MouseEvent e) {}
		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mouseMoved(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
 
	private static class ScaleHandler implements MouseWheelListener {
		public void mouseWheelMoved(MouseWheelEvent e) {
			if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
				
				// make it a reasonable amount of zoom
				// .1 gives a nice slow transition
				canvas.scale += (.1 * e.getWheelRotation());
				
				// don't cross negative threshold.
				// also, setting scale to 0 has bad effects
				canvas.scale = Math.max(0.00001, canvas.scale); 
				canvas.repaint();
			}
		}
	}
}

Open in new window

0
 
objectsCommented:
>                    DataOutputStream dos = new DataOutputStream(FW);

change that to:


                   dos = new DataOutputStream(FW);
0
 
CEHJCommented:
Change FileOutputStream FW = new FileOutputStream("resources/out.txt", true);

to
FW = new FileOutputStream("resources/out.txt", true);

Open in new window

0
 
CEHJCommented:
(You don't actually use 'dos', but if you did, you'd need to use it in the method you created it, since you don't assign it to the global variable). So far, it's not a problem...
0
 
hipalAuthor Commented:
thanks .. now no exception  ... but nthing sensible gets written to file
0
 
hipalAuthor Commented:
and why we do not use dos ?
0
 
CEHJCommented:
You need to close the file at some point
0
 
CEHJCommented:
(That's to say close dos)
0
 
hipalAuthor Commented:
even if I am closing dos .. there is nothing I can see in text file jus tthis
 -- @80000

moreover can you suggest where I should close dos in this code ..
0
 
CEHJCommented:
What are you writing now, and where?
0
 
hipalAuthor Commented:
I just added a line dos.close();

It does not makes any difference and I am still not getting anything written in the text file

sorry about annoying and silly doubts . . :(
0
 
objectsCommented:
you want to use a PrintWriter if you want to write a text file. Your current code is writing a binary file

> moreover can you suggest where I should close dos in this code ..


close it as soon as you are done writing to it
0
 
objectsCommented:
> It does not makes any difference and I am still not getting anything written in the text file

no it won't. And you're not writing a text file, it will be a binary file

you also shouldn't be opening the file in mouseDragged()
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
CEHJCommented:
The kind of file you're writing isn't really relevant. How and where is more important
0
 
CEHJCommented:
You could open it on app init and close when you need to save the data
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.