null pointer exception in split function
Hello all
I am using reading a config file in which there is a entry
code_list="515,522,560,000"
while i am reading this string in my java application and using split on it and then assigning it to a string[].it is giving null pointer exception.needed code is as follows
static String[] splittArray=null;
code_req=properties.getProperty("code_list");
if (code_req != null || !code_req.equalsIgnoreCase(""))
{
splittArray=code_req.split(",");
System.out.println(code_req + " code request and splittarray" + splittArray);
}
output comes as follows
Reading configuration file :: ./config.cfg
"515,522,560,000" code request and splittarray[Ljava.lang.String;@a981ca
This exception is caught
READ CONFIG FILE :: java.lang.NullPointerException
manish250
Junior Poster in Training
87 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
For one, your if statement should be && not ||
As it is now, if it is null it attempts to do equalsIgnoreCase on that nuill reference.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
That doesn't look like the exception is thrown until after the System.println on line 6, so maybe it's not the split that is the prolem.
What is the exact complete exception message (including the line number)?
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
For one, your if statement should be && not ||
As it is now, if it is null it attempts to do equalsIgnoreCase on that nuill reference.
this is complete code
code_req=properties.getProperty("code_list");
if (code_req != null && !code_req.equalsIgnoreCase(""))
{
splittArray=code_req.split(",");
<strong>System.out.println(code_req + " code request and splittarray" + splittArray);</strong>
}
for(int n=0;splittArray!=null;n++)
{
strSub[n]=properties.getProperty("URL_SUB_"+splittArray[n]);
System.out.println("Url are"+strSub[n]);
}
java.lang.NullPointerException
at sub_unsub.readConfigFile(sub_unsub.java:82)//line in green color
at sub_unsub.main(sub_unsub.java:233)
But i am still not able to get complet array "SplitArray" after splitting the string in BOLD LINE.please suggest how i got that comma separated string into array.CURRENTLY ARRAY IN PRINTED AS
"515,522,560,000" code request and splittarray[Ljava.lang.String;@8814e9
manish250
Junior Poster in Training
87 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
Well, something on that line is null, even if it weren't that loop would eventually lead to an indexOutOfBoundsException since your for loop condition is corked. Either strSub is null, or properties is null (kind of doubt this one as it was used before the previous loop).
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Is your problem in printing the array [Ljava.lang.String;@8814e9 ?
That's how println prints an array of Strings.
If you want to see the contents either loop thru the elements printing them one at at time or use Arrays.toString(..)
JamesCherrill
Posting Genius
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Is your problem in printing the array [Ljava.lang.String;@8814e9 ?
That's how println prints an array of Strings.
If you want to see the contents either loop thru the elements printing them one at at time or use Arrays.toString(..)
Thanks you solved my problem.
manish250
Junior Poster in Training
87 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1