package library.model;import library.Constants;public class Test {
private String doctype=null;
private String match_flag=null;
private String strSearchType=null;
private String strTextPost=null;
private String author=null;
private String publisher=null;
private String suoqu_no=null;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getSuoqu_no() {
return suoqu_no;
}
public void setSuoqu_no(String suoqu_no) {
this.suoqu_no = suoqu_no;
}
public String getDoctype() {
return doctype;
}
public void setDoctype(String doctype) {
this.doctype = doctype;
}
public String getMatch_flag() {
return match_flag;
}
public void setMatch_flag(String match_flag) {
this.match_flag = match_flag;
}
public String getStrSearchType() {
return strSearchType;
}
public void setStrSearchType(String strSearchType) {
this.strSearchType = strSearchType;
}
public String getStrTextPost() {
return strTextPost;
}
public void setStrTextPost(String strTextPost) {
this.strTextPost = strTextPost;
}
public String getstrsql(){
String strSql = new String("SELECT * FROM " + Constants.TABLENAME + " WHERE ");
if (!strSearchType.equals(null))
strSql = strSql + strSearchType+" LIKE '" ;
if (match_flag.equals("0"))
strSql = strSql +strTextPost+"%'" ;
if (match_flag.equals("1"))
strSql = strSql + "%"+strTextPost+"%'";
if(!author.equals(null))
strSql=strSql+"and"+"author"+"like '"+author+"'";
if(!publisher.equals(null))
strSql=strSql+"and"+"publisher"+"like '"+publisher+"'";
if(!suoqu_no.equals(null))
strSql=strSql+"and"+"suoqu_no"+"like '"+suoqu_no+"'";
return strSql=strSql+";";
}
public static void main(String[] args){
Test test=new Test();
test.setDoctype("doctype");
test.setStrSearchType("strSearchType");
test.setStrTextPost("strTextPost");
test.setMatch_flag("match_flag");
String sql=new String(test.getstrsql());
System.out.println(sql);
}
}
编译成功,可运行出现下列错误:
Exception in thread "main" java.lang.NullPointerException
at library.model.Test.getstrsql(Test.java:73)
at library.model.Test.main(Test.java:90)
Press any key to continue...
请问这是怎么回事?
private String doctype=null;
private String match_flag=null;
private String strSearchType=null;
private String strTextPost=null;
private String author=null;
private String publisher=null;
private String suoqu_no=null;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getSuoqu_no() {
return suoqu_no;
}
public void setSuoqu_no(String suoqu_no) {
this.suoqu_no = suoqu_no;
}
public String getDoctype() {
return doctype;
}
public void setDoctype(String doctype) {
this.doctype = doctype;
}
public String getMatch_flag() {
return match_flag;
}
public void setMatch_flag(String match_flag) {
this.match_flag = match_flag;
}
public String getStrSearchType() {
return strSearchType;
}
public void setStrSearchType(String strSearchType) {
this.strSearchType = strSearchType;
}
public String getStrTextPost() {
return strTextPost;
}
public void setStrTextPost(String strTextPost) {
this.strTextPost = strTextPost;
}
public String getstrsql(){
String strSql = new String("SELECT * FROM " + Constants.TABLENAME + " WHERE ");
if (!strSearchType.equals(null))
strSql = strSql + strSearchType+" LIKE '" ;
if (match_flag.equals("0"))
strSql = strSql +strTextPost+"%'" ;
if (match_flag.equals("1"))
strSql = strSql + "%"+strTextPost+"%'";
if(!author.equals(null))
strSql=strSql+"and"+"author"+"like '"+author+"'";
if(!publisher.equals(null))
strSql=strSql+"and"+"publisher"+"like '"+publisher+"'";
if(!suoqu_no.equals(null))
strSql=strSql+"and"+"suoqu_no"+"like '"+suoqu_no+"'";
return strSql=strSql+";";
}
public static void main(String[] args){
Test test=new Test();
test.setDoctype("doctype");
test.setStrSearchType("strSearchType");
test.setStrTextPost("strTextPost");
test.setMatch_flag("match_flag");
String sql=new String(test.getstrsql());
System.out.println(sql);
}
}
编译成功,可运行出现下列错误:
Exception in thread "main" java.lang.NullPointerException
at library.model.Test.getstrsql(Test.java:73)
at library.model.Test.main(Test.java:90)
Press any key to continue...
请问这是怎么回事?
73行取不到数据 看看是不是
strSql=strSql+"and"+"author"+"like '"+author+"'";
这句author没有被实例化.
Test test = new Test();
test.setDoctype("doctype");
test.setStrSearchType("strSearchType");
test.setStrTextPost("strTextPost");
test.setMatch_flag("match_flag");
test.setAuthor("hehe");
test.setPublisher("O'Reilly");
test.setSuoqu_no("123");
String sql = new String(test.getstrsql());
System.out.println(sql);
}
if (author == null)
strSql = strSql + "and" + "author" + "like '" + author + "'";
if (publisher != null)
strSql = strSql + "and" + "publisher" + "like '" + publisher + "'";
if (suoqu_no != null)
strSql = strSql + "and" + "suoqu_no" + "like '" + suoqu_no + "'";
具体看你自己的业务逻辑来改, 反正原因就是这个.
strSql=strSql+"and"+"author"+"like '"+author+"'";
if(!publisher.equals(null))
strSql=strSql+"and"+"publisher"+"like '"+publisher+"'";
if(!suoqu_no.equals(null))
strSql=strSql+"and"+"suoqu_no"+"like '"+suoqu_no+"'"; 问题就出在这几行代码上,你在判断arthor是否为空时应该这么写:
if(author!=null)
strSql=strSql+"and"+"author"+"like '"+author+"'";
否则就可能出现问题,试想一下,如果author真的为null,你还在他的上面调用equals()方法,这时肯定会有NullPointerException的,
如果你单纯的要判断一个引用变量是否为null,你可以简单的使用x!=null即可,
如果你要判断它的值是否和另外一个对象相等,这时你在使用equals()方法,
但你要保证以下两点:
(1)调用equals()的引用变量不能为null;
(2)调用equals()的对象中的equals()方法已被有效的重写过,否则你只是在使用Object中的equals(),它只是在单纯的比较两个引用变量是否引用同以个对象。
以下是修改过的程序
package library.model;import library.Constants;
public class Test {
private String doctype=null;
private String match_flag=null;
private String strSearchType=null;
private String strTextPost=null;
private String author=null;
private String publisher=null;
private String suoqu_no=null;
private String TABLENAME="name"; public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getSuoqu_no() {
return suoqu_no;
}
public void setSuoqu_no(String suoqu_no) {
this.suoqu_no = suoqu_no;
}
public String getDoctype() {
return doctype;
}
public void setDoctype(String doctype) {
this.doctype = doctype;
}
public String getMatch_flag() {
return match_flag;
}
public void setMatch_flag(String match_flag) {
this.match_flag = match_flag;
}
public String getStrSearchType() {
return strSearchType;
}
public void setStrSearchType(String strSearchType) {
this.strSearchType = strSearchType;
}
public String getStrTextPost() {
return strTextPost;
}
public void setStrTextPost(String strTextPost) {
this.strTextPost = strTextPost;
}
public String getstrsql(){
String strSql = new String("SELECT * FROM " + Constants.TABLENAME + " WHERE "); try{
if (!strSearchType.equals(null))
strSql = strSql + strSearchType+" like '" ;
if (match_flag.equals("0"))
strSql = strSql +strTextPost+"%'" ;
if (match_flag.equals("1"))
strSql = strSql + "%"+strTextPost+"%'";
if(author != null)
strSql=strSql+"and"+"author"+"like '"+author+"'";
if(publisher != null)
strSql=strSql+"and"+"publisher"+"like '"+publisher+"'";
if(suoqu_no != null)
strSql=strSql+"and"+"suoqu_no"+"like '"+suoqu_no+"'";
}catch(NullPointerException npe){
npe.printStackTrace();
} return strSql=strSql+"';";
}
public static void main(String[] args){
Test test=new Test();
test.setDoctype("doctype");
test.setStrSearchType("strSearchType");
test.setStrTextPost("strTextPost");
test.setMatch_flag("match_flag");
String sql=new String(test.getstrsql());
System.out.println(sql);
}
}