|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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);
}
__________________
~ishnid; Have you tried: [ search.cpan.org | perldoc | Java API | mysql.com | google ] Apostrophes are NOT used for possessive pronouns or for noun plurals, including acronyms. |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
||||
|
||||
|
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! ![]() |
|
#5
|
||||
|
||||
|
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 |
|
#6
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > NullPointerException-why? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|