Try
Dim sr As New IO.StreamReader(Mapfile & ".txt")
'Dim intValue As String = ""
Dim strLine As String = ""
Dim X As Integer = 0
Dim Y As Integer = 0
Do Until sr.EndOfStream
strLine = sr.ReadLine
strLine = strLine.Replace(strLine.LastIndexOf(","), "")
For Each item As String In Split(strLine, ",", -1)
'MsgBox("X:" & X & " Y:" & Y & "= " & item)
If item = "" Then
item = 0
End If
If X <= MapWidth Then
Map(X, Y, 0) = Int(item)
End If
X = X + 1
Next
X = 0
Y = Y + 1
Loop
sr.Close()
sr.Dispose()
Catch ex As Exception
MsgBox("Map: " & Mapfile & " could not be loaded." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "ERROR")
IsOn = False
End Try
Trying to port this code over from Visual Basic To Java. I’ve Tried using Buffered Reader but nothing seems To Make it happen. The Code Above is for Visual Basic, The Code below is my java port that doesnt seem to be working the same. http://pastebin.com/freXYTi3
public void readFile(Context c) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(c.getAssets().open("map1.txt")));
String line = null;
String newLine = "";
int x = 0;
int y = 0;
while ((line = br.readLine()) != null) {
int length = line.length();
String lastChar = line.substring(length-1);
if (lastChar.contains(",")) {
newLine = line.substring(0,length-1) + "";
}
//line = line.substring(0, line.lastIndexOf(",")) + "";
for (String str : line.split(",", -1)) {
System.out.println(str);
if(str == ""){
str = "0";
}
if(x <= mapwidth){
System.out.println(x + " " + y);
int N = Integer.parseInt(str);
Map[x][y] = N;
}
x = x + 1;
}
x = 0;
y = y + 1;
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (br != null)
br.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
Without knowing the exact line you are failing on (the error codes give line numbers, but I don’t know the correlation to your file), the only thing I notice is that this line:
MIGHT be a one-off bug. I think VB is 1-based and Java is zero based, but it’s just a guess that you might want < rather than <= Can you let us know what line the NPE was on.
Also this is wrong:
needs to be “str.equals(“”)” or “str.length()==0”
but I don’t see anything that could cause an NPE within the loop
Also you assign newline in the loop and you never use it within that scope, so if it exits and you have another “newline” and you expect it to be set–don’t hold your breath.