Results 1 to 13 of 13
  1. #1
    tmboredi is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default dataInputStream cannot be resolved in GUI class

    I have this in my main.class


    dataInputStream = new DataInputStream(socket.getInputStream())

    which works perfectly fine without the GUI

    But in my GUI class I get an error at this line:

    dataLabel1.setText(dataInputStream.readUTF())

    it says dataInputStream cannot be resolved.



    Any idea what's wrong?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: dataInputStream cannot be resolved in GUI class

    That means you haven't declared a variable called dataInputStream that is visible at that point in your code.

  3. #3
    tmboredi is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: dataInputStream cannot be resolved in GUI class

    I did. In another class though. But I've imported that class over.. can't work this way?

    I have this in my main.class


    dataInputStream = new DataInputStream(socket.getInputStream())

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: dataInputStream cannot be resolved in GUI class

    No.
    You would have to pass that reference to the place you were using it.
    Not knowing what you;re doing or how I can't say how you should do that.

  5. #5
    tmboredi is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: dataInputStream cannot be resolved in GUI class

    Oh sorry. Was wrecking my head to get it to work late last night and posted that post in a messed up state lol.
    I'm using sockets to link my android appplication on the phone to a java GUI on the PC. The Java application is supposed to display the values read from the incoming stream (from the phone). My main class (without the GUI) works fine displaying the values in the eclipse console, but it doesn't work when I use dataLabel1.setText(dataInputStream.readUTF()) for my GUI class. Hmmm if I were to pass that reference to the place i'm using it, wouldn't most of my code be in the GUI class?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: dataInputStream cannot be resolved in GUI class

    Then don't pass the stream into the GUI.
    Call methods on the GUI passing in the relevant data.

  7. #7
    tmboredi is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: dataInputStream cannot be resolved in GUI class

    Not really getting you...

    My main class... copied over the main string from the gui class to the main string here. Not sure if this is the right way.
    I bolded my problem area.

    Java Code:
    package testing.data;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    
    public class DataServer {
    	
    	public static void main(String[] args){
    		
    		try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new ServerGUI().setVisible(true);
                }
            });
    		ServerSocket serverSocket = null;
    		Socket socket = null;
    		DataInputStream dataInputStream = null;
    		
    		DataOutputStream dataOutputStream = null;
    		
    		try {
    			serverSocket = new ServerSocket(8888);
    
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		while(true){
    			try {
    				socket = serverSocket.accept();
    				[B]dataInputStream = new DataInputStream(socket.getInputStream());[/B]
    				dataOutputStream = new DataOutputStream(socket.getOutputStream());
    
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			finally{
    				if( socket!= null){
    					try {
    						socket.close();
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    				
    				if( dataInputStream!= null){
    					try {
    						dataInputStream.close();
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    				
    				if( dataOutputStream!= null){
    					try {
    						dataOutputStream.close();
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    		}
    	}
    }
    ServerGUI class....

    Java Code:
    package testing.data;
    
    
    public class ServerGUI extends javax.swing.JFrame {
    
      
        public ServerGUI() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        
        
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
       //    LAYOUT STUFFS cut out
    
       jLabel1 = new javax.swing.JLabel();
       [B]jLabel1.setText(dataInputStream.readUTF());[/B]
    
            pack();
        }
    
       
        public static void main(String args[]) {
           
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            
    
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new ServerGUI().setVisible(true);
                }
            });
        }
      
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        
    }
    I get dataInputStream cannot be resolved on my ServerGUI.class ... Help me out please. I've been stuck here for ages :S

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: dataInputStream cannot be resolved in GUI class

    dataInputStream is a variable of the DataServer class and in fact it is declared and only visible within DataServer's static main method and nowhere else. So knowing this, how is a completely separate class, ServerGUI, supposed to mathemagically know what the dataInputStream is if this variable is no where declared in this class, but is just used out of the blue? This can never work.

    One solution is to give your ServerGUI constructor a parameter and pass the dataInputStream into the GUI, and then in a background thread read data from the Stream, and update the GUI with this information but do the latter only on the event thread, the EDT. But you've got way too much code in your static main method. Better perhaps is to create a separate OOP class to handle data input and output and give the GUI an instance of this class so that it may interact with it.

    Have you gone through the basic Java tutorials first before starting complex chit like this? If not, you won't regret going through a basic text on Java first. It's usually a lot easier to run when you know fully well how to walk first.
    Last edited by Fubarable; 01-13-2012 at 03:00 PM.

  9. #9
    tmboredi is offline Member
    Join Date
    Jan 2012
    Posts
    8
    Rep Power
    0

    Default Re: dataInputStream cannot be resolved in GUI class

    oh.. how can I make it visible globally then? sorry i'm new in Java.. should I put everything into one class? Or what do you suggest?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: dataInputStream cannot be resolved in GUI class

    Quote Originally Posted by tmboredi View Post
    oh.. how can I make it visible globally then?
    No, you don't want to do this!

    sorry i'm new in Java.. should I put everything into one class? Or what do you suggest?
    I suggest you first learn basic Java before attempting this stuff. Seriously. You've got to learn things in order. Else you're like someone trying to do brain surgery without first taking basic anatomy, physiology, and intro to surgery courses.
    Last edited by Fubarable; 01-13-2012 at 03:07 PM.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: dataInputStream cannot be resolved in GUI class

    Echoing Fubarable a bit, but you do seem to be trying to run before you can walk.

    Messing around with sockets and streams when you don't seem to know the basics of the language is a little ambitious.

    For example, your ServerGUI has no need for a main() method does it?
    That's the entry point for your application, which is already there in the DataServer class. No need to have two and possibly lead to confusion.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: dataInputStream cannot be resolved in GUI class

    Edit: the other solution is as Tolls suggested (and sorry for stepping on your thread, Tolls), to have the Stream reading class call GUI class methods to pass the read data into the GUI.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,432
    Rep Power
    18

    Default Re: dataInputStream cannot be resolved in GUI class

    No problem.
    It's a Friday and I'll be off soon for the weekend anyway, so step away!
    :)

Similar Threads

  1. DataInputStream
    By robs in forum Java Gaming
    Replies: 7
    Last Post: 04-14-2011, 06:27 PM
  2. Replies: 1
    Last Post: 03-11-2011, 08:37 AM
  3. DataInputStream to file
    By jasondeegan in forum New To Java
    Replies: 5
    Last Post: 06-16-2010, 03:35 PM
  4. Problem with DataInputStream
    By ZivonHunter in forum New To Java
    Replies: 1
    Last Post: 11-21-2008, 05:10 AM
  5. Replies: 1
    Last Post: 08-07-2007, 07:16 AM

Posting Permissions

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