0

I have this code in an Android activity;

public static TwoWeeksActivity context;
public static DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_two_weeks);

    RadCartesianChartView chartView = new RadCartesianChartView(this);

    ViewGroup rootView = (ViewGroup)findViewById(R.id.container);
    rootView.addView(chartView);

    ArrayList<DateCountChart> vvv=new ArrayList<DateCountChart>();

    vvv=dbHelper.getCigCountTwoWeeks();

When I execute the app, when it arrives at tthis last line it returns java null pointer exception.

Why vvv is null? I have instantiated it:

ArrayList<DateCountChart> vvv=new ArrayList<DateCountChart>();

Please note that

 dbHelper.getCigCountTwoWeeks();

function in in dbhelper returns ArrayList of DateCountChart object

Any help appreciated.

Paolo Anghileri
  • 457
  • 5
  • 14
  • please also show your getCigCountTwoWeeks function – bloodscript Feb 15 '17 at 14:46
  • 3
    Have you instantiated the dbHelper? As it is most likley that object that returns null. – vegaasen Feb 15 '17 at 14:46
  • 1
    Do NOT keep a static activity field. – wrkwrk Feb 15 '17 at 14:48
  • @vegaasen I forgot instantiating the dbHelper: this.dbHelper=new DBHelper(this); Thanks! – Paolo Anghileri Feb 15 '17 at 14:48
  • In the last line, it doesn't even matter if you have already initialised vvv. As vegaasen said, it is dbHelper. – Axel Feb 15 '17 at 14:48
  • I agree with vegaasen, it seems that you have not instantiated dbHelper. In the other hand, this instantiation is useless `ArrayList vvv=new ArrayList();` if the following sentence is `vvv=dbHelper.getCigCountTwoWeeks();`. Just do `ArrayList vvv=dbHelper.getCigCountTwoWeeks();` – RubioRic Feb 15 '17 at 14:49
  • After instantiating dbHelper everything works. Thanks everyone! I cannot upvote you since I do not have 15 reputations yet. – Paolo Anghileri Feb 15 '17 at 15:04

1 Answers1

0

you should to instantiated dpHelper object. I think this is the main problem.
also you can put logcat screen shot to review.

Ramzy Hassan
  • 926
  • 8
  • 21