简体   繁体   English

错误给出java.lang Null PointerException

[英]Error gives java.lang Null PointerException

Hey While I am running the application it gives a error jDocument doc = md.getDocument(fromPosition, toPosition, GMapDirection.MODE_DRIVING), So please anyone help me to fix this problem. 嘿,当我运行应用程序时,它给出了一个错误jDocument doc = md.getDocument(fromPosition,toPosition,GMapDirection.MODE_DRIVING),所以请任何人帮助我解决此问题。

ManinActivity code is below : ManinActivity代码如下:

public class MainActivity extends Activity {
GoogleMap map;
GMapDirection md;
LatLng fromPosition = new LatLng(13.687140112679154, 100.53525868803263);
LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

     LatLng coordinates = new LatLng(13.685400079263206, 100.537133384495975);      
    map.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16));  
    map.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
    map.addMarker(new MarkerOptions().position(toPosition).title("End"));

    Document doc = md.getDocument(fromPosition, toPosition, GMapDirection.MODE_DRIVING);
    int duration = md.getDurationValue(doc);
    String distance = md.getDistanceText(doc);
    String start_address = md.getStartAddress(doc);
    String copy_right = md.getCopyRights(doc);

    ArrayList<LatLng> directionPoint = md.getDirection(doc);
    PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

    for(int i = 0 ; i < directionPoint.size() ; i++) {          
        rectLine.add(directionPoint.get(i));
    }
    map.addPolyline(rectLine);   
}       

} }

// GMapDirection class code is below : // GMapDirection类代码如下:

 public class GMapDirection {
    public final static String MODE_DRIVING = "driving";
    public final static String MODE_WALKING = "walking";
    public GMapDirection() { }  
    public Document getDocument(LatLng start, LatLng end, String mode){
        String url = "http://maps.googleapis.com/maps/api/directions/xml?" 
                + "origin=" + start.latitude + "," + start.longitude  
                + "&destination=" + end.latitude + "," + end.longitude 
                + "&sensor=false&units=metric&mode=driving";
        Log.d("GoogleMapsDirection", url);
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response = httpClient.execute(httpPost, localContext);
            InputStream in = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(in);
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I think that you are saying that this line: 我认为您说的是这一行:

    Document doc = md.getDocument(fromPosition, toPosition,
                                  GMapDirection.MODE_DRIVING);

is giving you a NullPointerException. 给你一个NullPointerException。 That means that md is null when you execute the above. 这意味着当您执行上述操作时, mdnull (There is no other possible way that that line of code could throw that exception!) (没有其他方法可以使该行代码引发该异常!)

I can see a declaration for md : 我可以看到md的声明:

    GMapDirection md;

but I can't see where you initialize it. 但我看不到在哪里初始化它。 If you don't initialize md anywhere, then that is why it is null ... and that is the cause of your exception. 如果您未在任何地方初始化md ,那么这就是为什么它为null的原因,这就是导致异常的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM