hipal
asked on
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.tra nslateX);
thanks
I am not able to understand why it is giving nullpointerexception . where I am trying to write in file
that is dos.writeDouble(canvas.tra
thanks
Is canvas null? Is dos null?
ASKER
No .. CEHJ ..
can you please try running the code .. m troubled too long for this :(
thanks in advance
can you please try running the code .. m troubled too long for this :(
thanks in advance
There isn't any code ;) You didn't attach it
ASKER
ohh sorry ... hehe
attached now ..
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();
}
}
}
}
> DataOutputStream dos = new DataOutputStream(FW);
change that to:
dos = new DataOutputStream(FW);
change that to:
dos = new DataOutputStream(FW);
Change FileOutputStream FW = new FileOutputStream("resource s/out.txt" , true);
to
to
FW = new FileOutputStream("resources/out.txt", true);
(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...
ASKER
thanks .. now no exception ... but nthing sensible gets written to file
ASKER
and why we do not use dos ?
You need to close the file at some point
(That's to say close dos)
ASKER
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 ..
-- @80000
moreover can you suggest where I should close dos in this code ..
What are you writing now, and where?
ASKER
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 . . :(
It does not makes any difference and I am still not getting anything written in the text file
sorry about annoying and silly doubts . . :(
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
> moreover can you suggest where I should close dos in this code ..
close it as soon as you are done writing to it
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The kind of file you're writing isn't really relevant. How and where is more important
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.