Results 1 to 2 of 2
  1. #1
    Matth963 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Pass a string from a class to another

    I'm trying to pass a String from one class to another class but the result I'm getting is 'null'. I want to pass the String username from LoginFrame to HomeworkFrame;

    HomeworkFrame:

    Java Code:
    public void loadSubjects (){
        String item;
        try{
            System.out.println(username);
            Scanner f = new Scanner (new FileReader (username + " " + "Subjects" + ".txt"));
            while(f.hasNext()){
                item = f.nextLine();
                chSubjects.add(item);
            }
            f.close();  
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Subjects cannot be loaded!", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    LoginFrame:

    Java Code:
    public void loginUser(){
        r = new Registration();
        h = new HomeworkFrame();
        l = new Login();
        l.username = txtUser.getText();
        l.password = txtPass.getText();
        try{
            String line;
            boolean passwordFound = false ;
            BufferedReader f = new BufferedReader(new FileReader(l.username + ".txt"));
            while((line = f.readLine()) != null){
                if(line.equals(l.password)){
                    passwordFound = true;
                }
            }
            if(passwordFound){
                    h.username = l.username;
                    dispose();
                    m.setSize(700,600);
                    m.setLocation(100,100);
                    m.setVisible(true);
                }else{
                    JOptionPane.showMessageDialog(null, "Wrong information!", "Error", JOptionPane.ERROR_MESSAGE);
                }
            f.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Wrong information!", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    Obviously I'm getting the error "Subjects cannot be loaded" as the username is null(I checked it by using the println method).

    (PS: I'can't attach the files being used)

    Thanks in advance
    Matthew

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Pass a string from a class to another

    I don't see where username is defined, or how loadSubjects is called, or what classes these things are in, or where you think the value for the username in loadSubjects is coming from.

    Also, you need to printStackTrace() on your exceptions because you are losing a ton of information about what the actual error you are getting is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2011, 01:34 PM
  2. Pass String Value
    By DJCali in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 12:48 AM
  3. Pass String[] into method in different class
    By Swankee in forum New To Java
    Replies: 19
    Last Post: 09-28-2009, 05:30 PM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

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
  •