Java: Why is this code not working? -
can tell me what's wrong java code?
array : static string [][] data = new string[6][2];
in case, data[0][1]="abcd"
, data[1][1]="efgh"
, remaining data[2-5][1]
should null
. trying print out non-null
ones. @ (j=2,k=3)
, code doesn't executed after that. eg, "i here3" doesn't printed.
int k=1; system.out.println("data[2][0]="+data[2][0]); for(int j=0; j<data.length; j++,k++) { system.out.println("j="+j+" k="+k); if (!(data[j][0].equals("null"))) { system.out.println("i here2"); sbissues = sbissues.append("\n"); sbissues = sbissues.append(k); sbissues = sbissues.append(". "); sbissues = sbissues.append(data[j][1]); } system.out.println("sbissues="+sbissues.tostring()); system.out.println("i here1"); } system.out.println("i here3");
the output looks like:
data[2][0]=null j=0 k=1 here2 sbissues= 1. abcd here1 j=1 k=2 here2 sbissues= 1. abcd 2. efgh here1 j=2 k=3
the problem never reach "i here3".
guys answers, correct (to use !=null instead of .equals). guess can't vote of you, i'm allowed vote once.
my guess don't want printing when it's null.
use if statement instead:
if (data[j][0] != null) {
in java complex variables references objects. can have 2 variables pointing same object. when variable pointing nothing it's null. cannot dereference null variable, that's when nullpointerexception.
the primitive data types int, long, byte, etc. behave differently. every time assign you're making copy of data. cannot null.
Comments
Post a Comment