Solved

nullpointerexception at dataoutputstream

Posted on 2010-11-14
18
327 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:hipal
  • 9
  • 6
  • 3
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132631
Is canvas null? Is dos null?
0
 

Author Comment

by:hipal
ID: 34132642
No .. CEHJ ..

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

thanks in advance
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132648
There isn't any code ;) You didn't attach it
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:hipal
ID: 34132661
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
 
LVL 92

Expert Comment

by:objects
ID: 34132677
>                    DataOutputStream dos = new DataOutputStream(FW);

change that to:


                   dos = new DataOutputStream(FW);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132680
Change FileOutputStream FW = new FileOutputStream("resources/out.txt", true);

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

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132694
(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
 

Author Comment

by:hipal
ID: 34132730
thanks .. now no exception  ... but nthing sensible gets written to file
0
 

Author Comment

by:hipal
ID: 34132732
and why we do not use dos ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132735
You need to close the file at some point
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132738
(That's to say close dos)
0
 

Author Comment

by:hipal
ID: 34132753
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132764
What are you writing now, and where?
0
 

Author Comment

by:hipal
ID: 34132775
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
 
LVL 92

Expert Comment

by:objects
ID: 34132780
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 34132786
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34132792
The kind of file you're writing isn't really relevant. How and where is more important
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 34132794
You could open it on app init and close when you need to save the data
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question