i am getting run time error as provided in title
my code is

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);
}
}

Recommended Answers

All 7 Replies

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 != null && the rest)

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".

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 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:

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.

String s1="nikhil is a good boy";
   String []p=s1.split(" ");

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.

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.

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.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.