Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 21602
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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: 21602
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Yup, yup, yup. Tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!