Skip to content

Android Null Pointer Exception for onclick listener

The following code is giving me null pointer exception even though I can’t find anything wrong with the code. Tried commenting it out and it seems to be working. But still not able to find out what is wrong with the code? tried my own trouble shooting of commenting out the code and such and right now the code below is giving me nullpointer exception

Log Cat

12-19 05:49:14.833: E/AndroidRuntime(3392): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)’ on a null object reference 12-19 05:49:14.833: E/AndroidRuntime(3392): at com.techiequickie.bharath.boadraf.newBet_activity.onCreate(


package com.techiequickie.bhr.boadraf;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import databasehandler.DatabaseHandler;
import databasehandler.UserFunctions;

 * Created by YP on 17-Nov-14.
public class newBet_activity extends ActionBarActivity
    EditText inputBetname, inputBetbrief, inputBetdescription;
    SeekBar valueSeekbar; //= null;
    Button placeBet;

    // JSON Response node names
    private static String KEY_SUCCESS = "success";
    private static String KEY_ERROR = "error";
    private static String KEY_ERROR_MSG = "error_msg";
    //private static String KEY_UID = "uid";
    private static String KEY_BETNAME = "bet_name";
    private static String KEY_BETBRIEF = "bet_brief";
    private static String KEY_BETDESCRIPTION = "bet_description";
    private static String KEY_BETVALUE = "bet_value";
   // private static String KEY_CREATED_AT = "created_at";

    protected void onCreate(Bundle savedInstanceState)
        // Importing all assets like buttons, text fields
        inputBetname = (EditText) findViewById(;
        inputBetbrief = (EditText) findViewById(;
        inputBetdescription = (EditText) findViewById(;
        placeBet = (Button) findViewById(;

        //valueSeekbar = (SeekBar) findViewById(;


         new SeekBar.OnSeekBarChangeListener()
            int progressChanged = 0;

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                progressChanged = progress;

            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(newBet_activity.this, "seek bar progress:" + progressChanged, Toast.LENGTH_SHORT).show();

        placeBet.setOnClickListener(new View.OnClickListener()

            public void onClick(View view)
                String betname = inputBetname.getText().toString();
                String betBrief = inputBetbrief.getText().toString();
                String betDescription = inputBetdescription.getText().toString();
                //StringBuilder betValue = ((toString()) valueSeekbar.getProgress());

                //new BetRegisterTask().execute(betname,betBrief,betDescription);


        class BetRegisterTask extends AsyncTask<String, Void, String>

            protected String doInBackground(String... params)

                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.createBet(params[0], params[1], params[2]);

                // check for login response
                    if (json.getString(KEY_SUCCESS) != null)

                        String res = json.getString(KEY_SUCCESS);
                        if (Integer.parseInt(res) == 1)
                            // user successfully registred
                            // Store user details in SQLite Database
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                            JSONObject json_user = json.optJSONObject("user");

                            // Clear all previous data in database
                            db.newBet(json_user.getString(KEY_BETNAME), json_user.getString(KEY_BETBRIEF), json.getString(KEY_BETDESCRIPTION));
                            return "1";


                } catch (JSONException e)

                return "0";

            protected void onPostExecute(String s)
                if (s.equals("1"))

                    // Launch Dashboard Screen
                    Intent dashboard = new Intent(getApplicationContext(), Loginactivity.class);
                    // Close all views before launching Dashboard
                    // Close Registration Screen

                    // Error in registration
                    //registerErrorMsg.setText("Error occurred in registration");

    public void showToastforSucessfulBetCreation() {
        Toast toast = Toast.makeText(this, "Registration Sucessfull", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM, 0, 30);;


    public void showToastforUnsucessfulBetCreation() {
        Toast toast = Toast.makeText(this, "Registration UnSucessfull", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM, 0, 30);;





placeBet is not yet initialized that’s why it is giving null pointer exception

check the following line

User contributions licensed under: CC BY-SA
1 People found this is helpful