0

I just started programming in java (yesterday as a metter of fact) and I'm messin' around trying to learn the basics. So I decided that I wanted to read a string, save the words in an array of strings and then sort that array and output it sorted. Here's the code for it:

package project2;
import java.util.Scanner;

class First
{public static void main(String args[])
{   Scanner scan=new Scanner(System.in);
    Second obj=new Second(2,3);
    String s; // the string
    String word[]=new String[10]; // the array of strings
    int n=0; // the number of elements in the array
    System.out.print("String: ");
    s=scan.nextLine();
    System.out.printf("This is the string: %s\n", s);
    for(String i : s.split("[,. ]") )
        {word[n]=i;
        n++;}

    for(int j=0; j<word.length;j++) //1
     {for (int i=j+1 ; i<word.length; i++)
         {if(word[i].compareTo(word[j])<0)
             {String temp= word[j];
              word[j]= word[i]; 
              word[i]=temp;}}} //2


    System.out.print("The sorted string is: ");
    for(int i=0;i<n;i++)
        System.out.print(" "+word[i]); 

}}

Now, when I try to run it for the input:

aa bb cc

I get the error:

Exception in thread "main" aa bb cc java.lang.NullPointerException at project2.First.main(First.java:20)

Now the error comes from within 1 and 2, but I can't figure out what causes the error.

Vasile Turcu
  • 153
  • 11
  • 1
    What's causing the error is the fact that one of your values is null. You should use your debugger to step through the program and figure out which one. Or you could look at the error, and chances are that it will give you the line where the null value is occurring at. – BlackHatSamurai Oct 11 '16 at 20:33
  • 1
    If you used `word = s.split("[,. ]");` instead of the for each loop, you wouldn't have the issue. The problem is that you're not entering a string which splits into exactly 10 tokens, so elements of the array allocated via `String word[] = new String[10];` remain null (if you entered more than 10, you'd get an `ArrayIndexOutOfBoundsException` with the current code). – Andy Turner Oct 11 '16 at 20:34
  • 1
    BTW: `String[] word`, not `String word[]`. The latter is legal syntax, but the "array-ness" is part of the type, not the variable name. – Andy Turner Oct 11 '16 at 20:38
  • 1
    Also instantiate your Scanner with proper delimiter for your purposes: Scanner scanner = new Scanner(System.in).useDelimiter("\\n");. Because scanner will receive only "aa" from your input, because using default whitespace delimiter. – eg04lt3r Oct 11 '16 at 20:38
  • @AndyTurner right, I fixed it, thanks bud. – Vasile Turcu Oct 11 '16 at 20:42
  • 1
    And according to Andy suggestion you do not need extra word array. You can override it as: String[] words = s.split("[,. ]"); In this case your NullPointerException should disappear. – eg04lt3r Oct 11 '16 at 20:42
  • 1
    I think that you have made a long way without any need. Java includes variety of libraries and functionalities that are built in your JDK and you can use it freely. In that case you can easily use the **sort()** method as the following example: String[] strings = { " DD ", " CC ", " BB ", " EE ", " AA " }; Arrays.sort(strings); No Null Pointer Exception with be in such a simple and clear code. – Rotem Oct 11 '16 at 20:42
  • 1
    @rotemy, he is newbie and he tries to write program without external libs, in this case he can learn better java basics and how basic sorting algorithms works. It's really good for beginners. But in future will be much better for him to know these common libraries. – eg04lt3r Oct 11 '16 at 20:45
  • 1
    @eg04lt3r You are very right when you try to learn basics, but as far as I saw during the last years many newbies are not familiar with the extensive methods and libraries they have in the box. And as a newbie it will be also a great lesson to go over an existing code which is in extensive use to get a great example how to make such functionality. – Rotem Oct 11 '16 at 20:50

0 Answers0