Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 26th, 2003, 10:28 AM
zcrar70's Avatar
zcrar70 zcrar70 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 49 zcrar70 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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

Reply With Quote
  #2  
Old March 26th, 2003, 01:33 PM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,568 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 1 h 39 m 10 sec
Reputation Power: 1395
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);
}

Reply With Quote
  #3  
Old March 27th, 2003, 04:04 AM
zcrar70's Avatar
zcrar70 zcrar70 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 49 zcrar70 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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

Reply With Quote
  #4  
Old March 27th, 2003, 04:49 AM
zcrar70's Avatar
zcrar70 zcrar70 is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 49 zcrar70 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
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!


Reply With Quote
  #5  
Old March 27th, 2003, 05:48 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Sep 2001
Location: Dublin, Eire
Posts: 5,568 ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level)ishnid User rank is General 6th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 4 Days 1 h 39 m 10 sec
Reputation Power: 1395
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

Reply With Quote
  #6  
Old March 31st, 2003, 05:06 PM
svelez svelez is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 8 svelez User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > NullPointerException-why?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway