-1

我正在尝试开发一个应用程序..以下是一个片段

class metro_nodes {
public String station;
public GeoPoint point; }

public class mainscreen extends MapActivity {
/** Called when the activity is first created. */

MapController controller;
double latitude,longitude;
LocationManager loc;
Location lastknownloc;
LocationListener loclistener;
List<GeoPoint> geopoints = new ArrayList<GeoPoint>();
MapView mapView;
private LinkedList<metro_nodes> station_location = new LinkedList<metro_nodes>();
metro_nodes anand_nagar;
anand_nagar.station = "anand_nagar";
    }

现在在倒数第二行给出-“令牌“站”上的语法错误,此令牌后预期的VariableDeclaratorId”

如果我在这个语句周围加上花括号,那么错误就会被删除..但是我得到 java lang 空指针异常......问题是什么?

4

3 回答 3

1

您在那里有类字段声明,倒数第二行是不属于变量声明部分的语句 - 必须作为方法/构造函数的一部分来完成。

通过将它括在花括号中,您实际上是在创建一个类初始化块,但是在将任何实例分配给它之前尝试访问 anand_nagar 变量会导致 NPE。Austin 是对的 - 似乎您缺少一些基本的 Java 编程知识,所以也许您应该先从一些 Java 教程开始:http ://docs.oracle.com/javase/tutorial/

于 2012-06-16T20:26:11.973 回答
0

空指针到期是因为您没有使用 new 为变量 anand_nagar 创建 metro_nodes 实例

metro_nodes anand_nagar;
anand_nagar = new metro_nodes();//<----------------need this line to avoid NPE 
anand_nagar.station = "anand_nagar";

并且还请遵循标准,例如类名应该像 java 中的 MetroNode .....

public class MainScreen extends MapActivity {


    private MapController mController;
    private double mLatitude,mLongitude;
    private LocationManager mLoc;
    private Location mLastknownloc;
    private LocationListener mLoclistener;

    private MapView mMapView;
    private LinkedList<MetroNode> mStationLocations;
    MetroNode mAnandNagar;


    //in general we use cunstructor for initliaztion but in android we usesonCreate
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ..other code ..............

        List<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
        mStationLocations = new LinkedList<MetroNode>();
        mAnandNagar = new MetroNode();
        mAnandNagar.setStation("anand_nagar");
    }
}

class MetroNode {
    private  String station;
    private  GeoPoint point;

    public MetroNode(){
        super();    
    }

    public MetroNode(String station, GeoPoint point) {
        super();
        this.station = station;
        this.point = point;
    }

    public String getStation() {
        return station;
    }
    public void setStation(String station) {
        this.station = station;
    }
    public GeoPoint getPoint() {
        return point;
    }
    public void setPoint(GeoPoint point) {
        this.point = point;
    }




}
于 2012-06-16T20:23:19.663 回答
0

你还没有发布给你语法错误的代码......但我猜你得到了空指针异常,因为你从未初始化任何“metro_nodes”对象。

例如:

// This is a helper class
class MetroNode {
  public String station;
  public GeoPoint point; 

  public MetroNode (String station, GeoPoint point) {
    this.station = station;
    this.point = point;
  }
}

// This is the module's main class
public class MainScreen extends MapActivity {

  // This is public and private data for each class instance
  private LinkedList<MetroNode> station_location = null;

  // This is the activity's initialization
  public void onCreate(Bundle savedInstanceState) 
    station_locations = new LinkedList<MetroNode>();
    MetroNode metroNode = new MetroNode ("anand_nagar", new GeoPoint (1.00, 2.00));
    station_locations.add (metroNode);
  ...
于 2012-06-16T20:27:23.577 回答