title: Inserting data to database in the onClick method
body: <p>MainActivity:</p>
  
  <pre><code>public class MainActivity extends Activity implements SensorEventListener, OnClickListener {
  
      private SensorManager mSensorManager;
      private Sensor mAcc;
      TextView xaxis,yaxis,zaxis,ballxy,gps,statics;
      public float velocity = 10.6f;
      public float xPosition,yPosition,zPosition;
      public static float staticXposition,staticYposition,staticZposition;
      private Database myDBAdapter;
  
      public ContentValues values;
  
      public String GPSposition;
      public SQLiteDatabase db;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
          mAcc = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  
          LocationManager locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
          LocationListener locList = new LocationListener() {
  
              @Override
              public void onStatusChanged(String provider, int status, Bundle extras) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onProviderEnabled(String provider) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onProviderDisabled(String provider) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onLocationChanged(Location location) {
                  gps.setText("GPS:"+location.getLatitude()+";"+location.getLongitude());
                  GPSposition = location.getLatitude() + " " + location.getLongitude();
  
              }
          };
          locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locList);
  
  
          // inicjowanie TextView
          gps = (TextView) findViewById(R.id.gps);
          xaxis = (TextView) findViewById(R.id.xaxis);
          yaxis = (TextView) findViewById(R.id.yaxis);
          zaxis = (TextView) findViewById(R.id.zaxis);
          ballxy = (TextView) findViewById(R.id.ballxy);
          statics = (TextView) findViewById(R.id.statics);
  
  
  
          Button start = (Button) findViewById(R.id.start);
          start.setOnClickListener(this);
  
  
          myDBAdapter = new Database(this).open();
          //TODO Any database operations
          myDBAdapter.close();
  
  
      }
  
  
      public void insertData(){
  
  
          String time;
          Time now = new Time();
          now.setToNow();
          time = now.toString();
  
          values.put("ID", "1");
          values.put("LEFT_POSITION", xPosition);
          values.put("RIGHT_POSITION", yPosition);
          values.put("GPS", GPSposition);
          values.put("TIME", time);
  
          if(db!=null){
              db.insert("baza", null, values);    
          }else{
              Log.d("AppName","db is null");
          }
  
  
  
      }
  
      @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;
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public final void onSensorChanged(SensorEvent event) {
          ImageView ball = (ImageView) findViewById(R.id.ball);
          xPosition = event.values[0] - staticXposition;
          yPosition = event.values[1] - staticYposition;
          zPosition = event.values[2] - staticZposition;
  
      }
  
  
  
  
      @Override
      protected void onPause() {
          // TODO Auto-generated method stub
          super.onPause();
          mSensorManager.unregisterListener(this);
      }
  
      @Override
      protected void onResume() {
          // TODO Auto-generated method stub
          super.onResume();
          mSensorManager.registerListener(this, mAcc, SensorManager.SENSOR_DELAY_NORMAL);
      }
  
  
  
  
      @Override
      public void onClick(View v) {
  
          switch(v.getId()){
          case R.id.start:
  
              staticXposition = xPosition;
              staticYposition = yPosition;
              staticZposition = zPosition;
  
              insertData();
  
  
          }
  
      }
  
  
  }
  </code></pre>
  
  <p>I created method with contentValues:</p>
  
  <pre><code>public void insertData(){
  
  
          String time;
          Time now = new Time();
          now.setToNow();
          time = now.toString();
  
          values.put("ID", "2");
          values.put("LEFT_POSITION", xPosition);
          values.put("RIGHT_POSITION", xPosition);
          values.put("GPS", GPSposition);
          values.put("TIME", time);
  
          db.insert("baza", null, values);
  
  
      }
  </code></pre>
  
  <p>And I'm trying to send data into database "baza", when user will push the button.</p>
  
  <pre><code>public void onClick(View v) {
  
          switch(v.getId()){
          case R.id.start:
  
              staticXposition = xPosition;
              staticYposition = yPosition;
              staticZposition = zPosition;
                  insertData();
  
  
          }
  
      }
  </code></pre>
  
  <p>I have some variables: xPosition, yPosition and zPosition. Data in this variables are dynamic, dependent on accelelometer. I'm trying to send this data to my database, but I have a error: Fatal exception main. Null pointer exception. Where I have a mistake?</p>
  
  <pre><code>01-12 12:42:17.093: E/AndroidRuntime(9216): FATAL EXCEPTION: main
  01-12 12:42:17.093: E/AndroidRuntime(9216): java.lang.NullPointerException
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at pl.pawelfrydrych.flyingball.MainActivity.insertData(MainActivity.java:123)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at pl.pawelfrydrych.flyingball.MainActivity.onClick(MainActivity.java:218)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at android.view.View.performClick(View.java:4240)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at android.view.View$PerformClick.run(View.java:17721)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at android.os.Handler.handleCallback(Handler.java:730)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at android.os.Handler.dispatchMessage(Handler.java:92)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at android.os.Looper.loop(Looper.java:137)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at java.lang.reflect.Method.invoke(Method.java:525)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  01-12 12:42:17.093: E/AndroidRuntime(9216):     at dalvik.system.NativeStart.main(Native Method)
  01-12 12:42:19.673: I/Process(9216): Sending signal. PID: 9216 SIG: 9
  </code></pre>