问题描述
我一直不能运行,我不懂应该怎样改,我很苦恼啊只一直弹出Exceptioninthread"main"java.lang.NullPointerExceptionatLine.<init>(TestLine.java:29)atTestLine.main(TestLine.java:230)谁可以帮帮忙,麻烦各位了.../***@(#)Test.java***@author*@version1.002011/3/30*/classLine{protectedString[]str,str2;privateStringname;privateStation[]stops;privateint[]distance;privatedoublepassengerFarePerKm,cargoFarePerKm;privateintcrossBorderSurcharge,reservationSurcharge;publicLine(StringlineName,Stringstations,Stringfares){name=lineName;str=fares.split(":");passengerFarePerKm=Double.parseDouble(str[0]);cargoFarePerKm=Double.parseDouble(str[1]);crossBorderSurcharge=Integer.parseInt(str[2]);reservationSurcharge=Integer.parseInt(str[3]);str=stations.split("");stops=newStation[str.length];for(inti=0;i<str.length;i++){str2=str[i].split(":");stops[i]=newStation(str2[0],str2[1]);distance[i]=Integer.parseInt(str2[2]);}}publicStringgetName(){returnname;}publicStation[]getStops(){returnstops;}publicdoublegetPassengerFarePerKm(){returnpassengerFarePerKm;}publicdoublegetCargoFarePerKm(){returncargoFarePerKm;}publicintgetCrossBorderSurcharge(){returncrossBorderSurcharge;}publicintgetReservationSurcharge(){returnreservationSurcharge;}publicStringtoString(){Stringoutput="";for(inti=0;i<stops.length;i++){output+=stops[i].getName()+stops[i].getCountry()+"("+distance[i]+")";}output+="nnFaresSummary:nPassengerPerKM:EUR"+passengerFarePerKm+"nCargoPerKGPerKM:EUR"+cargoFarePerKm+"nSurcharge:CrossBorder:EUR"+crossBorderSurcharge+"nSurcharge:SeatReservation:EUR"+reservationSurcharge+"nn";returnoutput;}publicintcalculateDistance(Stationstart,Stationend){intdis=0;/*for(inti=0;i<stops.length;i++){if(start==stops[i]){dis=distance[i];}}for(inti=0;i<stops.length;i++){if(start==stops[i]){dis-=distance[i];}}*/returnMath.abs(dis);}}classStation{privateStringname,country;publicStation(Stringname,Stringcountry){this.name=name;this.country=country;}publicStringgetName(){returnname;}publicStringgetCountry(){returncountry;}publicStringtoString(){returnname+","+country;}}/*abstractclassTrain{privateStringcode,startTime;privateLineline;privateStationstart,end;publicTrain(Stringcode,StringstartTime,Lineline,Stationstart,Stationend){this.code=code;this.startTime=startTime;}publicStringgetCode(){returncode;}publicStringgetStartTime(){returnstartTime;}publicLinegetLine(){returnline;}publicStationgetStart(){returnstart;}publicStationgetEnd(){returnend;}publicStation[]getIntermediateStops(){return}publicintgetCrossBorderCount(Stationfrom,Stationto){}publicabstractdoublecalculateDistanceFare(Stationfrom,Stationto);publicbooleanisValidRoute(Stationfrom,Stationto){return}publicdoublecalculateFare(Stationfrom,Stationto,Stationquantity){//TotalFare=DistanceFare*quantity+CrossBorderSurcharge}publicabstractStringtoString();}classCargoTrainextendsTrain{privateintcargoCapacity;publicCargoTrain(Stringcode,StringstartTime,Lineline,Stationstart,Stationend,intcargoCapacity){super(code,startTime,line,start,end);this.cargoCapacity=cargoCapacity;}publicdoublecalculateDistanceFare(Stationfrom,Stationto){//distancefare=unitfare(fareper1KGper1KM)*distanceofthejourney.return*0.01;}publicintgetCargoCapacity(){returncargoCapacity;}publicStringtoString(){return}}classPassengerTrainextendsTrain{privateintpassengerCapacity;publicPassengerTrain(Stringcode,StringstartTime,Lineline,Stationstart,Stationend,intpassengerCapacity){super(code,startTime,line,start,end);this.passengerCapacity=passengerCapacity;}publicdoublecalculateDistanceFare(Stationfrom,Stationto){//distancefare=unitfare(fareperpassengerper1KM)*distanceofthejourney.return*0.06;}publicintgetPassengerCapacity(){returnpassengerCapacity;}publicStringtoString(){return}}classExpressPassengerTrainextendsPassengerTrain{publicExpressPassengerTrain(Stringcode,StringstartTime,Lineline,Stationstart,Stationend,intpassengerCapacity){super(code,startTime,line,start,end,passengerCapacity);}publicdoublecalculateDistanceFare(Stationfrom,Stationto){//totalfare=NormalPassengerTrainFare+ReservationSurchargereturnsuper.calculateDistanceFare(from,to)+10;}publicStringtoString(){return"SeatReservationReq’d";}}*/publicclassTestLine{publicstaticvoidmain(String[]args){//LineinformationLineline=newLine("Paris-Zurich-Munich","Paris:France:0Lyon:France:450Geneve:Switzerland:600Zurich:Switzerland:870St.Gallen:Switzerland:950Memmingen:Germany:1070Munich:Germany:1180","0.06:0.01:30:10");System.out.println(line);Stationstations[]=line.getStops();System.out.println(stations[0]);System.out.println(stations[1]);System.out.println("...");System.out.println(stations[stations.length-2]);System.out.println(stations[stations.length-1]);System.out.println("Distancefrom"+stations[1]+"to"+stations[3]+"is"+line.calculateDistance(stations[1],stations[3]));System.out.println("Distancefrom"+stations[3]+"to"+stations[1]+"is"+line.calculateDistance(stations[3],stations[1]));}}
解决方案
解决方案二:
distance[i]=Integer.parseInt(str2[2]);没有初始化,for(inti=0;i<str.length;i++){之前,加上这句就可以了:distance=newint[str.length];学生吧~很简单的问题,稍微调试一下就出来了
解决方案三:
空指针错误,应该是你某一个数组没有初始化吧…
解决方案四:
没初始化
解决方案五:
//代码好长好乱啊~~我都懒得看了、、、、
解决方案六:
空指针异常是JAVA程序的所有错误里最好查的错误java.exe都已经告诉你哪一行访问了一个空的引用,顺着这个线索找一下就知道原因了
解决方案七:
检查程序里面所有的引用变量看看有哪些没有初始化!!把它们都初始化就行了。。。
解决方案八:
debug就好。