#1
  1. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15

    NullPointerException-why?


    hi there,

    I have the following code in Dic.class:
    Code:
    public class Dic {
    
    	private String s;
    	
    	public void Dic() throws Exception, NullPointerException {
    
    		s="hello";
    	}
    	
    	public boolean inDic (String lemma)throws Exception {
    		if (s==null){
                     throw new Exception("s is null-"+s);
          } else {
                     return true;
           }
    }
    which I am calling from page1.jsp:
    Code:
    try{
    	Dic dic=new Dic();
    	if (inDic(String("xxx"){
                   out.println("ok");
    	} catch (Exception e){
    		out.println("Exception caught: "+e);
    	}
    this should throw an exception if the s string is null in the inDic() function. However in the constructor i set it to 'hello'. So why on earth does this throw an exception???

    any enlightenment gladly received
    Elie
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Try this code in your.jsp. In your code you're trying to call the method inDic without specifying which object you're calling in on.
    If this doesn't fix it, post the error message you're getting.
    Code:
    try{
       Dic dic=new Dic();
       if (dic.inDic("xxx")) {
          out.println("ok");
       }
    } 
    catch (Exception e){
       out.println("Exception caught: "+e);
    }
  4. #3
  5. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    hi there-
    thanks for replying. It still doesn't work! here is my full code:
    Code:
    package dictionaryT;
    
    public class Test {
    
    	private String s;
    
    	public void Dic() throws Exception, NullPointerException {
    		s="hello";
    	}
    
    	public boolean inDic (String lemma) throws Exception {
    		if (s==null){
    			throw new Exception("s is null-"+s);
    		} else if (s==lemma){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    for the Test.class file and:
    Code:
    <%@page import="dictionaryT.*;"%>
    Testing:
    <%
    try{
       Test dic=new Test();
       if (dic.inDic("xxx")==true) {
          out.println("ok");
       } else {
       	out.println("not in dic");
       }
    } catch (Exception e){
       out.println("Exception caught: "+e);
    }
    %>
    for the test.jsp file.

    this gives me the following eror message on screen:
    Code:
    Testing: Exception caught: java.lang.Exception: s is null-null
    which would mean that when called form the inDic function, the s String which was assigned a value in the class constructor loses this value later on.

    but why?

    Elie
  6. #4
  7. Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    49
    Rep Power
    15
    aaaaahhhhh....

    the problem was, the constructor should not have a retun value! so
    Code:
    public void Dic() throws Exception, NullPointerException {
    
    		s="hello";
    	}
    should be:
    Code:
    public Dic() throws Exception, NullPointerException {
    
    		s="hello";
    	}
    doh!

  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Drat! I should have spotted that too!

    BTW - just so that you know, there's no need to compare a boolean value with true in an 'if' statement. An 'if' statement checks to see if whatever is in the brackets is true. Your inDic() method returns only true or false so it's already in the format that the 'if' statement wants.
    Therefore, you can just do this:
    Code:
    if (dic.inDic("xxx")) {
       //whatever
    }
    ~ishnid
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    8
    Rep Power
    0
    Also s==lemma in your inDic() method will not work properly. All that does is compare the obeject reference of s and lemma, and not that the value of the strings are equals. You need to use the equals() or compareTo() in the String class. Just so u understand what I mean I'll give you some examples.

    String s = "hello";
    String x = s;

    (s==x) will return true, because they both point to the same object reference

    But in this example, it is different
    String s = "hello"
    String x = "hello";
    (s==x) will return true because they are 2 different objects
    (s.equals(x) will return tue because the strings have the same value.

    So in your example, it will always return false even if it is the same value unless you are actually passing the reference to the same string object.

    I believe this is more of what you wanted than what you're trying to implement. I hope this helps.

IMN logo majestic logo threadwatch logo seochat tools logo