Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 10
Registered: ‎09-11-2009
My Device: Not Specified

accessing text file throws an exception

hello

i am trying to access a file 'test.txt' which is saved in a package 'com.Testing'

and the below code is written in file AcesstextFile.java. but it is nt working and  throwing an exception.

 

try {
            Class classs = Class.forName("com.Testing.AcesstextFile.java");
            InputStream is = classs.getResourceAsStream("/test");
            InputStreamReader isr = new InputStreamReader(is);
            char c;
            while ((c = (char)isr.read()) != -1) {
                System.out.print(c);

            }
            

        } catch(Exception ex) {

            System.out.println("---------------  Error: -----------  " + ex.toString());

        }

 

 

 

Exception is :

java.lang.NullPointerException

 

 

suggest me how can i access csv or text file in my blackberry application

 

 

Poojs
India
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: accessing text file throws an exception

My guess is that you do not have a class named "com.Testing.AcesstextFile.java", so classs is null when you try to call getResourceAsStream. If the code you posted is part of an instance method, you can try something like

 

Class classs = getClass();

 

Alternatively, you can try

 

Class classs = class_name.class;

 

where class_name is the name of a class in your app.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Developer
Posts: 10
Registered: ‎09-11-2009
My Device: Not Specified

Re: accessing text file throws an exception

still not  working. and my file name is right what i mentioned afore. suggest me the solution

Poojs
India
Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel

Re: accessing text file throws an exception

String fullPath = "file:///SDCard/foo.txt";
byte[] data = "foo".getBytes();

try {
    FileConnection fconn = (FileConnection) Connector.open(fullPath, Connector.READ_WRITE);

    if (fconn.exists()) {
        fconn.delete();
    }

    fconn.create();

    OutputStream os = fconn.openOutputStream();

    os.write(data);

    os.close();

    fconn.close();
}
catch (IOException e) {
    System.out.println("Oh noes!!1! " + e.toString());
}

 

try this..

Swapnil G.
Trusted Contributor
Posts: 200
Registered: ‎02-22-2010
My Device: BlackBerry 9550
My Carrier: Airtel

Re: accessing text file throws an exception

[ Edited ]

i think you are writing "getResourceAsStream("/test")"  try to convert it to getResourceAsStream("/test.txt") since for bitmap when i have used it doesnt works if we dont give the extension of the file.

 

 

http://developers.sun.com/mobility/apis/articles/fileconnection/index.html

Swapnil G.
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: accessing text file throws an exception

Which line is throwing the null pointer exception?

 

Do you indeed have a file named "test" in the root of the res folder for your project?

 

If you look inside the .jar file created during the build process, does the file "test" show up at the top level?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
New Developer
Posts: 10
Registered: ‎09-11-2009
My Device: Not Specified

Re: accessing text file throws an exception

InputStreamReader isr = new InputStreamReader(is);

above line throwing exception

 

i have tried it both the ways by placing it into res folder or into com.Testing package also.
and the exception is java.lang.NullPointerException

Poojs
India
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: accessing text file throws an exception

It sounds like the file is not being packaged. Did you look inside the .jar file (it's basically a .zip file) to confirm that the file was included?

 

I'm assuming that you're using the Eclipse plug-in. If you created the file outside of the plug-in and copied it to the project directory, you may need to refresh the project before the packager will realize that the file is there. Just highlight the project name and press the F5 key.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Contributor
Posts: 30
Registered: ‎04-13-2010
My Device: N/A
My Carrier: T-Mobile

Re: accessing text file throws an exception

Dude I was having the same error for a while here's how you learn it

 

This should work for you just use the getClass().getResourceAsStream("/text.txt"); This works WITHOUT any package declaration

 

 

 InputStream stream = getClass().getResourceAsStream("/dare.txt");

                  	if(stream != null)
                  	{
                  	    LineReader lineReader = new LineReader(stream);                              
                  	           
                  	    // We read data from input stream one line at a time until we
                  	    // reach end of file. Each line is parsed to extract data.  
                  	    
                  	    Random dice = new Random();
                  	    int num = 0; 
                  	    for (int counter=1; counter<=10; counter++){
                  	        num = dice.nextInt(40);
                  	    }
                  	    
                  	    for(int i =0;i< num;i++)
                  	    {
                  	        try
                  	        {
                  	            String line = new  String(lineReader.readLine());
                  	            text.setText(line);


                  	            	                
                  	            // Parse the current line...
                  	                            
                  	        }
                  	        catch(EOFException eof)
                  	        {
                  	            // We've reached the end of the file.
                  	            break;
                  	        }
                  	        catch(IOException ioe)
                  	        {
                  	            // Error reading data from file
                  	        }               
                  	    }
                  	    
       } 

 

Thanks,
Saula
Your neighborhood programmer.
http://homeworkjava.com