nullpointerexception - NullReferenceException was unhandled Object reference not set to an instance of an object. c# -
i tried running program, , have compared code friend's code, error keeps on showing though tried changing these lines (which think error coming from):
try { filestream b = new filestream(@"c:\user\user_2\desktop\board.txt", filemode.open); streamreader stream = new streamreader(b); int x = 0; while (!stream.endofstream) { if (x == 0) g1 = stream.readline().split(' '); else if (x == 1) g2 = stream.readline().split(' '); else if (x == 2) g3 = stream.readline().split(' '); x++; } stream.close(); b.close(); } catch (exception e) { }
the program used check text file contains these 3 lines:
o . x x o . x . o
...and see if there winner.
this part visual studio highlights error:
int n = 0; int m = n + 1; int o = m + 1; boolean result = false; int winner = 0; string dw = ""; while (n <= 2) { // in if-statement error: if (g1[n].equals(g2[n]) && g2[n].equals(g3[n]) && !g1[n].equals(".")) { result = true; winner++; dw = g1[n]; } if (g1[n].equals(g1[m]) && g1[m].equals(g1[o]) && !g1[n].equals(".")) { result = true; winner++; dw = g1[n]; } else if (g2[n].equals(g2[m]) && g2[m].equals(g2[o]) && !g2[n].equals(".")) { result = true; winner++; dw = g2[n]; } else if (g3[n].equals(g3[m]) && g3[m].equals(g3[o]) && !g3[n].equals(".")) { result = true; winner++; dw = g3[n]; } else if (g1[n].equals(g2[m]) && g2[m].equals(g3[o]) && !g1[n].equals(".")) { result = true; winner++; dw = g1[n]; } else if (g3[n].equals(g2[m]) && g2[m].equals(g1[o]) && !g3[n].equals(".")) { result = true; winner++; dw = g3[n]; } n++; }
i don't know know, can't make work.
edit: tried printing out values of arrays before if statements doesn't print out. sorry guys, i'm new here.
first of all, try reading file this:
using system.io; try { var file = @"c:\user\user_2\desktop\board.txt"; var lines = file.readalllines(file).tolist(); var g1 = lines[0].split(' '); var g2 = lines[1].split(' '); var g3 = lines[2].split(' '); } catch (exception e) { throw e; }
probably you'll exception thrown.
the line catch (exception e) { }
culprit not being notified there exception. should handle exceptions catch in code.
p.s. have use c:\users\...
in path, instead of c:\user\...
. anyway, make sure use valid path file.
Comments
Post a Comment