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 ());
Re: NullPointerException when using array of string in Java
Quote:
Originally Posted by
Sean J
This Code generates an exception: Exception in thread "main" java.lang.NullPointerException.
You manipulating a null at line mentioned in your exception.
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.