Solved

nullpointerexception at dataoutputstream

Posted on 2010-11-14
18
330 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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…

730 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