• Post Reply Bookmark Topic Watch Topic
  • New Topic

extends LinkedList,but use iterator warning that is null  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Where it warns that is null, why is it?


[HENRY: Added Code Tags]
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume it's saying argsStrs is null? That's null because you set it to null. You need to create an array to put the values in.
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags and post correct code so that we can point out the exact issue.
 
filson sun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:Please UseCodeTags and post correct code so that we can point out the exact issue.



Thank you !

like that ,I create a project with two classes

one :

import java.util.LinkedList;

public class ArgumentsList extends LinkedList<String> {
public ArgumentsList(){
init();
}

public void init(){
add("-s");
add("-r");
add("-i");
}
}

two:

import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;



public class SD {


public static void main(String args[]){
Iterator<String>argListIterator=new ArgumentsList().iterator();
String argStrs[]=null;
int j=-0;
while (argListIterator.hasNext()) {
argStrs[j] = (String) argListIterator.next();
j++;
}
}


}


-------------------------------------------
But the problem is the same :"Exception in thread "main" java.lang.NullPointerException
at SD.main(SD.java:15)
"
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew has mentioned the possible reason for the NullPointerException. You need to create an array object instead of initializing it to null.
 
filson sun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohamed Sanaulla wrote:Matthew has mentioned the possible reason for the NullPointerException. You need to create an array object instead of initializing it to null.


Thank you ! Is there a function to quickly initialize the array? only use for(****){} to initialize it ?
 
Sheriff
Posts: 21331
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time.

Why not use the toArray methods?
This version of the toArray method uses the provided array and stores the elements in that array. If the array is not large enough (like my first example) it will use reflection to create a new array of the exact same type as the given array, in this case a String[].
 
filson sun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Please UseCodeTags next time.

Why not use the toArray methods?
This version of the toArray method uses the provided array and stores the elements in that array. If the array is not large enough (like my first example) it will use reflection to create a new array of the exact same type as the given array, in this case a String[].


Thank you !
 
Rob Spoor
Sheriff
Posts: 21331
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!