943,808 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1095
  • Java RSS
Jul 2nd, 2009
0

null pointer exception error while finding longest word in line

Expand Post »
i am getting run time error as provided in title
my code is
Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jul 2nd, 2009
0

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

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)
Reputation Points: 22
Solved Threads: 13
Junior Poster in Training
di2daer is offline Offline
66 posts
since Sep 2008
Jul 2nd, 2009
0

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

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
Java Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 2nd, 2009
0

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

[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
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jul 2nd, 2009
0

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

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009
Jul 3rd, 2009
0

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

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:
Quote ...
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.
Java Syntax (Toggle Plain Text)
  1. String s1="nikhil is a good boy";
  2. String []p=s1.split(" ");
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 3rd, 2009
0

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

Click to Expand / Collapse  Quote originally posted by akulkarni ...
[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.

Java Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jul 4th, 2009
0

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

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 4
Junior Poster
akulkarni is offline Offline
111 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help with character movement on slanted surfaces
Next Thread in Java Forum Timeline: doubt on object reference





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC