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(newBet_activity.java:80)
Code:
package com.techiequickie.bhr.boadraf; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; 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"; @SuppressWarnings("unused") private static String KEY_ERROR = "error"; @SuppressWarnings("unused") 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"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.newbet); // Importing all assets like buttons, text fields inputBetname = (EditText) findViewById(R.id.betName_et); inputBetbrief = (EditText) findViewById(R.id.betBriefDescription_et); inputBetdescription = (EditText) findViewById(R.id.betDescription_et); placeBet = (Button) findViewById(R.id.btn_newbet); //valueSeekbar = (SeekBar) findViewById(R.id.betValue_bar); /* valueSeekbar.setOnSeekBarChangeListener ( 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> { @Override protected String doInBackground(String... params) { UserFunctions userFunction = new UserFunctions(); JSONObject json = userFunction.createBet(params[0], params[1], params[2]); // check for login response try { 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 userFunction.logoutUser(getApplicationContext()); db.newBet(json_user.getString(KEY_BETNAME), json_user.getString(KEY_BETBRIEF), json.getString(KEY_BETDESCRIPTION)); return "1"; } } } catch (JSONException e) { e.printStackTrace(); } return "0"; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if (s.equals("1")) { // Launch Dashboard Screen Intent dashboard = new Intent(getApplicationContext(), Loginactivity.class); // Close all views before launching Dashboard dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(dashboard); // Close Registration Screen finish(); showToastforSucessfulBetCreation(); } else { // Error in registration //registerErrorMsg.setText("Error occurred in registration"); showToastforUnsucessfulBetCreation(); } } }*/ public void showToastforSucessfulBetCreation() { Toast toast = Toast.makeText(this, "Registration Sucessfull", Toast.LENGTH_LONG); toast.setGravity(Gravity.BOTTOM, 0, 30); toast.show(); } public void showToastforUnsucessfulBetCreation() { Toast toast = Toast.makeText(this, "Registration UnSucessfull", Toast.LENGTH_LONG); toast.setGravity(Gravity.BOTTOM, 0, 30); toast.show(); } }
Advertisement
Answer
placeBet is not yet initialized that’s why it is giving null pointer exception
check the following line
palceBet=findViewId(R.id.btn_newbet);