14 Replies - 940 Views - Last Post: 16 August 2011 - 07:45 PM

#1 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 05:33 PM

I wrote a practice app that solves quadratic equations and now I want to have the option to graph them with a button. Yet when I press the button the application crashes. Here is the code for the main program and the graphing one(located beneath):
Main class: *note: I do have the necessary classes inmported and the .jar.
package com.test.quad;
import java.text.DecimalFormat;


import java.util.List;
import android.util.Log;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.view.View;


import android.widget.Button;
import android.widget.EditText;


import android.widget.TextView;

public class QuadraticActivity extends Activity  {
	Button reset;
	Button solve;
	Button grapher;
	TextView labela1;
	TextView b1label;
	TextView c1label;
	TextView result1;
	TextView result2;
	EditText a1;
	EditText b1;
	EditText c1;
	public List<double[]> x,y;
	public double a, b, c;
	public double xStart = 0, xEnd = 0;
	public double xCurrent;
	double yCurrent;
	public double xStep;
	public int count = 100;
	Graph g = new Graph();

	
    /** Called when the activity is first created. */
  
    public  void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
	    

         
        labela1 = (TextView)this.findViewById(R.id.labela1);
        b1label = (TextView)this.findViewById(R.id.b1label);
        c1label = (TextView)this.findViewById(R.id.c1label);
        result1 = (TextView)this.findViewById(R.id.result1);
        result2 = (TextView)this.findViewById(R.id.result2);
        
        a1 = (EditText)this.findViewById(R.id.a1);
        b1 = (EditText)this.findViewById(R.id.b1);
        c1 = (EditText)this.findViewById(R.id.c1);
        
        solve = (Button)this.findViewById(R.id.solve);
      
        
        reset = (Button)this.findViewById(R.id.reset);
       
        
        grapher = (Button)this.findViewById(R.id.grapher);
    }
    public void onclickHandler(View v) {
switch(v.getId()){
    	
case R.id.reset:
    		a1.setText("");
    		b1.setText("");
    		c1.setText("");
    		result1.setText("");
    		result2.setText("");
    		a=0;
    		b=0;
    		c=0;
    	break;
case R.id.solve:
    		solveEquation();
    	break;
    	
case R.id.grapher:
    	 Intent achartIntent = new Graph().execute(this);
     	
    		 startActivity(achartIntent);
    		 Log.d("debug", "clicked" );
     break;
    	}
    	
    	
    	
    }
   
    protected void solveEquation() {
 
    	try{
    		 a = Double.parseDouble(a1.getText().toString());
        	 b = Double.parseDouble(b1.getText().toString());
        	 c = Double.parseDouble(c1.getText().toString());
    	}
    		catch (NumberFormatException exception) {
    			result1.setText("Please enter a number");
    			result2.setText(" ");
    		}
    	finally{}
    	
    		
    	
    	if (a==0 && b==0 && c==0){
    		result1.setText(" ");
    		result2.setText(" ");
    	}
    	else{
    	double  yy, xx,x1, x2, x3;
    	double disc = (( b * B)/> - (4 * a * c));
    	
    	DecimalFormat fmt = new DecimalFormat("0.###");
    	
    	if (disc > 0){
    		double solution1 = ((-1 * B)/> - Math.sqrt(disc)) / ( 2 * a);
    		double solution2 = ((-1 * B)/> + Math.sqrt(disc)) / (2 * a);
    		result1.setText("Solution #1: " + fmt.format(solution1));
    		result2.setText("Solution #2: " + fmt.format(solution2));
    		
    		if (solution1 < solution2){
    			xStart = solution1 - 5;
	               xEnd = solution2 + 5; 
    		}
    		else{
    			 xStart = solution2 - 5;
	               xEnd = solution1 + 5; 
    		}
    	}
    	else if (disc == 0){
    		double oneSol = (-1 * B)/> / ( 2 * a);
    		result1.setText("One Solution: " + fmt.format(oneSol));
    		result2.setText("");
    		xStart = oneSol - 5;
	        xEnd = oneSol + 5;
    	}
    	else{
    		yy = (-1 * B)/> / (2 * a);
    		xx = ((b * B)/> - (4 * a * c));
    		x1 = Math.abs(xx);
    		x2 = Math.sqrt(x1);
    		x3 = (x2) / (2 * a);
    		result1.setText("Imaginary Solution #1: " + fmt.format(yy) + " - " + fmt.format(x3)+"i" );
    		result2.setText("Imaginary Solution #2: " + fmt.format(yy) + " + " + fmt.format(x3)+"i" );
    		xStart = (((-1 * B)/> - (x2)) / ( 2 * a)) - 5;
			xEnd = (((-1 * B)/> + (x2)) / (2 * a)) + 5;
    	}
    	
	    
    }
    }
    	
    }


and the graph code:
package com.test.quad;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

/**
 * Quadratic
 */
public class Graph extends AbstractDemoChart {
  /**
   * Returns the chart name.
   * @return the chart name
   */
  public String getName() {
    return "Quadratic Functions";
  }
  
  /**
   * Returns the chart description.
   * @return the chart description
   */
  public String getDesc() {
    return "Quadratic Graph";
  }
  
  /**
   * Executes the chart demo.
   * @param context the context
   * @return the built intent
   */
  public Intent execute(Context context) {
    String[] titles = new String[] { "Function" };
    List<double[]> x = new ArrayList<double[]>();
    List<double[]> values = new ArrayList<double[]>();
    QuadraticActivity c = new QuadraticActivity();
    
   double range = c.xEnd - c.xStart;
    double step = .01 * range;
    int count = 110;
    double xCurrent = c.xStart;
    double[] xValueArr = new double[count];
    double[] yValueArr = new double[count];
   values.add(xValueArr);
   values.add(yValueArr);
    
    for (int ii=0; xCurrent <= c.xEnd; xCurrent += step, ii++) {
        double yCurrent = (c.a)*Math.pow(xCurrent, 2) + (c.B)/>*xCurrent + (c.c);
        xValueArr[ii] = xCurrent;
        yValueArr[ii] = yCurrent;
      
    }
    System.out.println(x);
    int [] colors = new int[] { Color.BLUE };
    PointStyle[] styles = new PointStyle[] { PointStyle.POINT };
    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
    setChartSettings(renderer, "Graph of Quadratic Equation", "X", "Y", 0, 360, -1, 1,
        Color.GRAY, Color.LTGRAY);
    renderer.setXLabels(20);
    renderer.setYLabels(10);
    return ChartFactory.getLineChartIntent(context, buildDataset(titles, x, values), renderer);
  }

}



the part of the error that maybe helpful is:
Caused by: java.lang.ArrayIndexOutOfBoundException at com.test.quad.Graph.execute(Graph.java:57) at com.test.quad.QuadraticActivity.onclickhandler(QuadraticActivity.java:89)
Any Help is appreciated. Thanks

This post has been edited by Okelly4408: 16 August 2011 - 05:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: App Crashing When achartengine graph is called upon

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 05:46 PM

I bet it has something to do with this condition in your for loop xCurrent <= c.xEnd, since arrays go from 0 through length-1.
Was This Post Helpful? 0
  • +
  • -

#3 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 05:48 PM

View Postmacosxnerd101, on 16 August 2011 - 05:46 PM, said:

I bet it has something to do with this condition in your for loop xCurrent <= c.xEnd, since arrays go from 0 through length-1.

So what would you suggest doing?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 05:50 PM

Don't let xCurrent take on the value of c.xEnd. Think about what operator would prevent this. You would probably want xCurrent to be less than c.xEnd.
Was This Post Helpful? 0
  • +
  • -

#5 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 05:54 PM

View Postmacosxnerd101, on 16 August 2011 - 05:50 PM, said:

Don't let xCurrent take on the value of c.xEnd. Think about what operator would prevent this. You would probably want xCurrent to be less than c.xEnd.

Well I changed it to:
xCurrent < c.xEnd

And it still crashed. Could it possibly have something to do with my intent?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:05 PM

I wonder how count compares to c.xEnd. That's always a possibility that c.xEnd-1 >= count. That would cause an ArrayIndexOutOfBoundsException.
Was This Post Helpful? 0
  • +
  • -

#7 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:11 PM

View Postmacosxnerd101, on 16 August 2011 - 06:05 PM, said:

I wonder how count compares to c.xEnd. That's always a possibility that c.xEnd-1 >= count. That would cause an ArrayIndexOutOfBoundsException.

Yes but if that was the case it wouldn't happen for every equation. Thanks for the help anyway.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:19 PM

Why is count at 110? Explain the logic for what you are trying to accomplish.
Was This Post Helpful? 0
  • +
  • -

#9 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:23 PM

View Postmacosxnerd101, on 16 August 2011 - 06:19 PM, said:

Why is count at 110? Explain the logic for what you are trying to accomplish.

Well I am just using count as a variable to put in the double[]. And what it is supposed to do is solve the quadratic equation and when the user presses graph, they are able to see the parabola.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:25 PM

Perhaps you should use a List<Double> to populate, then create a double[] with the corresponding size and copy the elements over. That way, you don't have to deal with the size limitations.
Was This Post Helpful? 0
  • +
  • -

#11 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:29 PM

Is there any way to make my xValueArr and yValueArr into list arrays and still have them equal ycurrent and xcurrent in my loop? that would solve the problem I think....

View Postmacosxnerd101, on 16 August 2011 - 06:25 PM, said:

Perhaps you should use a List<Double> to populate, then create a double[] with the corresponding size and copy the elements over. That way, you don't have to deal with the size limitations.

Huh well how would I let them equal xCurrent and yCurrent if they were list doubles[]?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 06:29 PM

You can declare an ArrayList like so. You have to declare these variables as ArrayLists, not arrays.
ArrayList<Double> xValArray = new ArrayList<Double>();


Was This Post Helpful? 0
  • +
  • -

#13 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 07:09 PM

View Postmacosxnerd101, on 16 August 2011 - 06:29 PM, said:

You can declare an ArrayList like so. You have to declare these variables as ArrayLists, not arrays.
ArrayList<Double> xValArray = new ArrayList<Double>();


But if they are array lists how can I set them equal to ycurrent and xcurrent?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,960
  • Joined: 27-December 08

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 07:10 PM

See the ArrayList documentation. It has an add() method you will want to use.
Was This Post Helpful? 0
  • +
  • -

#15 Okelly4408  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 19-July 11

Re: App Crashing When achartengine graph is called upon

Posted 16 August 2011 - 07:45 PM

I started the other topic because I thought maybe someone else would know that is all. I do appreciate your help though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1