日期:2014-05-16  浏览次数:20309 次

关于java.lang.NullPointerException的错误
我是一个新手,尝试做了一个程序,但执行总是报java.lang.NullPointerException错,请专家帮我看看:
A:执行页面STeam_S.jsp的代码:
<%@   page   language= "java "   import= "java.util.* "   pageEncoding= "UTF-8 "%>
<%@page   import= "com.mrpsoft.bean.team.* "%>
<%@page   import= "com.mrpsoft.common.* "%>
<%@page   import= "com.mrpsoft.bean.sysAdmin.* "%>
<%@page   import= "com.mrpsoft.team.* "%>
<%@page   import= "java.sql.* "%>
<%@page   import= "java.text.* "%>
<%@page   import= "com.ibatis.sqlmap.client.SqlMapClient "%>
<% String   tempTeamId   =   request.getParameter( "tempTeamId ");
STeamForm   STF   =   null;
List   JouList   =   null;
int   Jourdays   =   0;
if(tempTeamId!=null)
{
STF     =   STeamMng.getSTeamFormInfo(tempTeamId);
JouList   =   TeamMng.getTeamJourney(tempTeamId);
Jourdays   =   JouList.size();
}
else
{
response.sendRedirect( "STeam_S.jsp ");
}

%>

<%=STeamMng.getStatusCN(STF.getStatus())%>

B:Java文件:
package   com.mrpsoft.team;

import   java.io.Serializable;

import   com.mrpsoft.common.*;
import   com.mrpsoft.dispatch.*;
import   com.mrpsoft.dataDeal.sysAdmin.*;
import   com.mrpsoft.bean.team.*;
import   com.mrpsoft.bean.sysAdmin.*;
import   com.mrpsoft.bean.dispatch.*;

import   java.util.*;
import   java.text.*;

import   com.ibatis.sqlmap.client.SqlMapClient;
import   java.sql.SQLException;

public   class   STeamMng   implements   Serializable
{
private   static   SqlMapClient   sqlMap   =   IbatisUtil.getSqlMapClient();

//团队状态中文显示
public   static   String   getStatusCN(String   status)
{
String   CNName   =   " ";
if(status.equalsIgnoreCase( "2 "))
{
CNName   =   "等待拼团 ";
return   CNName;
}
else   if(status.equalsIgnoreCase( "3 "))
{
CNName   =   "正式成团 ";
}
else  
{
CNName   =   "散团状态 ";
}
return   CNName;
}

}

报错提示:
java.lang.NullPointerException
com.mrpsoft.team.STeamMng.getStatusCN(STeamMng.java:26)
org.apache.jsp.team.STeamForm_005fA_jsp._jspService(STeamForm_005fA_jsp.java:315)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
以下提示略...
请专家帮我看看错在哪里

------解决方案--------------------
猜测应该status是null的原因
你在使用一个对象的属性或者方法的时候要能保证他不是null,否则就会出现你那个异常。
你把代码改成:
public static String getStatusCN(String status)
{
String CNName = " ";
if( "2 ".equalsIgnoreCase(status))
{
CNName = "等待拼团 ";
return CNName;
}
else if( "3 ".equalsIgnoreCase(status))
{
CNName = "正式成团 ";
}
else
{
CNName = "散团状态 ";
}
return CNName;
}
这样可以保证不出现上面的异常。