Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

extends LinkedList,but use iterator warning that is null

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic