質問

   public class CountryLookupTest {

                public static void main(String[] args) {
                String location1=null;
                try {
                int number=0;
                LookupService citylookup = newLookupService("D://GeoLiteCity.dat",
                            LookupService.GEOIP_MEMORY_CACHE );
                FileReader fr =new FileReader("d:\\IP.txt");
                BufferedReader br = new BufferedReader(fr);
                String line;
                while( (line = br.readLine()) != null ){
                    location1=line;
                    System.out.println(location1);
                Location record = citylookup.getLocation(location1);                        
                Site S[]= new Site[100];            
                S[number]= new Site(record.longitude,record.latitude);
                number++;
                            System.out.println("longtitude " + S[0].getLongtitude());  
                System.out.println("latitude " + S[0].getLatitude());
                            }
    public class Site { 
        private float longitude;
        private float latitude;

            public Site(float a, float b){
            setLongitude(a);
            setLatitude(b);
            }
    }

メインクラスを使用して、IPアドレスをラインごとに保存し、それらをオブジェクトに保存して配列に保存するTXTを読み取ります。
コードをテストし、実行時間エラーが発生しました。 140.118.175.208 Longtitude 121.524994 Latitude 25.0392例外「Main」Java.lang.nullpointerexceptionおよびI add s [1] System.out.println( "longtitude" + s [1] .getlongtitude();それは私に同じ問題を示し、何が起こったのかわかりません。配列objを割り当てたと思いますか?ありがとう!

役に立ちましたか?

解決

問題はラインにあります:

Site S[]= new Site[100];  

反復ごとに新しい配列を作成するため、最後にヌルポインターでのみ満たされます。 S [0]にアクセスしようとすると、2回目の反復でヌルポインターが得られます。

それが最初に印刷する理由ですが、2回目はヌルポインターを取得します。 S [0]の初めての値は、2回目ではありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top