By munishguptamg


2014-05-17 06:05:45 8 Comments

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bugs3.munishgupta.cabup/com.bugs3.munishgupta.cabup.WannaHier}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) at android.app.ActivityThread.access$600(ActivityThread.java:123) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.bugs3.munishgupta.cabup.WannaHier.onCreate(WannaHier.java:44) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) ... 11 more

  • Wanna Hier.java

            package com.bugs3.munishgupta.cabup;
        import java.util.concurrent.ExecutionException;
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;
        import android.app.Activity;
        import android.content.Intent;
        import android.location.Location;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.Toast;
        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
        import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
        import com.google.android.gms.maps.MapFragment;
        import com.google.android.gms.maps.model.LatLng;
        import com.google.android.gms.maps.model.Marker;
        import com.google.android.gms.maps.model.MarkerOptions;
    
        public class WannaHier extends Activity {
            GoogleMap map;
            Intent info;
            String mob,lat,lon,title=null;
            Bundle b;
            Button btVCab;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.wanna_hier);
                info = getIntent();
                b  = info.getExtras();
                mob = b.getString("mob");
                btVCab=(Button) findViewById(R.id.btVCab);
                map=((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                map.setMyLocationEnabled(true);
                lat=Double.toString(map.getMyLocation().getLatitude());
                lon=Double.toString(map.getMyLocation().getLongitude());
                map.setOnMarkerClickListener(new OnMarkerClickListener() {
    
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        // TODO Auto-generated method stub
                    try
                    {
                        title=marker.getTitle().toString();
                        request(title,lat,lon,mob);
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(getApplicationContext(), "Please turn on your location", Toast.LENGTH_LONG).show();
                    }
                        return false;
                    }
                });
                map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
    
                    @Override
                    public void onMyLocationChange(Location location) {
                        // TODO Auto-generated method stub
                        JSONGetRequest jRequest = JSONGetRequest
                                .getSingleInstance(WannaHier.this);
                        try {
    
                            String url = (Tags.url + "op=update&query=update requestpick set carNo='" +title+
                                  "',latitude='" + lat + "', longitude='" + lon +"'where cust_mob='"+ mob +"'").replaceAll(
                                            " ", "%20");
    
                            JSONObject json = jRequest.execute(url).get();
    
                            if (json.getInt("success")==1) {
                                Log.d("Requested Successfully!", json.toString());
                            } else {
                                Log.d("Request failed!", json.getString("message"));
                            }
    
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
                btVCab.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        JSONGetRequest jRequest = JSONGetRequest
                                .getSingleInstance(WannaHier.this);
                        try {
    
                            String url = (Tags.url + "op=select&query=select * from driverloc")
                                    .replaceAll(" ", "%20");
    
                            JSONObject json = jRequest.execute(url).get();
    
                            if (json.getInt("success")==1) {
                                Log.d("Requested Successfully!", json.toString());
                                JSONArray js=json.getJSONArray("entities");
                                try{
                                    for(int co=js.length(), i=0; i<co;i++){
                                        json=js.getJSONObject(i);
                                        title=json.getString("carNo");
                                        lat=json.getString("latitude");
                                        lon=json.getString("longitude");
                                        map.addMarker(new MarkerOptions()
                                        .position(new LatLng(Double.parseDouble(lat),Double.parseDouble(lon)))
                                        .title(title));
                                    }
                                }
                                catch(JSONException e) {
                                    e.printStackTrace();
                                }           
                            } else {
                                Log.d("Request failed!", json.getString("message"));
                            }
    
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
    
                    }
                });
            }
            @Override
            protected void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
                Toast.makeText(getApplicationContext(), "Please Turn on your location and click on view cabs", Toast.LENGTH_LONG).show();
            }
    
            public void request(String title, String lat, String lon, String mob )
            {
                JSONGetRequest jRequest = JSONGetRequest
                        .getSingleInstance(WannaHier.this);
                try {
    
                    String url = (Tags.url + "op=insert&query=insert into requestpick values ('"
                            +title+ "','" + lat + "','" + lon +"','"+ mob +"')").replaceAll(
                                    " ", "%20");
    
                    JSONObject json = jRequest.execute(url).get();
    
                    if (json.getInt("success")==1) {
                        Log.d("Requested Successfully!", json.toString());
                    } else {
                        Log.d("Request failed!", json.getString("message"));
                    }
    
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            @Override
            protected void onPause() {
                // TODO Auto-generated method stub
                super.onPause();
                JSONGetRequest jRequest = JSONGetRequest
                        .getSingleInstance(WannaHier.this);
                try {
    
                    String url = (Tags.url + "op=delete&query=delete from requestpick where cust_mob='"
                    + mob +"'").replaceAll(" ", "%20");
    
                    JSONObject json = jRequest.execute(url).get();
    
                    if (json.getInt("success")==1) {
                        Log.d("Requested Successfully!", json.toString());
                    } else {
                        Log.d("Request failed!", json.getString("message"));
                    }
    
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                finish();
            }
            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.wanna_hier, menu);
                return true;
            }
    
        }
    
  • Profile.java

    package com.bugs3.munishgupta.cabup;
    import com.bugs3.munishgupta.cabup.R;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class Profile extends Activity {
    
    Button obhier, obshare, obsettings;
    String mob,carno,table;
    Intent info;
    Bundle b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
        obhier=(Button) findViewById(R.id.btnHier);
        obshare=(Button) findViewById(R.id.btnShare);
        obsettings=(Button) findViewById(R.id.btnSetting);
        info = getIntent();
        b  = info.getExtras();
        table=b.getString("table");
    
        if((carno=b.getString("carno"))!=null )
        {
            Toast.makeText(getApplicationContext(), carno, Toast.LENGTH_SHORT).show();
            obhier.setVisibility(View.INVISIBLE);       
        }
        else
        {
            obshare.setVisibility(View.INVISIBLE);
        }
    
        obshare.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent wshare=new Intent(Profile.this,WannaShare.class);
                    mob = b.getString("mob");
                    wshare.putExtra("mob", mob);
                    wshare.putExtra("carno", carno);
                    wshare.putExtra("table", table);
                    startActivity(wshare);
            }
        });
        obhier.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent whier=new Intent(Profile.this,WannaHier.class);
                    mob = b.getString("mob");
                    whier.putExtra("mob", mob);
                    whier.putExtra("table", table);
                    startActivity(whier);
            }
        });
        obsettings.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent isetting = new Intent(Profile.this,ChangeSetting.class);
                if(b.getString("mob")!=null)
                {
                    mob = b.get("mob").toString();
                    isetting.putExtra("mob", mob);
                    isetting.putExtra("table", table);
                }
                startActivity(isetting);
            }      
        });       
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.profile, menu);
        return true;
    }
    }
    

1 comments

@iTech 2014-05-17 06:12:48

The answer is at WannaHier: line 44 which I suspect it to be

lat=Double.toString(map.getMyLocation().getLatitude());

Because getMyLocation() can return null if there is no location available so you have to check for this.

For example:

Location location = map.getMyLocation();
if(location != null){
    lat=Double.toString(location.getLatitude());
    lon=Double.toString(location.getLongitude()); 
}

Related Questions

Sponsored Content

31 Answered Questions

[SOLVED] How to get the build/version number of your Android application?

40 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

25 Answered Questions

[SOLVED] Is there a way to get the source code from an APK file?

17 Answered Questions

[SOLVED] "Debug certificate expired" error in Eclipse Android plugins

2 Answered Questions

[SOLVED] Android - Vpnservice DatagramChannel.open() not working

1 Answered Questions

[SOLVED] Loadmanager onLoadFinished not called

  • 2013-05-05 21:54:08
  • kevingoos
  • 929 View
  • 5 Score
  • 1 Answer
  • Tags:   android sqlite

1 Answered Questions

Trying to run my first Andengine app on device

  • 2014-07-16 20:03:27
  • user3846448
  • 436 View
  • 0 Score
  • 1 Answer
  • Tags:   android andengine

2 Answered Questions

Sponsored Content