1

I was trying to make a graph based on the two array values, i.e conc and optical density. I have created two activities for saving those arrays and pass the array list using bundle and intent to the third activity using string. Whenever I try to run the code, my app crashes.

GraphView gpView;
String co;
String od;
Float x,y;
int l1,l2;
DataPoint [] dp;

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

    Bundle bundle1 = getIntent().getExtras();
    ArrayList<String> array1 = (ArrayList<String>) bundle1.getStringArrayList("Conc");

    Bundle bundle2 = getIntent().getExtras();
    ArrayList<String> array2 = (ArrayList<String>) bundle2.getStringArrayList("Od");


    gpView= findViewById(R.id.graph);
    LineGraphSeries <DataPoint> series = new LineGraphSeries<>();

    l1 = array1.size();
    l2 = array2.size();

    if((array1.contains(true))&&(array2.contains(true)))
    {
        for (int c1 = 0; c1<l1; c1++)
        {
            for(int c2=0; c2<l2; c2++ ) {
                co = array1.get(c1);
                od = array2.get(c2);
                x = Float.parseFloat(co);
                y = Float.parseFloat(od);
                dp[c1] = new DataPoint(x,y);
            }
        }
        gpView.addSeries(series);
    }



}

Alternately I tried to run a simple input method and it worked using this

    private DataPoint[] getDataPoint ()
    {
        dp = new DataPoint[]
                {
                        new DataPoint(10, 0.2),
                        new DataPoint(20, 0.38),
                        new DataPoint(30, 0.45),
                        new DataPoint(40, 0.69),
                        new DataPoint(50, 0.81),
                };
        return (dp);
    }
Lokesh Sharma
  • 13
  • 1
  • 3
  • post your log cat . and what are you trying to do with this line `if((array1.contains(true))&&(array2.contains(true)))` – Abu Yousuf Mar 18 '18 at 05:50
  • @AbuYousuf i had created an if statement to see that if the array contained elements or not. Also, I apologize now but I don't know how to post log cat. Could you please help me? – Lokesh Sharma Mar 18 '18 at 19:04
  • Check this https://developer.android.com/studio/debug/am-logcat.html for Logcat. – Abu Yousuf Mar 19 '18 at 02:31

1 Answers1

0

I think you haven't initialized your dp array. Initialize your array

dp = new DataPoint[l1];

before if statement if((array1.contains(true))&&(array2.contains(true)))

Edit:

May be you are having NullPointerException. Check array1 and array2 is not null

if(array1 == null){
  Toast.makeText(this, "array1 is null", Toast.LENGTH_LONG).show();
}


if(array2 == null){
  Toast.makeText(this, "array2 is null", Toast.LENGTH_LONG).show();
}

if(array1 != null){
   l1 = array1.size();
}


if(array2 != null){
   l2 = array2.size();
}

if(l1 > 0){

 dp = new DataPoint[l1];

 for (int c1 = 0; c1<l1; c1++)
 {
     for(int c2=0; c2<l2; c2++ ) {
         co = array1.get(c1);
         od = array2.get(c2);
         x = Float.parseFloat(co);
         y = Float.parseFloat(od);
         dp[c1] = new DataPoint(x,y);
     }
 }
 gpView.addSeries(series);
}

if array1 and array2 is null then make sure you are passing both arraylist via Intent.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
  • You have to to post `Logcat`. Else its tough to guess where is the problem – Abu Yousuf Mar 21 '18 at 00:27
  • Sorry for that late reply. Here is the link, I have attached the logical and the project folder as well. https://mega.nz/#F!yQUxUZDC!f7jphvz3yXFdmL2tDff_ag – Lokesh Sharma Mar 22 '18 at 19:34