문제

   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 위도 25.0392 스레드 "Main"java.lang.nullpointerexception 및 i 추가 [1] system.out.println ( "longtitude" + s [1] .getLongtitude ()); 그것은 나에게 같은 문제를 보여주고 S [1] 값을 인쇄하지 않습니다. 나는 무슨 일이 일어 났는지 모르겠습니까? 배열 OBJ를 할당 한 것 같아요? 감사합니다!

도움이 되었습니까?

해결책

문제는 다음과 같습니다.

Site S[]= new Site[100];  

각 반복마다 새 배열을 만들므로 끝에는 널 포인터 만 채워집니다. S [0]에 액세스하려고 할 때는 두 번째 반복에 널 포인터를 줄 것입니다.

그렇기 때문에 첫 번째 인쇄물이지만 두 번째로 널 포인터를 얻습니다. S [0]에게 값이 처음이되면 두 번째로는 그렇지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top