Mac122
Mac122

Reputation: 25

Why is AnyChart returning Null Object Reference exception?

I've been trying several tutorials for creating a simple pie chart using AnyChart and MPAndroidChart:

However, when I run all the examples above I get an exception:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference
        at com.anychart.APIlib.addJSLine(APIlib.java:27)
        at com.anychart.charts.Pie.<init>(Pie.java:34)
        at com.anychart.AnyChart.pie(AnyChart.java:130)
        at com.mpereira.savingstrackerapp.Activities.TestActivity.setupPieChart(TestActivity.java:37)
        at com.mpereira.savingstrackerapp.Activities.TestActivity.onCreate(TestActivity.java:33)

I've added the dependencies accordingly and here's the xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".Activities.TestActivity">

    <com.anychart.AnyChartView
        android:id="@+id/any_chart_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

here's the activity's code: (this is from https://www.youtube.com/watch?v=qWBA2ikLJjU)

public class TestActivity extends AppCompatActivity {

    private static String TAG = "MainActivity";
    AnyChartView anyChartView;
    String[] months = {"Jan", "Feb", "Mar"};
    int[] earnings = {500, 800, 350};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: starting to create chart");
        anyChartView = (AnyChartView) findViewById(R.id.any_chart_view);

        setupPieChart();
    }

    void setupPieChart(){
        Pie pie = AnyChart.pie();
        List<DataEntry> dataEntries = new ArrayList<>();
        for (int i=0;i<months.length; ++i){
            dataEntries.add(new ValueDataEntry(months[i], earnings[i]));
        }
        pie.data(dataEntries);
        anyChartView.setChart(pie);
    }
}

Here's the screenshot: exception

I know that we usually need keyword new for creating an object, but according to the tutorial https://github.com/AnyChart/AnyChart-Android/wiki/Getting-started, we don't need it in Pie pie = AnyChart.pie();

Appreciate any hint on how to fix it.

Upvotes: 0

Views: 1640

Answers (4)

thundernhut
thundernhut

Reputation: 11

I guess you must have called

Pie pie = AnyChart.pie();

before

anyChartView = (AnyChartView) findViewById(R.id.any_chart_view);

and you haven't realized that it's the cause of problem, change the order of calls will result in great trouble. I use AnyChart version 1.1.0

(implementation "com.github.AnyChart:AnyChart-Android:1.1.0")

As my finding, it is mandatory to (temporarily call) "mention" about AnyChartView previously before you instantiate a Chart class to have a chart object by calling something like AnyChart.line() or AnyChart.pie(). In detail, for example in Android Kotlin language:

//Step 1: you must write this first
    mychartView3 = llayout.findViewById<AnyChartView>(R.id.mychartView3)
    mychartView.layoutParams = LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, 500)//if you don't set it height to a fixed number, 
                                it might not be displayed on your phone at all so you can not see or touch it.
//Step 2: you have to call AnyChart.[chart-name's constructor]()
    val pie = AnyChart.pie()
//Step 3: Set chart 
    APIlib.getInstance().setActiveAnyChartView(mychartView)
    mychartView.setChart(pie)
//Step 4: if you want to show multi-charts on 1 activity screen, you have to follow strictly this protocol/procedure: 
    APIlib.getInstance().setActiveAnyChartView(mychartView)
    mychartView.setChart(loadlinechart())
    APIlib.getInstance().setActiveAnyChartView(mychartView2)
    mychartView2.setChart(loadlinechart1())
    APIlib.getInstance().setActiveAnyChartView(mychartView3)
    mychartView3.setChart(loadPolarChart())

There is 1 more tricky thing to overcome that is if you merely see a blank white area, just close your app and unload it from memory then reopen it then you'll see your chart there.

Upvotes: 0

chirag garg
chirag garg

Reputation: 36

According to this https://www.youtube.com/watch?v=qWBA2ikLJjU)

no need to add cast AnyChart View

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: starting to create chart");
        anyChartView = findViewById(R.id.any_chart_view);

        setupPieChart();
    }

Upvotes: 0

AnyChart Support
AnyChart Support

Reputation: 3905

Check your XML (you are using relative layout), imports in the main activity, API level you are using, and make sure that you have added AnyChart library to Gradle correctly according to Gettings Started guide. Your main activity code is ok, I have successfully built a chart based on your code.

Here is the result: enter image description here

The xml file content is the following:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.anychart.AnyChartView
        android:id="@+id/any_chart_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Compare your xml with the one provided above.

Upvotes: 0

Sandeep dhiman
Sandeep dhiman

Reputation: 1921

According to https://www.anychart.com/technical-integrations/samples/android-charts/ tutorial your layout code should be like this

  <com.anychart.anychart.AnyChartView
    android:id="@+id/any_chart_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>


and you have mentioned like this

<com.anychart.AnyChartView
    android:id="@+id/any_chart_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />


which is not correct thats why you are getting null pointer exception

Upvotes: 2

Related Questions