![]() |
| ||
| null pointer exception error while finding longest word in line i am getting run time error as provided in title my code is class maxstring |
| ||
| 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) |
| ||
| 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 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". |
| ||
| 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 |
| ||
| Re: null pointer exception error while finding longest word in line 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 |
| ||
| 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:
Use split() method of String class. String s1="nikhil is a good boy"; |
| ||
| Re: null pointer exception error while finding longest word in line Quote:
class maxstring {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. |
| ||
| Re: null pointer exception error while finding longest word in line
|
| All times are GMT -4. The time now is 1:36 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC