title: Switch case not running
body: <p>I take input from the user the number 
  of balls he/she want to bounce.Then I am using switch case to write further code according to number user entered.
  When i run project,error is displayed "Unfortunately ,application has stopped" when I try to move to activity_two .Is the problem is that variable 'value' is not initialized before calling switch or some other problem?And whts the solution...I have tried many things with that..</p>
  
  <p>SecondActivity.java</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.RectF;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  
  public class SecondActivity extends Activity {
      AnimationView animationview;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Intent i = getIntent(); 
          int text = i.getIntExtra("TextBox",3); 
          System.out.println(text);
          setContentView(R.layout.activity_two);
              animationview=(AnimationView)findViewById(R.id.animationView);
          animationview.setString(text);
  
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>AnimationView.java</p>
  
  <pre><code>import android.content.Context;
  
  import android.content.Intent;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.os.Handler;
  import android.util.AttributeSet;
  import android.app.Activity;
  import android.view.View;
  import android.widget.Toast;
  public class AnimationView extends View{
    private final int FRAME_RATE=15;
    private Paint paint;
    private Handler h;
    Ball myball;
    Ball greenball;
    Ball redball;
    Ball cyanball;
    Ball yellowball;
    Ball greyball;
    Ball magentaball;
    Ball grey;
    int value;
    public void setString(int value)
    {
       this.value = value;
       System.out.println("value="+value);
    }
  
      public AnimationView(Context context,AttributeSet attrs) {
          super(context,attrs);
          // TODO Auto-generated constructor stub
  
          h=new Handler();
          paint=new Paint();
          paint.setColor(Color.BLUE);
          switch(value)
          {
          case 1:
              myball=new Ball(100,100,Color.BLUE,50);
              myball.setDX(10);
              myball.setDY(10);
              break;
          case 2:
              myball=new Ball(100,100,Color.BLUE,50);
              greenball=new Ball(200,200,Color.GREEN,50);
              myball.setDX(10);
              myball.setDY(10);
              greenball.setDX(10);
              greenball.setDY(10);
              break;
          case 3:
              myball=new Ball(100,100,Color.BLUE,50);
              greenball=new Ball(200,200,Color.GREEN,50);
              redball=new Ball(50,400,Color.RED,50);
              myball.setDX(10);
              myball.setDY(10);
              greenball.setDX(10);
              greenball.setDY(10);
              redball.setDX(10);
              redball.setDY(10);
              break;
          case 4:
              myball=new Ball(100,100,Color.BLUE,50);
              greenball=new Ball(200,200,Color.GREEN,50);
              redball=new Ball(50,400,Color.RED,50);
              cyanball=new Ball(100,300,Color.CYAN,50);
              myball.setDX(10);
              myball.setDY(10);
              greenball.setDX(10);
              greenball.setDY(10);
              redball.setDX(10);
              redball.setDY(10);
              cyanball.setDX(10);
              cyanball.setDY(10);
              break;
          case 5:
              myball=new Ball(100,100,Color.BLUE,50);
              greenball=new Ball(200,200,Color.GREEN,50);
              redball=new Ball(50,400,Color.RED,50);
              cyanball=new Ball(100,300,Color.CYAN,50);
              yellowball=new Ball(10,300,Color.YELLOW,50);
              myball.setDX(10);
              myball.setDY(10);
              greenball.setDX(10);
              greenball.setDY(10);
              redball.setDX(10);
              redball.setDY(10);
              cyanball.setDX(10);
              cyanball.setDY(10);
              yellowball.setDX(10);
              yellowball.setDY(10);
              break;
          case 6: 
              myball=new Ball(100,100,Color.BLUE,50);
              greenball=new Ball(200,200,Color.GREEN,50);
              redball=new Ball(50,400,Color.RED,50);
              cyanball=new Ball(100,300,Color.CYAN,50);
              yellowball=new Ball(10,300,Color.YELLOW,50);
              magentaball=new Ball(150,300,Color.MAGENTA,50);
              myball.setDX(10);
              myball.setDY(10);
              greenball.setDX(10);
              greenball.setDY(10);
              redball.setDX(10);
              redball.setDY(10);
              cyanball.setDX(10);
              cyanball.setDY(10);
              yellowball.setDX(10);
              yellowball.setDY(10);
              magentaball.setDX(10);
              magentaball.setDY(10);
              break;
          case 7:
              myball=new Ball(100,100,Color.BLUE,50);
              greenball=new Ball(200,200,Color.GREEN,50);
              redball=new Ball(50,400,Color.RED,50);
              cyanball=new Ball(100,300,Color.CYAN,50);
              yellowball=new Ball(10,300,Color.YELLOW,50);
              magentaball=new Ball(150,300,Color.MAGENTA,50);
              grey=new Ball(100,150,Color.GRAY,50);
              myball.setDX(10);
              myball.setDY(10);
              greenball.setDX(10);
              greenball.setDY(10);
              redball.setDX(10);
              redball.setDY(10);
              cyanball.setDX(10);
              cyanball.setDY(10);
              yellowball.setDX(10);
              yellowball.setDY(10);
              magentaball.setDX(10);
              magentaball.setDY(10);
              grey.setDX(10);
              grey.setDY(10);
              break;
              default:System.out.println("invalid value");
  
  
          }
  
  
  
      }
      protected void onDraw(Canvas c)
      {
          switch(value)
          {
          case 1:
              myball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              break;
          case 2:
              myball.bounce(c);
              greenball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              break;
          case 3:
              myball.bounce(c);
              greenball.bounce(c);
              redball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              break;
          case 4:
              myball.bounce(c);
              greenball.bounce(c);
              redball.bounce(c);
              cyanball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
              break;
          case 5:
              myball.bounce(c);
              greenball.bounce(c);
              redball.bounce(c);
              cyanball.bounce(c);
              yellowball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
              c.drawCircle(yellowball.getX(), yellowball.getY(),yellowball.getRadius(), yellowball.getPaint());
              break;
          case 6:
  
              myball.bounce(c);
              greenball.bounce(c);
              redball.bounce(c);
              cyanball.bounce(c);
              yellowball.bounce(c);
              magentaball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
              c.drawCircle(yellowball.getX(), yellowball.getY(),yellowball.getRadius(), yellowball.getPaint());
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
              c.drawCircle(magentaball.getX(), magentaball.getY(),magentaball.getRadius(), magentaball.getPaint());
              break;
  
          case 7:
              myball.bounce(c);
              greenball.bounce(c);
              redball.bounce(c);
              cyanball.bounce(c);
              yellowball.bounce(c);
              magentaball.bounce(c);
              greyball.bounce(c);
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
              c.drawCircle(yellowball.getX(), yellowball.getY(),yellowball.getRadius(), yellowball.getPaint());
              c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
              c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
              c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
              c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
              c.drawCircle(magentaball.getX(), magentaball.getY(),magentaball.getRadius(), magentaball.getPaint());
              c.drawCircle(greyball.getX(), greyball.getY(),greyball.getRadius(), greyball.getPaint());
              break;
          default:System.out.println("invalid value");
          }
  
  h.postDelayed(r, FRAME_RATE);
  
      }
      private Runnable r=new Runnable()
      { public void run()
      { invalidate();
      }
      };
      }
  </code></pre>
  
  <p>Logcat error messages</p>
  
  <pre><code>4-08 19:34:59.610: E/AndroidRuntime(660): FATAL EXCEPTION: main
  04-08 19:34:59.610: E/AndroidRuntime(660): java.lang.NullPointerException
  04-08 19:34:59.610: E/AndroidRuntime(660):  at com.example.preliminarytest.AnimationView.onDraw(AnimationView.java:164)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13458)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13342)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13340)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13461)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.widget.FrameLayout.draw(FrameLayout.java:467)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13342)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13340)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.View.draw(View.java:13461)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.widget.FrameLayout.draw(FrameLayout.java:467)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2183)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2258)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewRootImpl.draw(ViewRootImpl.java:2153)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2021)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1832)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.Choreographer.doFrame(Choreographer.java:525)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.os.Handler.handleCallback(Handler.java:615)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.os.Handler.dispatchMessage(Handler.java:92)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.os.Looper.loop(Looper.java:137)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at android.app.ActivityThread.main(ActivityThread.java:4745)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at java.lang.reflect.Method.invokeNative(Native Method)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at java.lang.reflect.Method.invoke(Method.java:511)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  04-08 19:34:59.610: E/AndroidRuntime(660):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>activity_two.xml</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity" &gt;
  
     &lt;com.example.preliminarytest.AnimationView
         android:id= "@+id/animationView"
         android:layout_width="match_parent"
      android:layout_height="match_parent"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>