null pointer exception error while finding longest word in line

Reply

Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

null pointer exception error while finding longest word in line

 
0
  #1
Jul 2nd, 2009
i am getting run time error as provided in title
my code is
  1. class maxstring
  2. {
  3. public static void main(String args[])
  4. {
  5. String s1="nikhil is a good boy";
  6. int d;int x=0;
  7. d=s1.length();
  8. String[] s2=new String[d];
  9. char ch;
  10. for(int i=0;i<=d-1;i++)
  11. {
  12. ch=s1.charAt(i);
  13. if(ch==' ')
  14. {
  15. s2[x]=s1.substring(x,i-1);
  16. x=i+1;
  17. }
  18. }
  19. String max=s2[0];
  20. for(int i=0;i<=d-1;i++)
  21. {
  22. if(s2[i].compareTo(max)>0)
  23. max=new String(s2[i]);
  24. }
  25. System.out.println(max);
  26. }
  27. }
Last edited by Tekmaven; Jul 2nd, 2009 at 9:06 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 53
Reputation: di2daer is an unknown quantity at this point 
Solved Threads: 12
di2daer's Avatar
di2daer di2daer is offline Offline
Junior Poster in Training

Re: null pointer exception error while finding longest word in line

 
0
  #2
Jul 2nd, 2009
Hi,
when you create your array you give it the length of all the characters in the string you're analyzing, but you're not initiaing it, so it contains only null (s2[0] = null, s2[1] = null etc) since string is an object. In the last for-loop you're calling method compareTo() for a null object. You can add a null check in the if statement to avoid this problem (if(s2[i] != null && the rest)
---------------------------
333 - halfway to hell
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: null pointer exception error while finding longest word in line

 
0
  #3
Jul 2nd, 2009
Where are you "getting" it? The exception provides a line number, you know.

Also, why do for(int i=0;i<=d-1;i++) when you can do for(int i=0;i<d;i++).

I believe, however, that you problem is here
  1. String[] s2=new String[d];

Doing that creates an array of reference variables that must point to a String, however, initially all the elements contain a null pointer. You need to make sure that every element actually contains a reference to a String or the line if(s2[i].compareTo(max)>0) will cause an NPE on any element where you have not explicitly initiated a String reference.

You can probably solve it by changing ....

Edit:

Scratch that, I'm not sure what you're trying to do with that "logic" so I'll simply second that "add a null check".
Last edited by masijade; Jul 2nd, 2009 at 3:49 pm.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: null pointer exception error while finding longest word in line

 
0
  #4
Jul 2nd, 2009
[code]
i am trying to create an array of strings with String [] s2=new String[25]
i will store every word in that array my line is "nikhil is a good boy"(s1) i am using substring to separate those words removing the spaces and put it into s2 array i want s2[0]=nikhil
s2[1]=is s2[2]=a s2[3]=good s[4]=boy i wish to print the longest word in the string s1
please help
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: null pointer exception error while finding longest word in line

 
0
  #5
Jul 2nd, 2009
  1. i commented the tocompare part and changed max=new s2...line in it to max=s2 but same error.It is not printing the words separately remooving spaces. i got the program with stringtokenizer but i want to create it on my own
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,429
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 436
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

Re: null pointer exception error while finding longest word in line

 
0
  #6
Jul 3rd, 2009
akulkarni,

14th post and you don't know, how to and where to use code tag?
Read this How to use code tags.

Try to include a complete description of your problem at your first post of this thread.

What masijade said:
Scratch that, I'm not sure what you're trying to do with that "logic" so I'll simply second that "add a null check".
Here is my suggestion :
Use split() method of String class.
  1. String s1="nikhil is a good boy";
  2. String []p=s1.split(" ");
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: null pointer exception error while finding longest word in line

 
0
  #7
Jul 3rd, 2009
Originally Posted by akulkarni View Post
[code]
i am trying to create an array of strings with String [] s2=new String[25]
i will store every word in that array my line is "nikhil is a good boy"(s1) i am using substring to separate those words removing the spaces and put it into s2 array i want s2[0]=nikhil
s2[1]=is s2[2]=a s2[3]=good s[4]=boy i wish to print the longest word in the string s1
please help
Okay, so let's go through a bit of what you are actually doing.

  1. class maxstring {
  2. public static void main(String args[]) {
  3. String s1="nikhil is a good boy";
  4. int d;int x=0;
  5. d=s1.length();
  6. /* With this next statement you now have the following array
  7.   * 0 --> null
  8.   * 1 --> null
  9.   * 2 --> null
  10.   * ...
  11.   * 17 --> null
  12.   * 18 --> null
  13.   * 19 --> null
  14.   */
  15. String[] s2=new String[d];
  16. char ch;
  17. for(int i=0;i<=d-1;i++) {
  18. ch=s1.charAt(i);
  19. if(ch==' ') {
  20. s2[x]=s1.substring(x,i-1);
  21. x=i+1; // x is 0,7,10,12,17
  22. }
  23. }
  24. /* all elements, other than the following are still null
  25.   * 0 --> nikhil
  26.   * 7 --> is
  27.   * 10 --> a
  28.   * 12 --> good
  29.   * 17 --> boy
  30.   */
  31. String max=s2[0];
  32. /* in the following loop all elements except those listed above
  33.   * will throw an NPE, since those elements are null.
  34.   */
  35. for(int i=0;i<=d-1;i++) {
  36. /* This statement compares the Strings alphabetically
  37.   * I thought you wanted the longest String? Shouldn't you
  38.   * be comparing their "length()"s?
  39.   */
  40. if(s2[i].compareTo(max)>0)
  41. max=new String(s2[i]);
  42. }
  43. System.out.println(max);
  44. }
  45. }

A split() will give you an array of Strings, but that is not exactly "doing it yourself" as you said you wanted to. If you really want to parse the String yourself to create an array of Strings, then either use an ArrayList or handle your index better, or simply add the null check (which is the least effecient variant), or simply get the lengths of the Strings as you "find" them and compare them to the longest up to that point.
Last edited by masijade; Jul 3rd, 2009 at 3:30 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 99
Reputation: akulkarni is an unknown quantity at this point 
Solved Threads: 4
akulkarni akulkarni is offline Offline
Junior Poster in Training

Re: null pointer exception error while finding longest word in line

 
0
  #8
Jul 4th, 2009
  1.  
  2.  
  3. Dear Masijade
  4. i tried with finding length and then comparing but found it impossible. can u help me about that.Also how do i do the Null check .thanks for your explaination i will give more time to think over it cause right now i am not able to visualise that NPE error falling short of grasping the concept.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC