Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: Unable to fetch data from other class

  1. #1
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default Unable to fetch data from other class

    Hi,
    I have 2 classes - login and MailReader. login builds the GUI and gets data from the user i.e username, password. MailReader has the main function and makes an object of GUI class and calls the buildGUI method. On pressing the OK button the ActionListener for the button after setting the username and password invokes the MailReader class. In the MailReader class i have a try catch block. Now if i use the same object to retrieve the username and password from getters of login class it tells me that object unidentified. If i create a new object and call getters then i get null instead of the data entered.
    I sense some basic flaw that i am doin here. Any help is deeply appreciated.

    GUI class -

    Java Code:
    public class login {
    	public static final int WIDTH = 550;
    	public static final int HEIGHT = 400;
    	private static String MONITOR = "MONITOR";
    	private JFrame controllingFrame;
    	JFrame frame;
    	JTextField emailField;
    	JTextField mailboxField;
    	JPasswordField passwdField;
    	JButton monitorButton;
    	JButton cancelButton;
    	
    	private String email;
    	private String mailbox;
    	private String password;
    
    	public void buildGUI(){
    			frame = new JFrame("Welcome to Visualization");
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			BorderLayout layout = new BorderLayout();
    			JPanel background = new JPanel(layout);
    			background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    			
    			Box labelBox = new Box(BoxLayout.Y_AXIS);
    
    			//Label 1
    			
    			JLabel reg = new JLabel("Registration Form");
    			Font bigFont = new Font("serif", Font.BOLD, 28);
    			reg.setFont(bigFont);
    			labelBox.add(reg);
    			
    			//Label 2
    			JLabel dir = new JLabel("Please enter the details below :");
    			labelBox.add(dir);
    			
    			//EMail and Password field
    			
    			JPanel panelB = new JPanel(new GridLayout(3,1));
    			//JPanel panelB = new JPanel();
    			
    			panelB.add(new JLabel("Email ID:"));
    			emailField = new JTextField(20);
    			emailField.setToolTipText("Enter your email-ID");
    			//emailField.setActionCommand(MONITOR);
    			setEmail(emailField.getText());
    			
    			panelB.add(emailField);
    			
    			panelB.add(new JLabel("Password :"));
    			passwdField = new JPasswordField(20);
    			passwdField.setToolTipText("Enter your password");
    			//passwdField.setActionCommand(MONITOR);
    			setPassword(passwdField.getPassword());
    			panelB.add(passwdField);
    			labelBox.add(panelB);
    			
    			JPanel panelC = new JPanel();
    			monitorButton = new JButton("MONITOR");
    			monitorButton.addActionListener(new MonitorListener());
    			panelC.add(monitorButton);
    			cancelButton = new JButton("CANCEL");
    			cancelButton.addActionListener(new CancelListener());
    			panelC.add(cancelButton);
    			labelBox.add(panelC);
    			
    			background.add(BorderLayout.WEST, labelBox);
    			frame.getContentPane().add(background);
    			frame.setBounds(50,50,300,300);
    			frame.pack();
    			frame.setSize(WIDTH, HEIGHT);
    			frame.setVisible(true);
    	}
    
    	//ActionListener Inner Class for the button MONITOR
    
    	public class MonitorListener implements ActionListener{
    		public void actionPerformed(ActionEvent a){
    			String cmd = a.getActionCommand();
    			if(MONITOR.equals(cmd)){
    				setEmail(emailField.getText());
    				setPassword(passwdField.getPassword());
                                    // Invoking the MailReader Class
    				new MailReader();
    			}
    			else{
    				JOptionPane.showMessageDialog(controllingFrame,
    		                "Invalid password. Try again.",
    		                "Error Message",
    		                JOptionPane.ERROR_MESSAGE);
    			}
    		}
    	}
    	public class CancelListener implements ActionListener{
    		public void actionPerformed(ActionEvent a){
    				System.exit(0);
    		}
    	}
    	public void setEmail(String emailString){
    		email = emailString;
    	}
    	
    	public void setPassword(char[] passwordChar){
    		String passwordString = new String(passwordChar); 
    		password = passwordString;
    	}
    	
    	public String getEmail(){
    		return email; 
    	}
    	
    	public String getPasswd(){
    		return password; 
    	}
    }
    MailReader Class -

    Java Code:
    public class MailReader {
    	private int unreadMessages;
    	private int totalMessages;
    	Folder inbox;
    	
    	public int getUnreadMessages(){
    		return unreadMessages;
    	}
    	
    	public MailReader(){
    
    		Properties props = System.getProperties();
    		props.setProperty("mail.store.protocol", "imap");
    		try{
    			Session session = Session.getDefaultInstance(props, null);
    			Store store = session.getStore("imap");
    
    // Here is the problem where it is difficult getting the username and password from the login class
    			String mailId = obj1.getEmail();
    			System.out.println("Mail id fetched is :" + mailId);
    			String passwod = obj1.getPasswd();
    			
    			store.connect(mailbox, mailId, passwod);
    			System.out.println(store);
    
    			inbox = store.getFolder("Inbox");
    			int totalMsgs = inbox.getMessageCount();
    			int unreadMsgs = inbox.getUnreadMessageCount();
    			System.out.println("Total no of	Messages : " + totalMsgs);
    			System.out.println("No of Unread Messages : " + unreadMsgs);
    			inbox.open(Folder.READ_ONLY);
    			Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
    /*********** Use a suitable FetchProfile***********/
    			FetchProfile fp = new FetchProfile();
    			fp.add(FetchProfile.Item.ENVELOPE);
    			fp.add(FetchProfile.Item.CONTENT_INFO);
    			inbox.fetch(messages, fp);
    						
    			try{
    				printAllMessages(messages);
    				inbox.close(true);
    				store.close();
    			}
    			catch (Exception ex){
    				System.out.println("Exception arose at the time of read mail");
    				ex.printStackTrace();
    			}
    		}
    		catch (NoSuchProviderException e){
    			e.printStackTrace();
    			System.exit(1);
    		}
    		catch (MessagingException e){
    			e.printStackTrace();
    			System.exit(2);
    		}
    	}
    
    public static void main(String[] args) {
    		login obj1 = new login();
    		obj1.buildGUI();
    	}
    Last edited by buggedup; 02-03-2013 at 11:39 AM. Reason: adding code

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Unable to fetch data from other class

    Hello buggedup,

    Check the access modifier for obj1. ;)

    Regards.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default Re: Unable to fetch data from other class

    It doesn't have an access modifier as it is a local variable and so is not visible anywhere else in the code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Unable to fetch data from other class

    Quote Originally Posted by Tolls View Post
    It doesn't have an access modifier as it is a local variable and so is not visible anywhere else in the code.
    Exactly what I was hinting at.

    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regards.

  5. #5
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default Re: Unable to fetch data from other class

    I think there is a flow problem but i am not able to figure out what. I am calling the builGUI() method from the login class and before buildGUI() completes i invoke the MailReader() constructor. Is this correct?
    However i changed the flow and put the main in login class and now am calling buildGUI and then i doing a new MailReader(); in the main. But this has a problem as well. The GUI does not wait for user input at all. It just flashes and goes away.
    I am a bit confused and stuck on how to do this. Should i stick to the previous code or this version?
    Last edited by buggedup; 02-05-2013 at 10:47 PM.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default Re: Unable to fetch data from other class

    Take a step back.

    Your Login class (classnames should begin with an uppercase letter) is your view. It's job is to display stuff and to react to user events.
    One of those events involves a button click, which should result in your MailReader code running.
    That code is not part of the GUI.
    It should either be called by a controller, which would be registered to the Login class, or should itself be registered to the login class.
    That would normally be in the form of an interface.

    Look at how ActionListener works, and the registering of one.
    That's how you want this to work.

    Java Code:
    public class Login {
       private MonitorListener monitorListener;  // This is an interface that your MailReader will implement.  Names are up to you.
    
       public Login (MonitorListener ml) {
          monitorListener = ml;
          // rest of your code here.
       }
    
       // Inside your ActionListener, monitor part
       monitorListener.monitorEmails(email, mailbox, password);
    etc etc
    }
    Your main() method will create a MailReader, that does nothing in the constructor and implements the monitorEmails method.
    That method does what your constructor code does at the moment.
    The main() will pass that object into the Login constructor.
    buggedup likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default Re: Unable to fetch data from other class

    I don't exactly get it what you are trying to say. I understand that the controller part should not be a part of the view i.e Login class. But i have 2 inner classes which implement ActionListener i.e MonitorListener and CancelListener. Should i replace both of these with the actionPerformed method ?
    main() creates a MailReader, but how can i create an object of MailReader before Login? i shud get input frm user first rite?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default Re: Unable to fetch data from other class

    No, they are both listeners.
    They're fine.
    One for each button.

    You want, inside the monitor one, to tell the controller to do whatever it is MailReader does.
    Move the code in mailReader into its own method.
    Don't bother with a constructor for that at all.
    Call that method from the MonitorListener.
    buggedup likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    buggedup is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default Re: Unable to fetch data from other class

    Just moved the code from the MailReader() constructor to a method montorEmails() and called it from the monitorListener. It worked. But i dont understand why this happened? y would the GUI disappear if i have the code in the MailReader constructor and y would the GUI wait for input if i have the code in the method.

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default Re: Unable to fetch data from other class

    I have no idea what your code looked like for the bit where the GUI disappears, so I couldn't say.
    I expect there was a System.exit() being fired somewhere, which wasn't happening for the method call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2011, 10:45 AM
  2. Replies: 4
    Last Post: 01-21-2010, 05:46 PM
  3. How to fetch integer data from excel
    By nehakuls in forum JDBC
    Replies: 5
    Last Post: 11-24-2009, 01:52 PM
  4. How to fetch integer data from excel
    By nehakuls in forum NetBeans
    Replies: 2
    Last Post: 11-18-2009, 05:47 AM
  5. Unable to read data from inputstream
    By renuka_anil in forum Java Servlet
    Replies: 0
    Last Post: 01-29-2009, 03:20 PM

Tags for this Thread

Posting Permissions

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