i am getting null pointer exception from String placeName = placeText.getText().toString();

筅森魡賤 提交于 2019-12-02 02:36:24

问题


Hi want to get the place name from the edit text and mark on map here is my code where i got null pointer exception please help me what i should do and where i am going wrong.

as I am getting the place name from the edit text field in dialog box.

View layout = View.inflate(this, R.layout.alertbox, null);

            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Enter the places");
            dialog.setView(layout);

            dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {



                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // TODO Auto-generated method stub


                     EditText placeText = (EditText) findViewById(R.id.strtplace);          
                             String placeName = placeText.getText().toString();
//                              
  Break from execution if the user has not entered anything in the field
                        if(placeName.compareTo("")==0) 
                        numberOptions = 5;
                        String [] optionArray = new String[numberOptions];
                        Geocoder gcoder = new Geocoder(TravellogActivity.this);  

                        try{
                            List<Address> results = gcoder.getFromLocationName(placeName,numberOptions);
                            Iterator<Address> locations = results.iterator();
                            String raw = "\nRaw String:\n";
                            String country;
                            int opCount = 0;
                            while(locations.hasNext()){
                                Address location = locations.next();
                                lat = location.getLatitude();
                                lon = location.getLongitude();
                                country = location.getCountryName();
                                if(country == null) {
                                    country = "";
                                } else {
                                    country =  ", "+country;
                                }
                                raw += location+"\n";
                                optionArray[opCount] = location.getAddressLine(0)+", "+location.getAddressLine(1)+country+"\n";
                                opCount ++;
                            }
                            Log.i("Location-List", raw);
                            Log.i("Location-List","\nOptions:\n");
                            for(int i=0; i<opCount; i++){
                                Log.i("Location-List","("+(i+1)+") "+optionArray[i]);
                            }

                        } catch (IOException e){
                            Log.e("Geocoder", "I/O Failure; is network available?",e);
                        }           

                        p = new GeoPoint(latE6, lonE6);
                        myMC.animateTo(p); 


                    }                       
                });

            dialog.show();
            break;

            }



        return false  ;

    }

回答1:


use EditText placeText = (EditText) layout.findViewById(R.id.strtplace); intead of EditText placeText = (EditText) findViewById(R.id.strtplace);




回答2:


NullPointerExceptions are the most easy exceptions to find and debug.

If you're getting a NullPointerException on this line :

String placeName = placeText.getText().toString();

The only thing that can be null is the placeText variable, or the placeText.getText()

You need to figure out why it is null. As your placeText is on the alert, so you should fetch it from the layout using layout.findViewById. As your fetching it from the wrong place, it will be null, causing the NullPointerException.

in theory, getText() could also return null, but the current implementation in the Android SDK will return an empty string when nothing is filled in, so that can't be null.




回答3:


heres the changes that you need to do: //both the view and the editext should be declared as final and the findViewById method needs a layout on which it is to be called

  • final View layout = View.inflate(this, R.layout.alertbox, null);
    • final EditText placeText = (EditText) layout.findViewById(R.id.strtplace);


来源:https://stackoverflow.com/questions/6420999/i-am-getting-null-pointer-exception-from-string-placename-placetext-gettext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!