Click to See Complete Forum and Search --> : Again: help with Java program
el-half
August 12th, 2004, 10:00 AM
This is a class in a program I am working on:
class ImageBox extends JFrame {
JTextField filename = new JTextField(15);
JLabel label = new JLabel("Save as: ", JLabel.LEFT);
JButton capture = new JButton("Get the image");
GridLayout layout = new GridLayout(2, 2);
DataConnect dataconn = new DataConnect(999);
byte[] image;
int i = 0;
String file;
public ImageBox() {
super("Save a screenshot");
capture.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
dataconn.establishConnection();
try {
while (true) {
image[i] = dataconn.data_reader.readByte();
i++;
}
} catch (IOException e) {
System.err.println("Exception: " + e.getMessage());
}
file = filename.getText();
}
});
JPanel pane = new JPanel();
pane.setLayout(layout);
pane.add(label);
pane.add(filename);
pane.add(capture);
setContentPane(pane);
}
void applyWindowListener() {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
}
};
this.addWindowListener(l);
this.pack();
this.setVisible(true);
}
}
Why, when I click the capture button an exception is thrown that there was an event generated at an unknown source, although I have added an ActionListener?
el-half
August 12th, 2004, 05:49 PM
euhm, no, there isn't supposed to be a ')' there, look at the end, it closes with '});' , notice the ')'. It compiles fine, it's just that damn exception that is thrown. I thought it would be a VM issue again so I changed the class to implement ActionListener and changed some stuff:
capture.addActionListener(this);
I also obviously created a public void actionPerformed() that is not inside the ActionListener declaration like above. Didn't solve anything.
zENGER
August 12th, 2004, 05:52 PM
Nm, you spacing was a little off in my browser and I matched the wrong } with it.
zENGER
August 12th, 2004, 05:58 PM
I mainly know c++, but did some java while in school so I'm a little rusty. Could this be the cause of your exception?
try {
while (true) {
image[i] = dataconn.data_reader.readByte();
i++;
}
Seems like that would be a forever loop until it reads past the end of dataconn and then tosses an exception?
el-half
August 12th, 2004, 06:06 PM
No, that's not the problem, dataconn is an instance of the DataConnect class (which I wrote), the DataConnect class is only used to establish a connection to transfer binary data.
It doesn't read past the end of anything, the server closes the connection when it has sent the data being received in the snippet you just quoted.
The problem is, like I said, and exception is thrown that an event at an unknown source has occured. It's not an exception that the compiler forces you to try/catch or declare that it can be thrown.
Full source code of the program can be found here: elhalf.com/Crat.java (client part) and elhalf.com/CratServer.java (server). You won't find that code in it, the code on my site works. I am adding a feature to take a screenshot on the remote computer and transfer it over a socket. But at least you can get an idea of what this program does.
zENGER
August 12th, 2004, 06:56 PM
The server may sever the connection, but how do you exit the while(true) loop? There is no break or return. Maybe it should be
try {
while (image[i] = dataconn.data_reader.readByte() )
i++;
}
el-half
August 12th, 2004, 07:18 PM
Well you are probably correct, there may be other logic mistakes too in the entire program but that's not the point for now. The program doesn't get to that point so for now I first have to find the problem of the exception that is thrown. I'll download the latest sdk (I am using 1.4.2_01-b6 and the latest is 1.4.2_05 so it is most likely a vm or compiler issue.
Juridian
August 18th, 2004, 11:12 PM
Have you already fixed this?
Have you been running this through a good debugger? Or tried debugging print statements to check your program state and commenting out the code you think may be troublesome?
el-half
August 22nd, 2004, 12:07 PM
I have commented out the code that may cause trouble.
public void actionPerformed(ActionEvent evt) {
dataconn.establishConnection();
try {
while (true) {
image[i] = dataconn.data_reader.readByte();
i++;
}
} catch (IOException e) {
System.err.println("Exception: " + e.getMessage());
}
file = filename.getText();
}
});
But it's strange, while there is no exception anymore when I remove actionPerformed's content the exception thrown has nothing to do with IO...
cgkanchi
August 22nd, 2004, 06:41 PM
Please stop using your current VM build. The last problem you had was also (most likely) a VM issue. Remember that the 'b' in the name signifies a beta release. Please get a 1.4 release VM and try again.
Cheers,
cgkanchi
el-half
August 22nd, 2004, 06:45 PM
I have the latest stable VM (1.4.2_05) already as I thought that was the problem. Nothing is solved :(
chsh
August 23rd, 2004, 05:23 PM
Originally posted here (http://www.AntiOnline.com/showthread.php?threadid=260944#post783470) by el-half
I have commented out the code that may cause trouble.
[...]
But it's strange, while there is no exception anymore when I remove actionPerformed's content the exception thrown has nothing to do with IO...
How about a catch block that reads:
try {
blahblah...;
} catch (Exception e) {
System.out.println("Exception Thrown: "+ e.getClass().getName() +");
System.out.println("Message: "+ e.getMessage());
}
Should help you understand which exception is being thrown.
el-half
August 23rd, 2004, 05:33 PM
Thanks, I now found out a NullPointer exception is thrown, so something is wrong with the IO stuff after all. I think.
/me is unable to fix it.
chsh
August 24th, 2004, 04:28 PM
NullPointerException is probably being thrown by that DataConnect class. I'd help, but DataConnect isn't a part of the Java API. I'd assume reading the third party docs for it might help you a bit.
el-half
August 24th, 2004, 04:33 PM
DataConnect is a class I wrote:
class DataConnect { //class for transfer of image
Socket socket = null;
BufferedInputStream buffer = null;
DataInputStream data_reader = null;
String address = "192.168.1.3";
int PORTNUM;
public DataConnect(int port) {
PORTNUM = port;
}
void establishConnection() {
try {
socket = new Socket(address, PORTNUM);
buffer = new BufferedInputStream(socket.getInputStream());
data_reader = new DataInputStream(buffer);
} catch (IOException e) {
System.err.println("Exception: " + e.getMessage());
System.exit(1);
}
}
}
chsh
August 24th, 2004, 04:53 PM
The exception should give you line numbers, so you should be able to figure out what line in which file is generating the exception. You might also try something like:
try {
i=0;
while ((image[i++] = dataconn.data_reader.readByte()) != -1) {
// Read data in while it's not EOS
}
} catch (Exception e) {
System.out.println("Exception Thrown: "+ e.getClass().getName() +");
System.err.println("Exception: " + e.getMessage());
}