0

I have a class DynamicDataDemo, which has a null pointer catch. However I don't understand why jPanel is being passed as null. It is initialized by netbeans as a border layout and size is also declared.

public DynamicDataDemo(final String title){
this(title,null);
}    

public DynamicDataDemo(final String title, JPanel panel){
super(title);
setContentPane(initContents(panel));
}

public JPanel initContents(JPanel contentPanel){
    if (contentPanel == null){
        System.out.print("You are Here");
        contentPanel = new JPanel(new BorderLayout());
    }


    this.series = new TimeSeries("Data", Millisecond.class);
    final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
    final JFreeChart chart = createChart(dataset);

    final ChartPanel chartPanel = new ChartPanel(chart);
    contentPanel.setLayout(new BorderLayout());
    contentPanel.removeAll();
    contentPanel.add(chartPanel, BorderLayout.CENTER);
    contentPanel.validate();
    return contentPanel;              

}

In my main class I call

Final DynamicDataDemo dynamicPlot = new DynamicDataDemo("nameOfPlot", jPanel1);

Any suggestions or solutions greatly appreciated!

Following code was generated by netBeans:

private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();

    jPanel1.setBackground(new java.awt.Color(0, 0, 0));
    jPanel1.setLayout(new java.awt.BorderLayout());

    jPanel2.setBackground(new java.awt.Color(0, 0, 0));
    jPanel2.setLayout(new java.awt.BorderLayout());
}

private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
// End of variables declaration                   

Code I wrote:

final DynamicDataDemo dynamicEyePlot = new DynamicDataDemo("Eye Height", jPanel1);
final DynamicDataDemo dynamicGainPlot = new DynamicDataDemo("Gain",jPanel2);
Emunzy
  • 1
  • 3
  • your jPanel1 is null, show where you got jPanel1 from – Tomer Shemesh May 23 '16 at 23:19
  • how do I show where I get jPanel1 from? I'm passing it from the main class where I declared jPanel1. – Emunzy May 24 '16 at 21:20
  • show where you call DynamicDataDemo from and where you get the panel that you pass into it. all i see is new DynamicDataDemo("nameOfPlot", jPanel1); but i want to see where jPanel1 is created or refrenced – Tomer Shemesh May 24 '16 at 21:21
  • okay added jPanel Initialize and declaration to end of post. Also updated original code to include null exception. – Emunzy May 24 '16 at 21:47
  • are you sure initComponents is being called? – Tomer Shemesh May 24 '16 at 21:49
  • Yes, I abbreviated the content being called by initCompponents to keep the post easy to read. However, I have other buttons being called in that function that get initialized correctly. Also that block of code is generated by net beans and can not be edited through the source code. – Emunzy May 24 '16 at 21:53

1 Answers1

1

Just change your code like below. Hope it helps.

    public DynamicDataDemo(final String title) {
        this(title,null);
    }

    public DynamicDataDemo(final String title, JPanel panel) {
        super(title);
        setContentPane(initContents(panel));
    }

    public JPanel initContents(JPanel contentPanel) {
        if (contentPanel == null) {
            contentPanel = new JPanel(new BorderLayout());
        }
        this.series = new TimeSeries("Data", Millisecond.class);
        final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
        final JFreeChart chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 400));
        contentPanel.setLayout(new BorderLayout());
        contentPanel.add(chartPanel);
        return contentPanel;
    }
Beniton Fernando
  • 1,533
  • 1
  • 14
  • 21
  • The solution is good for catching the null exception, and allows my code to compile. However I want to solve why jPanel1 is being passed as null. Thanks for the solution though. – Emunzy May 24 '16 at 21:54