0

In my application, there are two AutoCompleteTextView and a Button to calculate a certain operation. I am setting the Button responsive to the two AutoCompleteTextView in the sense that, if both of the AutoCompleteTextView are filled, only then should the Button appear.

Doing this I am getting a Null Pointer Exception. The program is as follows:

boolean isLocation=false;
boolean isDestination=false;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location=(AutoCompleteTextView)findViewById(R.id.locale);
        location.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
        location.setOnItemClickListener(new OnItemClickListener()

    {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            // TODO Auto-generated method stub
            place1 = (Places) adapterView.getItemAtPosition(position);
            location.setText(place1.description);
            location.clearFocus();
            destination.requestFocus();
            Log.d("AutoCompleteTask", location.getText().toString());
                }
    });

    location.addTextChangedListener(new TextWatcher()
    {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            fillLocation=true;
        }

    });


    destination = (AutoCompleteTextView)findViewById(R.id.destination);
    destination.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.list_item));
    destination.setOnItemClickListener(new OnItemClickListener() 
    {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                // TODO Auto-generated method stub
            place2 = (Places) adapterView.getItemAtPosition(position);
            destination.setText(place2.description);
            destination.clearFocus();
            calculate.requestFocus();               
            Log.d("AutoCompleteTask2", destination.getText().toString());
                }
    });

    destination.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            fillDestination=true;
        }
    });

    if(fillLocation==true || fillDestination==true )
    {
        calculate.setVisibility(View.VISIBLE);
    }
    else
    {
        calculate.setVisibility(View.INVISIBLE);
    }


    calculate = (Button)findViewById(R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {



            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            if(location.getText().toString().length()<1 || destination.getText().toString().length()<1) 
            {
                Toast.makeText(getApplicationContext(), "Give values before placing a query", Toast.LENGTH_LONG).show();
            }
            else{
                @SuppressWarnings("deprecation")
                String url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins="+URLEncoder.encode(location.getText().toString())+"&destinations="+URLEncoder.encode(destination.getText().toString())+"&unit=metric&mode=driving&sensor=false";
                new ReadDistanceJSONFeedTask().execute(url);
                }
            }
        });

Being new to developing apps, what is this Null Pointer Exception? Can someone please help ?

fmendez
  • 7,250
  • 5
  • 36
  • 35
Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • maybe put calculate = (Button)findViewById(R.id.calculate); after setContentView(); you should put logCat in – JRowan Apr 10 '13 at 03:40

3 Answers3

0

A Null Pointer Exception occurs when something in your code returns null

for example if you have:

int count;
String[] biz = {"hand","is","hammer"}

and then you try to do this:

String isnt = biz[count];

if you didnt initialize the variable count like:

count = 1;

then

String isnt = biz[count]; 

would throw a null pointer exception because nothing was set to count

JRowan
  • 6,824
  • 8
  • 40
  • 59
0

You're getting a NPE because of this:-

if(fillLocation==true || fillDestination==true )
{
    calculate.setVisibility(View.VISIBLE); // NPE here
}
else
{
    calculate.setVisibility(View.INVISIBLE); // NPE here
}
calculate = (Button)findViewById(R.id.calculate);

You've tried to set the visibility of the calculate button, even before initializing it. Move calculate = (Button)findViewById(R.id.calculate); above the if-else block & it should work.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Its is throwing NullPointerException because your calculate = (Button)findViewById(R.id.calculate); not visible put it in youronCreate() method after the setContentView().

Abhijit Chakra
  • 3,201
  • 37
  • 66