Skip to content

Nullpointerexception #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Slake07 opened this issue Jul 1, 2014 · 2 comments
Closed

Nullpointerexception #1

Slake07 opened this issue Jul 1, 2014 · 2 comments
Assignees

Comments

@Slake07
Copy link

Slake07 commented Jul 1, 2014

when i am using RadCartenChartView, i got nullpointer error.

code:

  RadCartesianChartView chartView = new RadCartesianChartView(MainActivity.this);


    BarSeries barSeries = new BarSeries(this);
    barSeries.setCategoryBinding(new DataPointBinding() {
        @Override
        public Object getValue(Object o) {
            return ((MonthResult) o).getMonth();
        }
    });
    barSeries.setValueBinding(new DataPointBinding() {
        @Override
        public Object getValue(Object o) {
            return ((MonthResult) o).getResult();
        }
    });
    barSeries.setData(this.monthResults);
    chartView.getSeries().add(barSeries); --> nullpointer excpetion here..
@antony-jekov antony-jekov self-assigned this Jul 1, 2014
@antony-jekov
Copy link
Contributor

Hello segi07,

Thank you for writing.

After reviewing your code I noticed that you have a few potential problems. First of all you are trying to add series to the chart before defining axes. That would result in a NPE, since the series, once attached to the chart, are looking for axes, so they can calculate all data points in order to be ready for rendering. Make sure you add the axes before the series.

Also you need to add the chart to the layout at some point, otherwise nothing will show on the screen.

I am sending you the bare minimum to get your code snippet up and running:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.ViewGroup;

import com.telerik.widget.chart.engine.databinding.DataPointBinding;
import com.telerik.widget.chart.visualization.cartesianChart.RadCartesianChartView;
import com.telerik.widget.chart.visualization.cartesianChart.axes.CategoricalAxis;
import com.telerik.widget.chart.visualization.cartesianChart.axes.LinearAxis;
import com.telerik.widget.chart.visualization.cartesianChart.series.categorical.BarSeries;

import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {

    ArrayList<MonthResult> monthResults;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewGroup root = (ViewGroup) findViewById(R.id.layout_root);

        LinearAxis vAxis = new LinearAxis(this);
        CategoricalAxis hAxis = new CategoricalAxis(this);

        this.monthResults = new ArrayList<MonthResult>();
        this.monthResults.add(new MonthResult("Jan", 255));
        this.monthResults.add(new MonthResult("Feb", 555));
        this.monthResults.add(new MonthResult("Mar", 355));

        RadCartesianChartView chartView = new RadCartesianChartView(MainActivity.this);
        root.addView(chartView);

        BarSeries barSeries = new BarSeries(this);
        barSeries.setCategoryBinding(new DataPointBinding() {
            @Override
            public Object getValue(Object o) {
                return ((MonthResult) o).getMonth();
            }
        });

        barSeries.setValueBinding(new DataPointBinding() {
            @Override
            public Object getValue(Object o) {
                return ((MonthResult) o).getResult();
            }
        });

        barSeries.setData(this.monthResults);

        chartView.setVerticalAxis(vAxis);
        chartView.setHorizontalAxis(hAxis);

        chartView.getSeries().add(barSeries);
    }


    private class MonthResult {
        private String month;
        private int result;

        public MonthResult(String month, int result) {
            this.month = month;
            this.result = result;
        }

        public String getMonth() {
            return month;
        }

        public void setMonth(String month) {
            this.month = month;
        }

        public int getResult() {
            return result;
        }

        public void setResult(int result) {
            this.result = result;
        }
    }
}

And the xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</FrameLayout> 

This is the result of the code:
screenshot_2014-07-01-18-16-37 1

I am closing this issue. Please refer to our support system for better user experience, should you encounter any more problems using our controls - Go to http://www.telerik.com/support/android-ui and select the Dedicated Support link.

Thank you and good luck with your projects!

Regards,
Antony Jekov
Telerik Android Team

@Slake07
Copy link
Author

Slake07 commented Jul 2, 2014

Hello antony-jerkov

Thanks for response,

Actually i was refering telerik guides thats why i got error.

i need another help if you can..

chart-cartesian-sample-1

i need this type of bar chart with different colors.. i tried but get only one color..

another problem is, i want to remove below screen.. how can i.??

device-2014-07-02-110857

Thank you in advance.

Regards,
Sagar Maiyad
Android Developer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants