求助java.lang.NullPointerException

问题描述

我一直不能运行,我不懂应该怎样改,我很苦恼啊只一直弹出Exceptioninthread"main"java.lang.NullPointerExceptionatLine.<init>(Te​​stLine.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就好。

时间: 2024-10-09 08:39:35

求助java.lang.NullPointerException的相关文章

ndroid-新人求助 java.lang.NullPointerException

问题描述 新人求助 java.lang.NullPointerException 04-26 21:42:40.039: E/AndroidRuntime(1125): FATAL EXCEPTION: main 04-26 21:42:40.039: E/AndroidRuntime(1125): Process: com.example.remotecontrol, PID: 1125 04-26 21:42:40.039: E/AndroidRuntime(1125): java.lang

配置文件-大神求助《properties.load(fileInputStream )》java.lang.NullPointerException

问题描述 大神求助<properties.load(fileInputStream )>java.lang.NullPointerException 我明明把配置文件路径给对了,可是总是说空指针异常.查了一下午还没有弄明白.求大神解救...调试总是显示在 properties.load(fileInputStream );出现异常..代码如下: public Configuration () throws FileNotFoundException { try { File file=new

android-在fragment转换中出现java.lang.NullPointerException

问题描述 在fragment转换中出现java.lang.NullPointerException 最近学校让做个android音乐播放器,我跟着论坛的教学做到这里时会出现闪退的问题,不知道怎么解决,求助(我是小白,如果可以请说详细些,谢谢啦) 解决方案 http://bbs.csdn.net/topics/390509766 解决方案二: 这是出错的代码 package com.example.test_01; import android.app.Fragment;import androi

JsonMappingException: (was java.lang.NullPointerException)

使用jackson 序列化Java对象的时候报异常: Java代码   com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.chanjet.gov.Student["age"])       at com.fasterxml.jackson.databind.JsonMappingException.

java.lang.NullPointerException: Expected timestamp

java.lang.NullPointerException: Expected timestamp in the Flume event headers, but it was null java.lang.NullPointerException: Expected timestamp in the Flume event headers, but it was null 使用flume的时候出现的问题. sink是hdfs,然后使用目录自动生成功能.出现如题的错误,看官网文档说的是需要在每

JSP页面中出现的一个异常:org.apache.jasper.JasperException: java.lang.NullPointerException

先把出现的异常情况贴出来吧: type Exception report messagejava.lang.NullPointerException descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: java.lang.NullPointerException

图片-svn更新后,jsp页面报java.lang.NullPointerException

问题描述 svn更新后,jsp页面报java.lang.NullPointerException 请大神指点 谢谢 解决方案 svn一般要先更新再提交,有报错的话就先修改错误 解决方案二: 空指针异常,看看你同组的人有没有修改过东西,没有全部上传

java web java.lang.NullPointerException

问题描述 java web java.lang.NullPointerException 严重: Servlet.service() for servlet [selectServlet] in context with path [/Exercise] threw exceptionjava.lang.NullPointerException at DB.DataBaseConnection.selectOperation1(DataBaseConnection.java:45) at Ser

异常-java.lang.NullPointerException

问题描述 java.lang.NullPointerException 请问下这种异常怎么解决呢 解决方案 空指针错误.这种错误一般是由于: 1.使用的类对象为null 2.指针越界 3.配置文件配置路径有错,无法找到路径 解决方案二: SDK和ADT为22.6.2版本 工程为4.4.2 新建了一个工程 ,代码编写好后并没有报错,在安装支持后报出如下错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com