Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7783913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:58:55+00:00 2026-06-01T19:58:55+00:00

I’m trying to do a search from my database here is the code it

  • 0

I’m trying to do a search from my database here is the code it keeps bringing the error below.The data is on a MySQL problem is it keeps crashing can somebody help me please.Here is my code.

public class Main extends MapActivity {
    /** Called when the activity is first created. */
    private Context ctx = this;
    private Context ctx2 = this;
    private ProgressDialog pd, pd2 = null;
    private Object data, data2 = null;private String url;private MapView myMap;
    private Connector http;

    static String searchy;
    String selection;
    // MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    Drawable drawable2;
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myMap = (MapView) findViewById(R.id.mapview);
        myMap.setBuiltInZoomControls(true);
        // addmarkers();
        // EditText searchInput = (EditText) findViewById(R.id.txtSearch);
        // final String searchitem = searchInput.getText().toString().trim();

        Button search = (Button) findViewById(R.id.searchBtn);
        search.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Log.i("MyApp", "Background thread starting");

                // pd = ProgressDialog.show(Main.this, "Working..",
                // "Downloading Data...", true, false);

                addmarkers();
            }
        });

    }

//adds the searched markers to the map

 public void addmarkers() {
        url = "http://10.0.2.2/therm/searchy.php";
        http = new Connector(ctx);
        http.connect(url, myMap);  }

//class connector that adds overlays and fetches from the database

       public class Connector {
            private Drawable marker;
        private GeoPoint point;
        private List<Overlay> mapOverlays;

        public Connector(Context ctx1) {
            ctx = ctx1;
            marker = ctx.getResources().getDrawable(R.drawable.default_marker);
            itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(
                    marker, myMap);
        }

        public void connect(String url, MapView myMap) {
            // TODO Auto-generated method stub
            InputStream is = null;
            // mapOverlays = myMap.getOverlays();
            // mapOverlays.clear();
            String result = null;
            int ct_id;
            String ct_name = null;
            // String[] ct_namey = null;
            String ct_add = null;
            String ct_lat;
            String ct_long;
            String imageURL;
            // the year data to send
            EditText txt_username = (EditText) findViewById(R.id.txtSearch);
            String woi = txt_username.getText().toString();
            Toast.makeText(getBaseContext(), woi, Toast.LENGTH_SHORT).show();

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("searchy", woi));
            // http post
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
            }

            // convert response to string
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                    Log.v("log_tag", "Line reads: " + line);

                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
            // parse json data
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    Log.i("log_tag","address: " + json_data.getString("address")

                    );
                    if (searchy.equals(json_data.getString("address"))) {
                        ct_id = json_data.getInt("id");
                        ct_name = json_data.getString("name");
                        // ct_namey[i] = json_data.getString("name");
                        ct_add = json_data.getString("address");
                        ct_lat = json_data.getString("lat");
                        ct_long = json_data.getString("lng");
                        imageURL = json_data.getString("imageUrl");

                        Double lon = (Double.parseDouble(ct_long));
                        Double lat = (Double.parseDouble(ct_lat));

                        point = new GeoPoint((int) (lat * 1E6),
                                (int) (lon * 1E6));

                        CustomOverlayItem overlayItem = new CustomOverlayItem(
                                point, ct_name, ct_add, imageURL);
                        itemizedOverlay.addOverlay(overlayItem);
                        mapOverlays.add(itemizedOverlay);
                        myMap.postInvalidate();

                    } else {
                        Toast.makeText(getBaseContext(),
                                "No such address is found", Toast.LENGTH_LONG)
                                .show();
                    }
                }

            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
            return;
        }

    }



    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

// the .php file

    <?php
mysql_connect("localhost","root","");
mysql_select_db("hospitals");
$q=mysql_query("SELECT * FROM hosi WHERE address like '%".$_REQUEST['searchy']."%'");
    while($e=mysql_fetch_assoc($q))
        $output[]=$e;
print(json_encode($output));
mysql_close();?>

Here’s the error I’m getting:

//error04-06 16:04:07.162: V/log_tag(10708): Line reads: [{"id":"1","name":"The Nairobi Hospital","address":"Upperhill","consult":"2000","phone":"+254202846000","desc":"The.... "}]
04-06 16:04:07.162: V/log_tag(10708): Line reads: 
04-06 16:04:07.502: I/log_tag(10708): address: Upperhill
04-06 16:04:07.552: W/dalvikvm(10708): threadid=1: thread exiting with uncaught exception (group=0x400287f0)
04-06 16:04:07.572: E/AndroidRuntime(10708): FATAL EXCEPTION: main
04-06 16:04:07.572: E/AndroidRuntime(10708): java.lang.NullPointerException
04-06 16:04:07.572: E/AndroidRuntime(10708): at   com.realestatefinder.plotjson.Main$Connector.connect(Main.java:276)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at com.realestatefinder.plotjson.Main$1.onClick(Main.java:86)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at android.view.View.performClick(View.java:2408)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at android.view.View$PerformClick.run(View.java:8816)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at android.os.Handler.handleCallback(Handler.java:587)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at android.os.Looper.loop(Looper.java:123)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at android.app.ActivityThread.main(ActivityThread.java:4627)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at java.lang.reflect.Method.invokeNative(Native Method)
04-06 16:04:07.572: E/AndroidRuntime(10708):    at java.lang.reflect.Method.invoke(Method.java:521)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:58:56+00:00Added an answer on June 1, 2026 at 7:58 pm

    Looks like you are getting a NPE because you’ve failed to initialize your mapOverlays collection. You commented out the initialization:

     // mapOverlays = myMap.getOverlays();
     // mapOverlays.clear();
    

    Your error shows:

    java.lang.NullPointerException
    at   com.realestatefinder.plotjson.Main$Connector.connect(Main.java:276)
    

    and you said that line 276 is:

       mapOverlays.add(itemizedOverlay);
    

    I think you learn how to use the Android debugger so you can trace this sort of code in the future. Best of luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.