Null Pointer Exception while refreshing the chart

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
swappyk
Newbie
Newbie
Posts: 6
Joined: Mon Aug 01, 2016 12:00 am
Contact:

Null Pointer Exception while refreshing the chart

Post by swappyk » Tue Sep 06, 2016 7:34 pm

Hi,

I have a Swing panel with couple of drop down inputs which will fetch the values and then refresh a chart. The chart initially loads fine and when I change the input and get the data again, I am getting a null pointer exception as shown below.

Code: Select all

UNCAUGHT EXCEPTION OCCURED IN THREAD: AWT-EventQueue-0 CLASS java.lang.NullPointerException CAUSE: java.lang.NullPointerException
java.lang.NullPointerException
	at com.steema.teechart.styles.Custom.checkPointInLine(Custom.java:537)
	at com.steema.teechart.styles.Custom.clicked(Custom.java:622)
	at com.steema.teechart.styles.Series.clicked(Series.java:3352)
	at com.steema.teechart.tools.DragPoint.clickedSeries(DragPoint.java:149)
	at com.steema.teechart.tools.DragPoint.mouseEvent(DragPoint.java:197)
	at com.steema.teechart.Chart.broadcastMouseEvent(Chart.java:1014)
	at com.steema.teechart.Chart.broadcastMouseEvent(Chart.java:1003)
	at com.steema.teechart.Chart.mouseMoved(Chart.java:1381)
	at com.steema.teechart.TChart.processMouseMotionEvent(TChart.java:1448)
	at java.awt.Component.processEvent(Component.java:6294)
	at java.awt.Container.processEvent(Container.java:2234)
	at java.awt.Component.dispatchEventImpl(Component.java:4881)
	at java.awt.Container.dispatchEventImpl(Container.java:2292)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4546)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
	at java.awt.Container.dispatchEventImpl(Container.java:2278)
	at java.awt.Window.dispatchEventImpl(Window.java:2750)
	at java.awt.Component.dispatchEvent(Component.java:4703)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
This is happening only if i change the inputs and get new data.Even though the chart object is resetting well, it somehow is being set to null. Here is my refresh code

Code: Select all

protected void initChart() {
        d_chart = new TChart();

        d_chart.getFooter().setVisible(false);
        d_chart.getHeader().setVisible(false);
        d_chart.getAspect().setView3D(false);

        Gradient g=d_chart.getPanel().getGradient();

        g.setDirection(GradientDirection.VERTICAL);

        g.setEndColor(new com.steema.teechart.drawing.Color(109,109,109));
        g.setMiddleColor(new com.steema.teechart.drawing.Color(149,202,255));
        g.setStartColor(new com.steema.teechart.drawing.Color(0,115,230));

        g.setVisible(true);

        d_chart.getAxes().getLeft().getAxisPen().setWidth(1);
        d_chart.getAxes().getBottom().getAxisPen().setWidth(1);

        d_chart.getHeader().getFont().setColor(new com.steema.teechart.drawing.Color(226,226,226));

        ColorPalettes.applyPalette(d_chart.getChart(), 10);

    }

    public void redrawChart(){
        d_chart.removeAllSeries();
        cop_DateOnly  start = d_dm.getStart();
        cop_DateOnly end = d_dm.getEnd();

        Line eLine = new Line(d_chart.getChart());
        eLine.setCursor(Cursor.HAND);
        eLine.setTitle("Hourly Weather");

        SeriesPointer tmpPointer = eLine.getPointer();
        tmpPointer.getBrush().setColor(Color.BLUE);
        tmpPointer.setInflateMargins(true);
        tmpPointer.getPen().setColor(Color.LIME);
        tmpPointer.setVisible(true);

        d_tool = new DragPoint(eLine);
        d_tool.setStyle(DragPointStyle.Y);
        // set the event...
        d_tool.addDragListener( new DragListener() {
            public void dragMoving(ChangeEvent e) {
//                d_chartGrid.repaint();
            }
            public void dragFinished(ChangeEvent e){
                updateDataModel();
            }

        });

        d_chartGrid = new ChartGrid(d_chart.getChart());
        d_chartGrid.setShowFields(true);

        // tell the chartGrid to show both the X and Y
        d_chartGrid.setShowXValues(ChartGridXValues.YES);

        // do not show labels at chart chartGrid
        d_chartGrid.setShowLabels(false);

        int i = 0;
        if(!cop_ArrayUtils.isEmpty(d_dm.getForecastedWeatherData()))
        {
            long st = start.getTime();
            for (bop_ForecastedWeatherHour sample : d_dm.getForecastedWeatherData()) {
                long time = sample.getForecastFor().getTime();
                time = time - st;
                time = time / (1000 * 60 * 60);
                if (sample.getForecastFor().compareTo(start) >= 0 && sample.getForecastFor().compareTo(end) <= 0) {
                    eLine.add((double) time, sample.getTemperature());
                }
            }
        }

        double maxValue = d_chart.getAxes().getLeft().getMaxYValue();
        double minValue = d_chart.getAxes().getLeft().getMinYValue();

        d_chart.getAxes().getLeft().setAutomatic(false);
        d_chart.getAxes().getLeft().setMaximum(maxValue + 10);
        d_chart.getAxes().getLeft().setMinimum(minValue - 10);

        eLine.repaint();
        drawStaticWeatherLine();
        d_chart.repaint();
    }

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Null Pointer Exception while refreshing the chart

Post by Yeray » Wed Sep 07, 2016 8:56 am

Hello,

I am not able to run your code because there are some properties, methods and classes being used but not initialized or incomplete. Ie d_dm object, updateDataModel and drawStaticWeatherLine methods, cop_ArrayUtils and bop_ForecastedWeatherHour classes. Also I can't see where the redrawChart method is being called.

However, looking at the code and at the exception it seems to suggest a mouse movement may be firing the clicked function (through the DragPoint tool) of a series which initialization hasn't been completed.
This kind of problems can happen when creating/populating series at the same time the chart is being redrawn, ie doing both things in different threads without using semaphores or similar to control the access.

A useful property on the chart is AutoRepaint. You could try setting it to false before modifying the chart series/data, set it back to true after doing it, and repainting the chart to finish the routine.

If you still find problems with it, please try to arrange a simple example project we can run as-is to reproduce the problem here.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply