I have three classes TestGUI , MainFrame and ReportsJPanel. MainFrame object is passed as parameter in ReportsJPanel constructor. When I try to call getter method of MainFrame object that was passed and get the value of private variable, from ReportsJPanel object I get the NullPointerException error. Error happens on this line in ReportsJPanel instalationLocation=mainFrame.getInstalationLocation();
Code for TestGUI:
package testgui;
public class TestGUI {
    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(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RiskManagerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainFrame mainFrame = new MainFrame();
                mainFrame.initComponents();
                mainFrame.setVisible(true);
            }
        });
    }
}
Code for MainFrame:
package testgui;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import left_panel_package.ReportsJPanel;
public class MainFrame extends JFrame{
    private String instalationLocation;
    public MainFrame(){
        setInstalationLocation("TEST_Location");
    }
    public void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TEST");
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(new Color(0, 0, 0));
        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(new Color(255, 0, 0));
        leftPanel.setPreferredSize(new Dimension(200,100));
        leftPanel.setLayout(new GridLayout(3,0));
        JPanel rightPanel = new JPanel();
        rightPanel.setBackground(new Color(0, 255, 0));
        rightPanel.setPreferredSize(new Dimension(200,100));
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(new Color(0, 0, 255));
        JPanel toolBar = new JPanel();
        toolBar.setBackground(new Color(0, 255, 255));
        toolBar.setPreferredSize(new Dimension(100,100));
        ReportsJPanel reportsPanel = new ReportsJPanel(this);
        reportsPanel.initComponents();
        leftPanel.add(reportsPanel);
        mainPanel.add(leftPanel, BorderLayout.WEST);
        mainPanel.add(rightPanel, BorderLayout.EAST);
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        mainPanel.add(toolBar, BorderLayout.NORTH); 
        setContentPane(mainPanel);
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        setSize(width, height);
        setLocationRelativeTo(null);
    }
    public String getInstalationLocation() {
        return instalationLocation;
    }
    private void setInstalationLocation(String instalationLocation) {
        this.instalationLocation = instalationLocation;
    }
}
Code for ReportsJPanel:
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import testgui.MainFrame;
public class ReportsJPanel extends JPanel{
    private MainFrame mainFrame;
    private String instalationLocation;
    public ReportsJPanel (MainFrame mainframe){
        this.mainFrame=mainFrame;
        setBorder(new TitledBorder("Reports"));
    }
    public void initComponents(){
        instalationLocation=mainFrame.getInstalationLocation();
    }
}
 
     
    