problem with file.

hey fellas!

In the code below I want from one file (that I call germanos.db) to get some values. But I had this error message when I run the program:

Exception in thread "main" java.lang.NullPointerException

at fixedLengthScanner.dbTest(Compiled Code)

at fixedLengthScanner.main(fixedLengthScanner.java:43)

If I remove the file then I have this message that is seems logical:

IOException error: germanos.db (No such file or directory)

My code is:

import java.io.*;

import java.util.*;

import java.util.Date;

import java.sql.*;

import java.sql.Types;

import java.text.*;

import java.net.*;

import java.lang.*;

import dk.digiquant.integration.*;

public class fixedLengthScanner

{

public int index=0;

public String s;

public fixedLengthScanner(String ss)

{

s = ss;

index = 0;

}

public String getField(int len)

{

String retStr = "";

try

{

retStr = s.substring(index, index+len);

index += len;

}

catch (IndexOutOfBoundsException ex)

{

System.out.println("Index out of bounds");

}

return retStr;

}

public static void main(String argv[]) throws Exception

{

//String theName = "arnaki aspro kai paxu" ;

fixedLengthScanner t = new fixedLengthScanner(null);

t.dbTest();

}

void dbTest() {

DataInputStream dis = null;

String dbRecord = null;

try {

File f = new File("germanos.db");

FileInputStream fis = new FileInputStream(f);

BufferedInputStream bis = new BufferedInputStream(fis);

while ( (dbRecord = dis.readLine()) != null) {

fixedLengthScanner fs = new fixedLengthScanner(dbRecord);

String INTContract_AID = fs.getField(8);

String F_INTService_AID = fs.getField(8);

String Update_Flag = fs.getField(1);

String Surname = fs.getField(60);

String Firstname = fs.getField(60);

String CompanyName = fs.getField(60);

String Fiscal_Code = fs.getField(30);

String FiscalAuthority = fs.getField(30);

String City = fs.getField(30);

String Address1 = fs.getField(60);

String Phone = fs.getField(30);

String FAX = fs.getField(30);

String BirthDate = fs.getField(8);

String ZipCode = fs.getField(15);

String Username_1 = fs.getField(48);

String Username_2 = fs.getField(48);

String Username_3 = fs.getField(48);

String Password = fs.getField(16);

String entry_Date = fs.getField(8);

String activation_Date = fs.getField(8);

String deActivation_Date = fs.getField(8);

String Comments = fs.getField(16);

System.out.println("end");

}

}catch (IOException e) {

System.out.println("IOException error: "+e.getMessage());

} finally {

if (dis != null) {

try {

dis.close();

} catch (IOException ioe) {

System.out.println("IOException error trying close the file: "+ioe.getMessage());

}

}

}

}

}

[3174 byte] By [impak] at [2007-9-26 2:45:33]
# 1
In dbTest() you have:DataInputStream dis = null;The next reference to dis is:while ( (dbRecord = dis.readLine()) != null)As dis is null, the exception is thrown. You should initialize dis before using it.
U063667 at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...
# 2
How can I initialize dis, without effecting my program?
impak at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...
# 3

replace this line:BufferedInputStream bis = new BufferedInputStream(fis);

withBufferedReader dis = new BufferedReader( new InputStreamReader( fis ) );

what you have done is declared a DataInputStream, but not initialized it. later you have declared and initialized a BufferedInputStream, but not used it anywhere.

anyway since you seem to want readLine(), you can't use BufferedInputStream. hence my suggestion of BufferedReader.

you could name the BufferedReader anything, but change the lines referencing dis appropriately.

parthasarkar at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...
# 4

well, I've done what you said and now I have this error message:

fixedLengthScanner.java:95: Undefined variable: dis

if (dis != null) {

^

fixedLengthScanner.java:97: Undefined variable or class name: dis

dis.close();

^

fixedLengthScanner.java:98: Exception java.io.IOException is never thrown in the body of the corresponding try statement.

} catch (IOException ioe) {

and I can't understand why.

Any ideas?

impak at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...
# 5
declare the BufferedReader outside the try block like this:BufferedReader dis = null;in the try block initialize it like thisdis = new BufferedReader( new InputStreamReader( fis ) );
parthasarkar at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...
# 6
thanx a lot ;-))it working fine now!!
impak at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...
# 7

DataInputStream dis = null;

String dbRecord = null;

try {

File f = new File("germanos.db");

FileInputStream fis = new FileInputStream(f);

BufferedInputStream bis = new BufferedInputStream(fis);

while ( (dbRecord = dis.readLine()) != null) {

Your dis (DataInputStream) is null when first get to the while loop, and you try to call a method on it.

D

dsklyut at 2007-6-29 10:27:30 > top of Java-index,Archived Forums,Java Programming...