i have 2 class for example class a and class b. i need the string store at class a pass and use by class b, here is my code for class a and class b.

class a

public void actionPerformed(ActionEvent arg0) {
                 JFileChooser fileChooser = new JFileChooser();

                 fileChooser.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
                 fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                 fileChooser.setAcceptAllFileFilterUsed(false);
                 int returnValue = fileChooser.showOpenDialog(null);
                 if (returnValue == JFileChooser.APPROVE_OPTION) {
                     String test;
                      File selectedFile = fileChooser.getSelectedFile();
                     test=fileChooser.getSelectedFile().toString();

                    }

class b

try{
            a path = new a();

            FileInputStream fis = new FileInputStream(path.test);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));

           while((line = br.readLine()) != null) 
           {

           tableModel.addRow(line.split("___")); 
           }
           br.close();
        }
       catch(IOException e){
           JOptionPane.showMessageDialog(null, "Error");
   e.printStackTrace();
       }

im check using System.out.print for String value test for both class. in class a, i can get the correct value, but in class b, i only get null.

You declare String test on line 9 of a. When you declare a variable in Java its scope if from the preceeding '{' to the following '}'. In this case that test variable exists just from line 8 to line 13, after which it is discarded. You presumably have another String test declared somewhere else, and that's the one the b uses.

ps If you are no longer waiting for replies to your previous topic, please mark it "solved". Thank you.

Edited 2 Years Ago by JamesCherrill

ok, i got it now so i make some changes. but still i cant get the value from class testgui String test value to class b.

public class testgui {


    private JFrame pu;

    String test;

    public static void main(String [] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    testgui window = new testgui();
                    window.pu.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
    }

    /**
     * Create the application.
     */
    public testgui() {

        initialize();
    }

i also had try the getter setter method but it still not working. i still cant get the value.

Edited 2 Years Ago by sing1006

ok, i find out the solution already.

class a
    static String test="testing";

and you can get the String test value from other class by

class b
    "your class name"."the String name"

for example

System.out.println(a.test);

then it will show the value inside that String.

This question has already been answered. Start a new discussion instead.