1

In php, it is possible to manipulate string as like as we want, for example:

$temp2 = array(); 
$theindex = 0;
foreach($arraysimpanloop as $a) {
    $temp1 = array(); 
    for($i = 0; $i < sizeof($a)-1; $i++) {
        $temp1[$i] = $a[$i];
    }
    natsort($temp1); // sort array temp1
    $stringimplode = implode($temp1);
    $temp2[$theindex] = $stringimplode;
    $theindex++;
}

Since I'm using Java now, so I'm wondering how the line:

$temp2[$theindex] = $stringimplode;

written in Java code. Because I tried in java:

temp2[theindex]= stImplode; // here temp2 is array of string

and it is always returns a nullpointer.

user3698011
  • 167
  • 3
  • 14
  • Did you initialize the array first? Personally, I doubt that line to throw a NullPointerException. Can you show your Java code and stacktrace? – Stultuske Feb 09 '15 at 07:52
  • 1
    Based on one line of code that you showed us here: `temp2[theindex]= stImplode;` - we can't help you. If you want us to help with the NPE you're getting - publish your code. – Nir Alfasi Feb 09 '15 at 07:54
  • 2
    Add the `java` code please. – Jens Feb 09 '15 at 07:54
  • what is theindex? is it int? are you initialize temp2? But it is more likely that you get NPE because you didnt initialize temp2 – Lrrr Feb 09 '15 at 08:01
  • What is your question? – user253751 Feb 09 '15 at 08:27
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Oleg Estekhin Feb 09 '15 at 08:53

2 Answers2

0

your string array may have not been initialized

int yourSize = ...;
String[] temp2 = new String[yourSize];

that's why you might get null pointer exeception. or you access an element from array that does not exist (e.g. your array has 100 elements and you try to access the 101 element)

aurelius
  • 3,946
  • 7
  • 40
  • 73
0
// You must create array objects prior to use (as your array() does)
String[] temp = new String[]{ "one", "two", "three" };
String[] a = new String[3];

// not trying to do anything meaningful
a[0] = "111";
a[1] = "222";
a[2] = "333";
int i = 0;
for( String e: a ){
     temp[i++] = e;
}
for( int i = 0; i < temp.length; ++i ){
    System.out.println( temp[i] );
}

Check some tutorial on Java arrays...

laune
  • 31,114
  • 3
  • 29
  • 42