Again: help with Java program
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Again: help with Java program

  1. #1
    Senior Member
    Join Date
    Jun 2003
    Posts
    772

    Again: help with Java program

    This is a class in a program I am working on:

    Code:
    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?
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    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.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  3. #3
    Senior Member
    Join Date
    Jul 2004
    Posts
    469
    Nm, you spacing was a little off in my browser and I matched the wrong } with it.

  4. #4
    Senior Member
    Join Date
    Jul 2004
    Posts
    469
    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?

  5. #5
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    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.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  6. #6
    Senior Member
    Join Date
    Jul 2004
    Posts
    469
    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++;
    }

  7. #7
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    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.
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  8. #8
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    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?
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  9. #9
    Senior Member
    Join Date
    Jun 2003
    Posts
    772
    I have commented out the code that may cause trouble.

    Code:
    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...
    The above sentences are produced by the propaganda and indoctrination of people manipulating my mind since 1987, hence, I cannot be held responsible for this post\'s content - me

    www.elhalf.com

  10. #10
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    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
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •