Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiation problems with id
    primarykey
    data
    text
    <p>I've followed a tutorial to realise a calculator. It seems that my buttons objects that i define, and instantiate with the ID are not instantiated because when i add a listener on them, they return a null pointer exception. here's the java code:</p> <pre><code>package com.example.calculette; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { //Variables declaration Button b0; Button b1; Button b2; Button b3; Button b4; Button b5; Button b6; Button b7; Button b8; Button b9; Button bC; Button bCE; Button bEgal; Button bPlus; Button bMoins; Button bDiviser; Button bMultiplier; EditText saisie; private double number1 = 0; private boolean clicOperateur = false; private boolean update = true; private String operateur = ""; //onCreate is the first procedure called by the activity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b0 = (Button) findViewById(R.id.button0); b1 = (Button) findViewById(R.id.button1); b2 = (Button) findViewById(R.id.button2); b3 = (Button) findViewById(R.id.button3); b4 = (Button) findViewById(R.id.button4); b5 = (Button) findViewById(R.id.button5); b6 = (Button) findViewById(R.id.button6); b7 = (Button) findViewById(R.id.button7); b8 = (Button) findViewById(R.id.button8); b9 = (Button) findViewById(R.id.button9); bC = (Button) findViewById(R.id.buttonC); bCE = (Button) findViewById(R.id.buttonCE); bEgal = (Button) findViewById(R.id.buttonEgal); bPlus = (Button) findViewById(R.id.buttonPlus); bMoins = (Button) findViewById(R.id.buttonMoins); bDiviser = (Button) findViewById(R.id.buttonDiviser); bMultiplier = (Button) findViewById(R.id.buttonMultiplier); saisie = (EditText) findViewById(R.id.EditText01); //Listeners declaration b0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub numberClick("0"); } }); </code></pre> <p>(i don't link the whole code because it's still the same for the 9 buttons and after it's simple methods).</p> <p>here you can see the XML associated:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="3"&gt; &lt;EditText android:id="@+id/EditText01" android:layout_width="fill_parent" android:layout_height="80px" android:textSize="20px" android:editable="false" android:cursorVisible="false" /&gt; &lt;View android:layout_height="3dip" android:background="#FF999999" /&gt; &lt;TableRow&gt; &lt;Button android:id="@+id/buttonCE" android:text="CE" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_span="2" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonC" android:text="C" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_span="2" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;Button android:id="@+id/button7" android:text="7" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/button8" android:text="8" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/button9" android:text="9" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonPlus" android:text="+" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;Button android:id="@+id/button4" android:text="4" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/button5" android:text="5" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/button6" android:text="6" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonMoins" android:text="-" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;Button android:id="@+id/button1" android:text="1" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/button2" android:text="2" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/button3" android:text="3" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonMultiplier" android:text="*" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;/TableRow&gt; &lt;TableRow&gt; &lt;Button android:id="@+id/button0" android:text="0" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonPoint" android:text="." android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonEgal" android:text="=" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;Button android:id="@+id/buttonDiviser" android:text="/" android:layout_width="fill_parent" android:layout_height="wrap_content" style="@style/button_text" android:background="@drawable/button" /&gt; &lt;/TableRow&gt; &lt;/TableLayout&gt; &lt;ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:src="@drawable/logosafran" android:id="@+id/imageView1" &gt;&lt;/ImageView&gt; &lt;/LinearLayout&gt; </code></pre> <p>The logcat says that i have a nullPointerException on the first listener declaration with b0.</p> <p>Thank you guys!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload