-2

I know that there are loads of questions about NullPointerException. I've been looking around trying to find an answer but I haven't found the right one. It crashes when I try to open energy.xml and it's the java that's the problem. Anyone know why? From the Logcat I can see that the problem is in OnCreate - btnNrj.setOnClickListener(this); I've tried to Clean and rebuild. I can only assume that I will have more problems afterwards but for now the most important thing is to make it not crash.

public class ActivityEnergy extends Activity implements OnClickListener {
    EditText etNum10;
    EditText etNum20;
    EditText etNum30;
    int nrjspinner = 0;
    Object value;

    Button btnNrj;

    String oper = "";

    TextView tvResult;

 ArrayList<String> nrjmethod = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.energy);

     // find the elements
    etNum10 = (EditText) findViewById(R.id.etNum10);
    etNum20 = (EditText) findViewById(R.id.etNum20);
    etNum30 = (EditText) findViewById(R.id.etNum30);

        btnNrj = (Button) findViewById(R.id.btnNrj);

        tvResult = (TextView) findViewById(R.id.tvResult);

        // set a listener
        btnNrj.setOnClickListener(this);

  Spinner spinner = (Spinner) findViewById(R.id.spinner); 

  // Create the ArrayAdapter
  ArrayAdapter <CharSequence> adapter = ArrayAdapter.createFromResource( this,
          R.array.nrjmethod, android.R.layout.simple_spinner_item);

                 // Set the Adapter
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);


  // Set the ClickListener for Spinner
  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

      @Override
      public void onItemSelected(AdapterView<?> parent, View view,
              int position, long id) {

          value = parent.getItemAtPosition(position);
          switch (position) {
          case 0:
              nrjspinner = 1;
              break;

          case 1:
              nrjspinner = 0.8;
              break;

          case 2:
              nrjspinner = 0.8;
              break;

          case 3:
              nrjspinner = 0.8;
              break;

          case 4:
              nrjspinner = 0.8;
              break;

          case 5:
              nrjspinner = 0.8;
              break;

          case 6:
              nrjspinner = 0.8;
              break;

          case 7:
              nrjspinner = 0.8;
              break;

          case 8:
              nrjspinner = 0.8;
              break;

          case 9:
              nrjspinner = 0.8;
              break;

          case 10:
              nrjspinner = 0.6;
              break;

          case 11:
              nrjspinner = 0.6;
              break;
          }

      }


      @Override
      public void onNothingSelected(AdapterView<?> arg0) {



    } 

        });


 }

    @Override
         public void onClick(View v) {
                // TODO Auto-generated method stub
                float num10 = 0;
                float num20 = 0;
                float num30 = 0;
                float result = 0;

                // check if the fields are empty
                if (TextUtils.isEmpty(etNum10.getText().toString())
                    || TextUtils.isEmpty(etNum20.getText().toString())
                    || TextUtils.isEmpty(etNum30.getText().toString())) {
                  return;
                }

                // read EditText and fill variables with numbers
                num10 = Float.parseFloat(etNum10.getText().toString());
                num20 = Float.parseFloat(etNum20.getText().toString());
                num30 = Float.parseFloat(etNum30.getText().toString());

             // defines the button that has been clicked and performs the corresponding operation
               switch (v.getId()) {
                case R.id.btnNrj:
                  oper = "";
                  result = (float) ((nrjspinner * ((num10 * num20) / num30))*0.001);
                  break;
                default:
                  break;

                }

             // form the output line
                tvResult.setText("Your result = " + result);

    }


}

And here's the logcat

10-27 17:40:58.832: E/AndroidRuntime(1497): FATAL EXCEPTION: main

10-27 17:40:58.832: E/AndroidRuntime(1497): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.develope.axson/se.develope.axson.ActivityEnergy}: java.lang.NullPointerException

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.ActivityThread.access$600(ActivityThread.java:141)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.os.Handler.dispatchMessage(Handler.java:99)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.os.Looper.loop(Looper.java:137)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.ActivityThread.main(ActivityThread.java:5103)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at java.lang.reflect.Method.invokeNative(Native Method)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at java.lang.reflect.Method.invoke(Method.java:525)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at dalvik.system.NativeStart.main(Native Method)

10-27 17:40:58.832: E/AndroidRuntime(1497): Caused by: java.lang.NullPointerException

10-27 17:40:58.832: E/AndroidRuntime(1497):     at se.develope.axson.ActivityEnergy.onCreate(ActivityEnergy.java:52)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.Activity.performCreate(Activity.java:5133)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

10-27 17:40:58.832: E/AndroidRuntime(1497):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

10-27 17:40:58.832: E/AndroidRuntime(1497):     ... 11 more
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
Peter
  • 1
  • 1
  • did you try to delete the autogenerated R.java file? If not try to delete it, close the eclipse or your IDE, open it again and clean the project – nio Oct 28 '13 at 10:25
  • 1
    why are u using object value .Bcoz u only just assigned it but not used any where – Shakeeb Ayaz Oct 28 '13 at 10:27
  • Line 52 is the line btnNrj.setOnClickListener(this) that I mentioned in my question. I've deleted the R.java. Still no go. Honestly I don't konw Tobias_k. I'm kind of a newbie at this. Can you explain about the object value, Shaheen? – Peter Oct 29 '13 at 20:01
  • @ShakeebShaheen I thought I assigned it with the following line: value = parent.getItemAtPosition(position); – Peter Oct 29 '13 at 20:13
  • It's not crashing anymore (see my answer). So far so good but now I can't get the formula to work. I can only assume it has something to do with the spinner and the value. – Peter Oct 29 '13 at 22:10

1 Answers1

0

I gave up finding a solution for the NullPointerException. Instead i found another way to fix the problem without listener. It was @Chronos who came with the solution in another thread.

In the XML where the button is defined ad

android:onClick="ButtonOnClick"

and in the code you define the "ButtonOnClick". In my case it looks like this:

public void ButtonOnClick(View v){
// TODO Auto-generated method stub
        float num10 = 0;
        float num20 = 0;
        float num30 = 0;
        float result = 0;

        // check if the fields are empty
        if (TextUtils.isEmpty(etNum10.getText().toString())
            || TextUtils.isEmpty(etNum20.getText().toString())
            || TextUtils.isEmpty(etNum30.getText().toString())) {
          return;
        }

        // read EditText and fill variables with numbers
        num10 = Float.parseFloat(etNum10.getText().toString());
        num20 = Float.parseFloat(etNum20.getText().toString());
        num30 = Float.parseFloat(etNum30.getText().toString());

     // defines the button that has been clicked and performs the corresponding operation
        switch (v.getId()) {
        case R.id.btnNrj:
          oper = "";
          result = (float) ((nrjspinner * ((num10 * num20) / num30))*0.001);
          break;
        default:
          break;

You can find Chronos answer in this thread: Android OnClickListener - identify a button

Community
  • 1
  • 1
Peter
  • 1
  • 1