Solved

nullpointerexception at dataoutputstream

Posted on 2010-11-14
18
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

717 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