TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   NullPointerException when using array of string in Java (http://forums.techarena.in/software-development/1168368.htm)

Sean J 25-04-2009 08:11 PM

NullPointerException when using array of string in Java
 
I use an API to segment the text into sentences. In the future I want to retrieve each sentence in a case of an array of string.

Code:

String [] tab = null;
  for (int i=0; i<annotations.size() i++) {
                                  Annotation annotation = annotations. Get (i);
  if (annotation!= null && (annotation.getType().equals ("Sentence"))) {
  DocumentContent content = currDoc.GetContent();
      Node = startNode annotation.GetStartNode();
        Node = endNode annotation.GetEndNode();
        long start = startNode.getOffset();
        long end = endNode.getOffset();
  tab [i] = content. getContent (start, end). toString ();
  }

This Code generates an exception: Exception in thread "main" java.lang.NullPointerException.

Note that this same code works fine by putting

Code:

System. Out. Println (i + ":" + content. GetContent (start, end). ToString ());

Modifier 25-04-2009 08:12 PM

Re: NullPointerException when using array of string in Java
 
Quote:

Originally Posted by Sean J (Post 4380779)
This Code generates an exception: Exception in thread "main" java.lang.NullPointerException.

You manipulating a null at line mentioned in your exception.

Lemog 25-04-2009 08:14 PM

Re: NullPointerException when using array of string in Java
 
The problem comes from
Code:

String [] tab = null;
or rather that you had not initialized tab.

Code:

String [] tab = new String [annotations.size ()]
should solve the problem.


All times are GMT +5.5. The time now is 06:39 AM.