| | |
null pointer exception error while finding longest word in line
![]() |
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
i am getting run time error as provided in title
my code is
my code is
Java Syntax (Toggle Plain Text)
class maxstring { public static void main(String args[]) { String s1="nikhil is a good boy"; int d;int x=0; d=s1.length(); String[] s2=new String[d]; char ch; for(int i=0;i<=d-1;i++) { ch=s1.charAt(i); if(ch==' ') { s2[x]=s1.substring(x,i-1); x=i+1; } } String max=s2[0]; for(int i=0;i<=d-1;i++) { if(s2[i].compareTo(max)>0) max=new String(s2[i]); } System.out.println(max); } }
Last edited by Tekmaven; Jul 2nd, 2009 at 9:06 pm. Reason: Code Tags
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)
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
333 - halfway to hell
Where are you "getting" it? The exception provides a line number, you know.
Also, why do
I believe, however, that you problem is here
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
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".
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)
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
----------------------------------------------
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
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
[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
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
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
Java Syntax (Toggle Plain Text)
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
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:
Here is my suggestion :
Use split() method of String class.
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".
Use split() method of String class.
Java Syntax (Toggle Plain Text)
String s1="nikhil is a good boy"; String []p=s1.split(" ");
Failure is not fatal, but failure to change might be. - John Wooden
•
•
•
•
[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
Java Syntax (Toggle Plain Text)
class maxstring { public static void main(String args[]) { String s1="nikhil is a good boy"; int d;int x=0; d=s1.length(); /* With this next statement you now have the following array * 0 --> null * 1 --> null * 2 --> null * ... * 17 --> null * 18 --> null * 19 --> null */ String[] s2=new String[d]; char ch; for(int i=0;i<=d-1;i++) { ch=s1.charAt(i); if(ch==' ') { s2[x]=s1.substring(x,i-1); x=i+1; // x is 0,7,10,12,17 } } /* all elements, other than the following are still null * 0 --> nikhil * 7 --> is * 10 --> a * 12 --> good * 17 --> boy */ String max=s2[0]; /* in the following loop all elements except those listed above * will throw an NPE, since those elements are null. */ for(int i=0;i<=d-1;i++) { /* This statement compares the Strings alphabetically * I thought you wanted the longest String? Shouldn't you * be comparing their "length()"s? */ if(s2[i].compareTo(max)>0) max=new String(s2[i]); } System.out.println(max); } }
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
----------------------------------------------
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
•
•
Join Date: Jun 2009
Posts: 99
Reputation:
Solved Threads: 4
Java Syntax (Toggle Plain Text)
Dear Masijade 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.
![]() |
Similar Threads
- Getting a null pointer exception when saving image to file. (Java)
- Null Pointer Exception problem (Java)
- Null Pointer Exception....... (Java)
- Null pointer exception while getting other applet in the same page (Java)
- null pointer exception --- urgent (Java)
- Java Null Pointer Exception (Java)
- getting null pointer assignment error (C)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
- Previous Thread: Need help with character movement on slanted surfaces
- Next Thread: doubt on object reference
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






